Threads Flashcards
0
Q
Describe the different threading models operating systems use.
A
- We have to distinguish between user threads and kernel threads. User threads must at some point be mapped to kernel threads for threads to be able to make system calls. There are 3 possible ways for this mapping to occur.
1) Many to one model. In this model, many threads are mapped to one kernel thread. This is not a great model for concurrency. If a single user thread makes a system call, every other threads are blocked. Used in older system. No longer common.
2) One to one model. One user thread to one kernel thread. This has the advantage of each user thread being independent, but is expensive. Applications usually have to limit number of threads when using this model.
3) Many to many model. Large number of user threads are mapped to a smaller number of kernel threads. This has the benefit of not being too expensive and one system call does not block the whole system and cpu can change to threads mapped to other kernel threads.
1
Q
Describe what threads are and benefits of threads.
A
- Threads are contained in a process , but has their own program counter, register set, and stack. Threads share heap, data section, and code section with other threads in the same process, however.
- Threads are useful when you have a program that needs to do things in parallel, such as browser that needs to render the page while accepting user input and fetch data from network.
- Threads help programs be more responsive, easily share data with other threads, and is a lot cheaper than processes to create. They are also a lot more easily scalable.
2
Q
Describe explicit threading
A
- You can explicitly create threads using available thread libraries such as Pthread, windows thread, and java threads.
- Pthreads is a specification for thread API in POSIX standard. It can be implemented using any of the user to kernel mapping model.
- Windows thread is specific to windows and uses one-to-one model.
- Java threads are implemented on different operating systems using different thread library so the model depends on the underlying thread library. It provides a decent abstraction of thread creation process, however.
3
Q
Describe implicit threading.
A
- It is sometimes best to take away details behind thread creation from application developers and put it either at language level or os level. There are a few ways to do this.
- Thread pool. It is expensive to continuously create new thread and we also can’t create new threads infinitely. To solve these 2 problems, thread pools are often used. A limited amount of threads are created and put in thread pool at beginning and we request threads from pool as necessary. If no threads are available, then we must wait for one to become available.
- OpenMP. Available in languages like C. You can mark certain sections of the code that can be run in parallel using compiler directives and the compiler takes care of it for you.
- Grand central dispatch. Extension to C available in mac os x and ios. Developers mark certain block of code that should be run in parallel and gcd places them in a queue, either serial queue or parallel queue depending on how the block was marked. This is especially nice if you have some for loop that you can parallelize.
4
Q
Describe some issues that are introduced by threads.
A
- Fork. If we fork a process with multiple threads, should only the thread calling fork be copied or all threads in process? POSIX provide a clone method that allow you to specify which resources to copy. This allow you to fork just the thread or all threads. Usually, if you are going to replace the new process with exec, then fork is sufficient. But if you want to actually make a copy, clone trying to copy resources so that the new process duplicate all threads is probably what you want.
- Signal handling. When kernel sends signal to thread, which thread should handle it? Usually, if it’s a synchronous signal sent as a result of some error, the thread that causes the error should be the only one that receive the error and handle it. However, if it’s an asynchronous signal sent as a result of some user action, there are multiple ways the signal can be handled. In POSIX, all threads receive the signal, but some thread can ask to block the signal. The first thread to receive the signal handles it and we don’t care about other threads.
- Thread cancellation. Threads can either be cancelled asynchronously or deferred. Asynchronous cancellation is not recommended since it kill the thread right away without chance for thread to cleanup resources. Deferred cancellation is preferred where thread is marked as should quit and then thread quit itself when appropriate.
- Thread local storage. Most thread libraries allow you to store some global variable that’s specific to just one thread.