1
CHAPTER 5: THREADS AND
CONCURRENCY
OPERATING SYSTEMS (CS-2006)
SPRING 2022, FAST NUCES
Motivation
� Most modern applications are multithreaded
� Threads run within application
� Multiple tasks with the application can be implemented by separate
threads
◦ Update display
◦ Fetch data
◦ Spell checking
◦ Answer a network request
� Process creation is heavy-weight while thread creation is light-weight
� Can simplify code, increase efficiency
� Kernels are generally multithreaded
Control Blocks
• Information associated with each process: Process Control Block
1. Memory management information
2. Accounting information
• Information associated with each thread: Thread Control Block
1. Program counter
2. CPU registers
3. CPU scheduling information
4. Pending I/O information
3
Course Supervisor: ANAUM HAMID
Control Blocks
Process ID (PID)
Parent PID
…
Next Process Block PCB
List of open files Handle Table
Image File Name
Thread Control Block (TCB)
List of Thread
Control Blocks Next TCB
Program Counter
… Registers
0
Course Supervisor: ANAUM HAMID
Single & Multithreading
Process
• Each thread has
• An execution state (Running, Ready, etc.)
• Saved thread context when not running
• An execution stack
• Some per-thread static storage for local variables
• Access to the memory and resources of its
process (all threads of a process share
this)
• Suspending a process involves
suspending all threads of the process
• Termination of a process terminates all
threads within the process
6
Course Supervisor: ANAUM HAMID
Threads
Threads specific
Threads share…. Attributes….
� Global memory ● Thread ID
� Process ID and parent ● Thread specific data
process ID ● CPU affinity
● Stack (local variables and function
� Controlling terminal
call linkage information)
� Process credentials (user ) ● ……
� Open file information
� Timers
� ………
Course Supervisor: ANAUM HAMID
Single and Multithreaded
Processes
Process
Vs.
Threads
Course Supervisor: ANAUM HAMID
Multithreaded Server
Architecture
10
� Responsiveness – One thread may provide rapid
response while other threads are blocked or slowed down
Benefits doing intensive calculations.
� Resource Sharing – By default threads share common
code, data, and other resources, which allows multiple
tasks to be performed simultaneously in a single address
space.
� Economy – Creating and managing threads ( and
context switches between them ) is much faster than
performing the same tasks for processes.
� Scalability – Utilization of multiprocessor architectures
- A single threaded process can only run on one CPU, no
matter how many may be available, whereas the
execution of a multi-threaded application may be split
amongst available processors.
Course Supervisor: ANAUM HAMID
Multicore Programming
� A recent trend in computer architecture is to
produce chips with multiple cores, or CPUs on a
single chip.
� Parallelism implies a system can perform more
than one task simultaneously
� Concurrency supports more than one task making
progress
◦ Single processor / core, scheduler providing concurrency
Course Supervisor: ANAUM HAMID 11
Concurrency vs. Parallelism
• Concurrent execution on single-core
system:
• Parallelism on a multi-core system:
Course Supervisor: ANAUM HAMID 12
Thread
Dispatchin
g Thread T1
Interrupt or system call Thread T2
executing
Save state into TCB1 ready or
waiting
Reload state from TCB2
Interrupt or system call
ready or executing
waiting
Save state into TCB2
Reload state from TCB1
ready or
waiting
executing
Course Supervisor: ANAUM HAMID
14
Types of Parallelism
Course Supervisor: ANAUM HAMID
Data Parallelism Task Parallelism
Course Supervisor: ANAUM HAMID
Same operations are performed Different operations are
15 on different subsets of same performed on the same or
data. different data.
Data vs. Synchronous computation Asynchronous computation
Task Speedup is less as each
processor will execute a
Parallelism
Speedup is more as there is different thread or process on
only one execution thread the same or different set of
operating on all sets of data. data.
Amount of parallelization is Amount of parallelization is
proportional to the input data proportional to the number of
size. independent tasks to be
performed
Designed for optimum load Load balancing depends on the
balance on multi processor availability of the hardware and
system. scheduling algorithms like static
and dynamic scheduling.
Course Supervisor: ANAUM HAMID
Amdahl’s Law
� gives the theoretical speedup in latency of the execution of a
task at fixed workload that can be expected of a system
whose resources are improved
Where S = portion of program executed serially
N = Processing Cores
Course Supervisor: ANAUM HAMID
Amdahl’s Law Example
� we have an application that is 75 percent parallel and 25
percent serial. If we run this application on a system with two
processing
cores?
� S=25%=0.25, N= 2
� If we add two additional cores , calculate speedup?
Course Supervisor: ANAUM HAMID
Types of Threads
● Support provided at either
● User level -> user threads
Supported above the kernel and managed without kernel support
● Kernel level -> kernel threads
Supported and managed directly by the operating system
What is the relationship between user and kernel threads?
Course Supervisor: ANAUM HAMID
User and Kernel Threads
User Threads
� Thread management done by user-level threads library
� Three primary thread libraries:
◦ POSIX Pthreads
◦ Win32 threads
◦ Java threads
Course Supervisor: ANAUM HAMID
Kernel Threads
� Supported by the Kernel
� Examples
◦ Windows
◦ Linux
◦ Mac OS X
◦ iOS
◦ Android
Course Supervisor: ANAUM HAMID
Multithreading models
In a specific implementation, the user threads must be mapped
to kernel threads, using these listed below Multithreading
Models:
1. Many-to-One
2. One-to-One
3. Many-to-Many User Thread – to - Kernel Thread
Course Supervisor: ANAUM HAMID 22
Many-to-One
Many user-level threads
mapped to single kernel thread
� Only one thread can access
the kernel at a time,
� multiple threads are unable
to run in parallel on
multicore systems.
� the entire process will block if a
thread makes a blocking system
call
Course Supervisor: ANAUM HAMID 23
One-to-One
Each user-level thread maps to kernel thread
� more concurrency than the many-to-one model by allowing another thread to run when
a thread makes a blocking system call.
� Allows multiple threads to run in parallel on multiprocessors.
� drawback is, creating a user thread requires creating the corresponding kernel thread
Course Supervisor: ANAUM HAMID 24
Many-to-Many Model
� multiplexes many user-level
threads to a smaller or equal
number of kernel threads
� developers can create as many
user threads as necessary, and
the corresponding
� kernel threads can run in parallel
on a multiprocessor.
� When thread performs a blocking
system call, the kernel can
schedule another thread for
execution.
Course Supervisor: ANAUM HAMID 25
Thread Libraries
� Three main thread libraries in use today:
◦ POSIX Pthreads
● May be provided either as user-level or kernel-level
● A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization
● API specifies behavior of the thread library, implementation is up to development of the
library
◦ Win32
● Kernel-level library on Windows system
◦ Java
● Java threads are managed by the JVM
● Typically implemented using the threads model provided by underlying OS
Course Supervisor: ANAUM HAMID
Pthreads Example
Pthreads Example (Cont.)
Pthreads Code for Joining 10 Threads
Windows Multithreaded C
Program
Windows Multithreaded C Program (Cont.)
Java Threads
� Java threads are managed by the JVM
� Typically implemented using the threads model
provided by underlying OS
� Java threads may be created by:
◦ Extending Thread class
◦ Implementing the Runnable interface
◦ Standard practice is to implement Runnable interface
Java Threads
Implementing Runnable interface:
Creating a thread:
Waiting on a thread:
Implicit Threading
� Creation and management of threads done by
compilers and run-time libraries rather than
programmers
� These listed below methods explored
1. Thread Pools
2. Fork Join
3. OpenMP
4. Grand Central Dispatch.
5. Intel Threading Building Blocks
Course Supervisor: ANAUM HAMID 34
Thread Pools
� Create a number of threads in a pool
where they await work
� Advantages:
◦ Usually slightly faster to service a request with
an existing thread than create a new thread
◦ Allows the number of threads in the
application(s) to be bound to the size of the
pool
Fork-Join Parallelism
� Multiple threads (tasks) are forked, and then joined.
Course Supervisor: ANAUM HAMID 36
OpenMP
• An Application Program Interface (API) that may be used to
explicitly direct multithreaded, shared memory parallelism
• Three main API components
– Compiler directives
– Runtime library routines
– Environment variables
• Portable & Standardized
– API exist both C/C++ and Fortan 90/77
– Multi platform Support (Unix, Linux etc.)
Course Supervisor: ANAUM HAMID
Open MP Example
Course Supervisor: ANAUM HAMID
Threading Issues
1. Semantics of fork() and exec() system calls
2. Signal handling
3. Thread cancellation of target thread
4. Thread-local storage
5. Scheduler Activations
Semantics of fork() and exec()
� Does fork()duplicate only the calling thread
or all threads?
◦ Some UNIXes have two versions of fork
� exec() usually works as normal – replace the
running process including all threads
Thread Cancellation
� Terminating a thread before it has finished
� Thread to be canceled is target thread
� Two general approaches:
◦ Asynchronous cancellation terminates the target thread immediately
◦ Deferred cancellation allows the target thread to periodically check if it
should be cancelled
◦ Cancellation only occurs when thread reaches cancellation point
● i.e., pthread_testcancel()
● Then cleanup handler is invoked
Pthread code to create and cancel a thread
Course Supervisor: ANAUM HAMID 42
Thread-Local Storage
� Thread-local storage (TLS) allows
each thread to have its own copy of
data.
� major thread libraries ( pThreads, Win32,
Java ) provide support for thread-specific
data, known as thread-local
storage or TLS.
Course Supervisor: ANAUM HAMID Course Supervisor: ANAUM HAMID 43
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
� Where should a signal be delivered for multi-threaded?
1. Deliver the signal to the thread to which the signal applies
2. Deliver the signal to every thread in the process
3. Deliver the signal to certain threads in the process
4. Assign a specific thread to receive all signals for the process
Course Supervisor: ANAUM HAMID 44
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
Kernel Scheduling
Course Supervisor: ANAUM HAMID
THANK YOU
Course Supervisor: ANAUM HAMID 47