Lecture06 Threads
Lecture06 Threads
Blocked 3. Ready
4.
Event-occurred
• Problem
– Creating a user-level thread requires creation of corresponding
kernel thread
– Kernel may restrict the number of threads created
• Example systems
– Windows, Linux, Solaris 9 (and later) implement the one-to-one
model
Many-to-Many Model
• Many user-level threads are multiplexed (mapped
dynamically) to a smaller or equal number of
kernel threads
– No fixed binding between a user and a kernel
thread
• The number of kernel threads is specific to a
particular application or computer system
– Application may be allocated more kernel threads
on a multiprocessor architecture as on a single-
processor architecture
• No restriction on user-level threads
– Applications can be designed with as many user-
level threads as needed
– Threads are then mapped dynamically onto a
smaller set of currently available kernel threads for
execution
Two-level Model
• Is a variant of the Many-to-Many model, allows a fixed
relationship between a user thread and a kernel thread
• Was used in older Unix-like systems
– IRIX, HP-UX, True64 Unix, Solaris 8
Threading Issues – fork() and exec()
• Semantics of fork() and exec() changes in a
multithreaded program
– Remember:
• fork() creates an identical copy of the calling process
– In case of a multithreaded program
• Should the new process duplicate all threads?
• Or should the new process be created with only one thread?
– If after fork(), the new process calls exec() to start a new program
within the created process image, only one thread may be
sufficient
– Solution: some Unix systems implement two versions
of fork()
Threading Issues – Thread cancellation
• Terminating a thread before it has finished
– this may depend on application
• E.g.: Web browser uses threads to load web page, but when user
selects a link to go to another web page, the loading of the current
web page can be aborted
• Two approaches
– Asynchronous cancellation terminates the target thread
immediately
– Deferred cancellation
• A cancellation request is sent to a target thread, but it continues to
execute undeterred up to a so-called cancellation point
– Particular areas in its program code where it checks for pending cancellation
requests and where a cancellation could take place safely
• If the target thread detects a cancellation request at a cancellation
point, it will end its execution, otherwise it will continue its execution
Threading Issues – Signal Handling
• Signals are used in Unix systems to notify a process
about the occurrence of an event
• All signals follow the same pattern
1. Signal is generated by particular event
2. Signal is delivered to a process
3. Once delivered, a signal must be handled
• In multithreaded systems, there are 4 options
– Deliver the signal to the thread to which the signal
applies
– Deliver the signal to every thread in the process
– Deliver the signal to certain threads in the process
– Assign a specific thread to receive all signals for the
process
Threading Issues – Thread Pools
• Threads come with some overhead
• Unlimited thread creation may exhaust memory and CPU
• Solution
– Thread pool: create a number of threads at process startup and put
them in a pool, from where they will be allocated
– When an application needs to spawn a thread, an allocated thread is
taken from the pool and adapted to the application’s needs
• Advantage
– Usually faster to service a request with allready instantiated thread
then creating a new one
– Allows number of threads in applications to be bound by thread pool
size
• Number of pre-allocated threads in pool may depend on
– Number of CPUs, memory size
– Expected number of concurrent requests