Operating Systems: Threads 2
Operating Systems: Threads 2
Threads 2
Operating Systems
Slide 1 of 22
Threading Issues
fork() & exec() System Calls Thread Cancellation Signal Handling Thread Pools Thread-Specific Data Scheduler Activations
Operating Systems
Slide 2 of 22
Operating Systems
If exec() is going to be called soon, duplicate only calling thread Else duplicate all threads
Operating Systems
Slide 4 of 22
Thread Cancellation
Thread cancellation is the task of terminating a thread before it has completed
E.g., multiple threads searching through DB one thread finds result others canceled User presses stop button while web page is loading (multiple processes do loading)
Operating Systems
Slide 5 of 22
Thread Cancellation
Cancellation of thread may occur in two different scenarios
Asynchronous cancellation: a thread immediately terminates the target thread Deferred cancellation: target thread periodically checks whether it should terminate allows graceful termination
Operating Systems
Slide 6 of 22
Signal Handling
A signal is used to notify a process that an event has occurred (UNIX terminology)
Signal is generated for a particular event Generated signal is delivered to process Process must handle the signal
Operating Systems
Slide 7 of 22
Synchronous Signals
Generated due to the actions of a process (e.g., division by 0, illegal memory access) Delivered to the process that caused the signal to be generated
Operating Systems
Slide 8 of 22
Asynchronous Signals
When generated by an event external to receiving process (e.g., terminating a process with Ctrl+C, timer expiry) Delivered to a process other than the one that caused it to be generated
Operating Systems
Slide 9 of 22
Signal Handlers
Every signal has to be handled by
Operating Systems
Slide 10 of 22
Signal Handlers
Single-threaded processes are easy; signal handling for multi-threaded processes is complicated
Deliver signal to the thread that it applies to Deliver signal to every thread Deliver signal to certain threads Assign a specific thread to receive all signals
Slide 11 of 22
Operating Systems
Signal Handlers
Delivering a signal depends on the type of signal generated
Synchronous signals are sent to the thread that caused them Less clear-cut in case of asynchronous signals
Operating Systems
Slide 12 of 22
Sometimes signal delivered only to accepting threads (or first accepting thread) Standard UNIX function for delivering signal: kill(aid_t aid, int signal) POSIX Pthreads provides: pthread_kill(pthread_t tid, int signal) Emulated by Aynchronous Procedure Calls (APC)
Slide 13 of 22
Operating Systems
User thread can specify a function to be called when it receives notification of a particular event More straightforward: delivered to a particular thread
Operating Systems
Slide 14 of 22
Thread Pools
Creating threads may be lighter than creating processes, but unlimited numbers could exhaust system resources Solution: create a number of threads & put them into a pool sit & wait for work When work available, awaken a thread & let it work
Operating Systems
Thread Pools
Benefits of thread pools
Assigning work to waiting thread is faster than creating a new thread Limit on the total number of threads: can avoid exhausting resources
Operating Systems
Thread-specific data
Source of efficiency
In some circumstances each thread needs its own copy of certain data called thread specific data Most thread libraries provide support
Operating Systems
Slide 17 of 22
Scheduler Activation
Implemented by putting an intermediate data structure between kernel & user threads
Operating Systems
Slide 18 of 22
Scheduler Activation
Operating Systems
Slide 19 of 22
Scheduler Activation
Application can schedule user threads onto LWPs Accomplished using a mechanism called upcalls Upcalls must be handled by an upcall handler in the thread library Upcall handler also run on a virtual processor
Slide 20 of 22
Operating Systems
Scheduler Activation
Kernel makes an upcall Kernel allocates a new virtual processor App runs an upcall handler on this new LWP
Upcall handler schedules another eligible process Kernel again upcalls when the blocking thread becomes available
Slide 21 of 22
Operating Systems
Reading Exercise
Read the section on Operating-System Examples (section 4.5 in 7th edition of the book)
Operating Systems
Slide 22 of 22