4 Threads
4 Threads
Threads
A thread shares the following with its parent process and siblings:
• Code section
• Data section
• Memory address space/ global memory
• I/O devices
• Other OS resources such as open files and signals
Single and Multithreaded Processes
Examples of Multithreaded Applications
A web browser might have one thread display images or text while
another thread retrieves data from the network.
Operations on Threads
• Thread operations associated with a change in thread state are
Spawn
Block
Unblock
Finish
Thread Synchronization
• It is necessary to synchronize the activities of the various threads
All threads of a process share the same address space and
other resources
Any alteration of a resource by one thread affects the other
threads in the same process
Types of Threads
Advantages of ULTs
• Thread switching does not require kernel mode privileges (no
mode switches)
• Scheduling can be application specific
• ULTs can run on any OS
Disadvantages of ULTs
• In a typical OS many system calls are blocking in nature
As a result, when a ULT executes a system call, not only is that
thread blocked, but all of the threads within the process are
blocked
• The kernel can only assign processes to processors.
Overcoming ULT Disadvantages
Jacketing
• Converts a blocking system call into a non-blocking system call
Kernel-Level Threads (KLTs)
Advantages of KLTs
• The kernel can simultaneously schedule multiple threads from the
same process on multiple processors.
• If one thread in a process is blocked, the kernel can schedule
another thread of the same process.
Disadvantages of KLTs
• The transfer of control from one thread to another within the same
process requires a mode switch to the kernel.
Combined Approaches
Many-to-One
One-to-One
Many-to-Many
Many-to-One
Types of parallelism
• Data parallelism – distributes subsets of the same data
across multiple cores, same operation on each
• Task parallelism – distributing threads across cores, each
thread performing unique operation
Data and Task Parallelism
Thread Libraries
Thread-local storage (TLS) allows each thread to have its own copy of
data
Useful when you do not have control over the thread creation process
(i.e., when using a thread pool)
Different from local variables
• Local variables visible only during single function invocation
• TLS visible across function invocations
Similar to static data
• TLS is unique to each thread
Scheduler Activations
Both M:M and Two-level models require
communication to maintain the appropriate
number of kernel threads allocated to the
application
Typically use an intermediate data structure
between user and kernel threads – lightweight
process (LWP)
• Appears to be a virtual processor on which
process can schedule user thread to run
• Each LWP attached to kernel thread
• How many LWPs to create?
Scheduler activations provide upcalls - a
communication mechanism from the kernel to
the upcall handler in the thread library
This communication allows an application to
maintain the correct number kernel threads
References