Ch-4 - Threads and Concurency
Ch-4 - Threads and Concurency
CS3500
PROF. SUKHENDU DAS;
DEPTT. OF COMPUTER SCIENCE AND ENGG., IIT MADRAS,
CHENNAI – 600036.
Email: [email protected]
URL: http://www.cse.iitm.ac.in/~vplab/os.html
Aug. – 2022.
THREADS & CONCURRENCY
OUTLINE
• Overview
• Multicore Programming
• Multithreading Models
• Thread Libraries
• Threading Issues
MOTIVATION
• Types of parallelism
• Many-to-One
• One-to-One
• Many-to-Many
MANY-TO-ONE
• Many user-level threads mapped to single kernel thread
• One thread blocking causes all to block
• Multiple threads may not run in parallel on multicore system because only one
may be in kernel at a time
• Few systems currently use this model
• Examples:
• Solaris Green Threads
• GNU Portable Threads
ONE-TO-ONE
• Each user-level thread maps to kernel thread
• Creating a user-level thread creates a kernel thread
• More concurrency than many-to-one
• Number of threads per process sometimes restricted due to
overhead
• Examples
• Windows
• Linux
MANY-TO-MANY MODEL
• Allows many user level threads to be mapped to many kernel threads
• Allows the operating system to create a sufficient number of kernel
threads
• Windows with the ThreadFiber package
• Otherwise not very common
TWO-LEVEL MODEL
• Similar to M:M, except that it allows a user thread to be bound to
kernel thread
THREAD LIBRARIES
• Thread library provides programmer with API for
creating and managing threads
• Two primary ways of implementing
• Library entirely in user space
• Kernel-level library supported by the OS
PTHREADS
• May be provided either as user-level or kernel-level
• A POSIX standard (IEEE 1003.1c) API for thread creation and
synchronization
• Specification, not implementation
• API specifies behavior of the thread library, implementation is up to
development of the library
• Common in UNIX operating systems (Linux & Mac OS X)
PTHREADS EXAMPLE
PTHREADS EXAMPLE (CONT.)
Try Pthreads Code for Joining 10 Threads!! (/** See fig. 4.12)
All Pthreads programs must include the pthread.h header file. The
statement pthread_t tid declares the identifier for the thread we will create.
Each thread has a set of attributes, including stack size and scheduling
information. The pthread attr_t attr declaration represents the attributes for
the thread.
If, however, the separate process does not call exec() after forking, the separate
process should duplicate all threads.
SIGNAL HANDLING
• Signals are used in UNIX systems to notify a process that a particular event
has occurred.
• A signal handler is used to process signals
1. Signal is generated by particular event
2. Signal is delivered to a process
3. Signal is handled by one of two signal handlers:
1. default
2. user-defined
• Every signal has default handler that kernel runs when handling signal
• User-defined signal handler can override default
• For single-threaded, signal delivered to process
SIGNAL HANDLING (CONT.)
• Where should a signal be delivered for multi-threaded?
• 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
Synchronous signals are delivered to the same process that performed the
operation that caused the signal;
When a signal is generated by an event external to a running process, that
process receives the signal asynchronously.
• If thread has cancellation disabled, cancellation remains pending until thread enables it
• Default type is deferred
• Cancellation only occurs when thread reaches cancellation point
• i.e., pthread_testcancel()
• Then cleanup handler is invoked
• On Linux systems, thread cancellation is handled through signals
THREAD-LOCAL STORAGE
• 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
PCB VS TCB
PCB vs TCB
S.No PCB TCB
2 The PCB stores information about the kernel The TCB includes thread specific information.
process.
A process can include different kernel threads
3 Some notable fields that the PCB could contain are TCB has a few of the same fields as the PCB
the process id, process group id, the parent process (register values, stack pointer, program counter,
and child processes, the heap pointer, program scheduling state), in addition to a few specific
counter, scheduling state (running, ready, blocked), values like the
permissions (what system resources the process is .
allowed to access), content of the general purpose
registers, and open files.
4 PCB describes an environment context (eg. memory TCB describes an execution context, (eg. stack
segments and permissions) pointer)