Lecture 5 Flashcards
Many-to-many
- Mapping is not fixed, need a thread scheduler
- When a user thread blocks, kernel notifies thread scheduler (upcall) to select another thread to use a free kernel thread
Pros and cons of many-to-many
- Pro: Increased concurrency; flexible (users may create as many user threads as they want)
- Con: Very complex to implement
POSIX Pthreads
- API specification, implementation is up to OS
- Common in UNIX systems
Windows Threads
- Implementation of the one-to-one model in kernel
Java Threads
- Java threads are managed by JVM, which is run on top of OS
- JVM specifies the interface, not the implementation
- JVM uses thread services provided by the host OS
Threading Issues:
- Semantics of fork() and exec() system calls
- Signal handling
- Thread pools
If a process has multiple threads and one of them calls fork(), should the entire process be duplicated or just the calling thread?
It depends. If exec() is called immediately after fork(), there’s no need to duplicate all threads as they will be overwritten anyway. Otherwise, all threads should be duplicated.
Signals are used in UNIX to…
…notify a process that an event has occured
Two types of signal:
- Synchronous (delivered to the same process that performed the operation that caused the signal)
- Asynchronous (Signal generated by an external event, usually delivered to a different process)
Signal Sequence
- Signal is generated by an event (eg. Ctrl-C pressed or packet arrived)
- Signal is delivered to a process
- Signal handler processes the signal (default by OS, can be user-defined)
Should OS deliver a signal to: all threads, one thread, or specific thread of a process?
- Synchronous: deliver signal to thread
- Asynchronous: deliver signal to all threads belonging to the process
True or false: Threads cannot block signals
False
Interrupts vs Signals
- Interrupts started by CPU, I/O, or software; signal started by kernel or process
- Interrupts handled by ISR in kernel, signals handled by default signal handler in kernel, or by process that overrides it
- ISR must be simple, signal handler can be complex and call other functions in the kernel
- Kernel may map specific interrupts to signals and deliver them to processes for specific handling
Thread Pools
Create a set of threads that wait for work
Advantages of Thread Pools
- Faster to service a request with existing thread than to create a new one
- Limit number of threads in an application to pool size (further requests are queued until a thread is free)
- Easy to manage resources