Chapter 3: Threads & Concurrency
Presented by: Dr. Ikram Saadaoui
Operating System
Thread Concept
▪ A thread or even a light process is a basic unit of CPU utilization.
▪ They allow a program to do multiple things at once. Think of a thread as
a worker that can handle a specific task.
▪ A thread comprises
- thread ID,
- a program counter,
- a register set,
- and a stack.
▪ It shares with other threads belonging to the same process its
code section, data section, and other operating-system resources,
such as open files.
Operating System 4.5
Single and Multithreaded Processes
▪ A traditional (or a heavy-weight) process has a single thread of control.
▪ Most operating-system kernels are now multithreaded.
• Several threads operate in the kernel, and each thread performs a
specific task, such as managing devices, managing memory, or
interrupt handling.
Operating System 4.6
Concept
• Example 1: Microsoft Word may have
- a thread for displaying graphics,
- another thread for responding to keystrokes from the user,
- and a third thread for performing spelling and grammar checking in the
background.
• Example 2 :Web browser may have
- A thread to download files
- A thread for each web page
- Example3: Web-server : the server will create a separate thread that
listens for client requests. When a request is made, rather than creating
another process, the server creates a new thread to service the request and
resume (continue) listening for additional requests.
Operating System 4.7
Benefits
▪ Responsiveness – may allow continued execution if part of process is
blocked, especially important for user interfaces
▪ Resource Sharing – threads share resources of process, easier than
shared memory or message passing
▪ Economy – cheaper than process creation, thread switching lower
overhead than context switching
▪ Scalability – process can take advantage of multicore architectures
Operating System 4.10
Concurrency vs. Parallelism
Concurrency: Multiple tasks make progress but not necessarily at the
same instant. The OS rapidly switches between tasks (time-sharing),
creating an illusion of simultaneity.
▪ Concurrent execution on single-core system:
Operating System 4.11
Concurrency vs. Parallelism
Parallelism: Multiple tasks execute at the same time on different CPU
cores.
▪ Parallelism on a multi-core system:
Operating System 4.12
Multicore Programming
▪ Multicore or multiprocessor systems put pressure on programmers,
challenges include:
• Dividing activities: The workload must be evenly distributed across
cores to maximize CPU usage.
• Data splitting: Data must be divided so that each core gets an
appropriate portion to process.
• Data dependency: Some tasks depend on previous tasks’ results,
limiting parallel execution.
Operating System 4.13
Multicore Programming
▪ 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
Operating System 4.14
User and Kernel Threads
▪ Support for threads may be provided either at the user level, for
user threads, or by the kernel, for kernel threads.
Operating System 4.15
User Threads
▪ User threads - User threads are managed entirely in user space by a
thread library without direct involvement of the operating system (OS)
kernel.
▪ Key Characteristics:
• Created and scheduled by user-level thread libraries (e.g., POSIX
pthreads, Java threads).
• The kernel is unaware of these threads.
• Faster context switching since no system calls are needed.
• No direct access to hardware or system resources.
Operating System 4.16
Kernel Threads
▪ Kernel threads - are managed directly by the operating system kernel.
▪ The kernel scheduler is responsible for managing multiple kernel
threads and can assign different threads to different CPU cores.
▪ Examples :
• Windows, Linux, Mac OS X, iOS, Android
▪ Key Characteristics:
• Created and scheduled by the OS kernel.
• The kernel is aware of these threads.
• True parallel execution is possible on multi-core systems.
Operating System 4.17
Kernel Threads
▪ Kernel threads are like the administrators of a university who manage
resources (e.g., classrooms, scheduling) and ensure overall functionality
at the system level.
▪ User threads are like professors who teach classes (execute tasks) and
decide how to organize and manage their students (subtasks) within the
resources provided by the administrators.
▪ User Threads are faster, lightweight, and managed in user space, but
they don’t achieve true parallelism (executed on a single core CPU).
▪ Kernel Threads allow true parallel execution and better resource
management, but they are slower due to kernel overhead.
Operating System 4.18
Multithreading Models
▪ Multithreading models define how user threads and kernel threads
interact. There are three main models:
▪ Many-to-One
▪ One-to-One
▪ Many-to-Many
Operating System 4.20
Many-to-One
▪ Many user-level threads are mapped to a single kernel thread
▪ One thread blocking causes all to block
▪ Multiple threads may not run in parallel on multicore system because
only one thread may be in kernel at a time
▪ Few systems currently use this model
▪ Examples:
• Solaris Green Threads
• GNU Portable Threads
Operating System 4.21
One-to-One
▪ Each user-level thread maps to kernel thread
▪ Creating a user-level thread creates a kernel thread
▪ Easy to manage, good performance.
▪ It provides more concurrency than the many-to-one model by
allowing another thread to run when a thread makes a blocking
system call.
▪ Can be resource-intensive as each thread requires a lot of OS
resources.
▪ Examples
• Windows
• Linux
• macOS
Operating System 4.22
Many-to-Many Model
▪ Many user-level threads are mapped to a smaller or equal number of
kernel threads.
▪ Pros:
• Developers can create as many user threads as necessary, and
the corresponding kernel threads can run in parallel on a
multiprocessor.
• when a thread performs a blocking system call, the kernel can
schedule another thread for execution.
▪ Cons: More complex to implement and manage.
▪ Windows with the ThreadFiber package
▪ Otherwise not very common
Operating System 4.23
Two-level Model
▪ Similar to M:M, except that it allows a user thread to be bound to
kernel thread
Operating System 4.24
Thread Libraries
▪ Thread library provides a set of functions (API) that allows programmers to
create and manage threads in an application.
▪ Two primary ways of implementing:
• Library entirely in user space
• Library supported by the OS
Operating System 4.25
Pthreads
▪ Pthreads is a standard set of C library functions for multithreaded
programming
▪ 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 (Specification, not implementation)
▪ Common in UNIX operating systems (Linux & Mac OS X)
Operating System 4.26
Pthreads Example
Operating System 4.27
Pthreads Example (Cont.)
Operating System 4.28
Pthreads Code for Joining 10 Threads
Operating System 4.29
Windows Multithreaded C Program
Operating System 4.30
Windows Multithreaded C Program (Cont.)
Operating System 4.31
End of Chapter 3
Operating System