CS-303 Operating Systems
Dr. Samia Ijaz
Fall - 2024
Department of Computer
Science
HITEC University Taxila
Chapter 4: Threads
Threads
● A thread is a basic unit of CPU utilization
consisting of
● a program counter,
● a stack,
● a set of registers, and
● a thread ID.
Motivation
● Most modern applications are multithreaded
● Threads run within application
● Multiple tasks with the application can be
implemented by separate threads e.g. for
the word processor
● Display graphics
● Responding to keystrokes
● Spell checking
● Process creation is heavy-weight while
thread creation is light-weight
● Can simplify code, increase efficiency
● Kernels are generally multithreaded
Single and Multithreaded Processes
Web Server Example
● A web server accepts client requests for web
pages, images, sound, and so forth.
● A busy web server may have several (perhaps
thousands of) clients concurrently accessing it.
● If the web server ran as a traditional single-
threaded process, it would be able to service
only one client at a time, and a client might
have to wait a very long time for its request to
be serviced.
Web Server Example
● One solution is to have the server run as a
single process that accepts requests. When the
server receives a request, it creates a separate
process to service that request.
● Process creation is time-consuming and
resource intensive
● Use one process that contains multiple threads.
● 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
resource-intensive
Benefits
● Responsiveness – may allow continued
execution if part of the process is blocked
● Resource Sharing – threads share resources
of process,
● Economy – cheaper than process creation,
thread switching lower overhead than context
switching
● Scalability – process can take advantage of
multiprocessor architectures
Multicore Programming
● Multicore or multiprocessor systems putting
pressure on programmers, challenges include:
● Dividing activities
● Balance
● Data splitting
● Data dependency (synchronization)
● Testing and debugging
Multicore Programming
● 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
Concurrency vs. Parallelism
● Concurrent execution on single-core system:
● Parallelism on a multi-core system:
Multicore Programming (Cont.)
● 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
Multicore Programming (Cont.)
User Threads and Kernel Threads
● User threads - management done by user-level
threads library / API
● Kernel is not aware of the existence of these threads.
● It handles them as if they were single-threaded
processes.
● User-level threads are small and much faster than
kernel-level threads.
● Context Switching takes less time
● Examples:
● Java Threads,
● POSIX Threads also called pthreads
User Threads and Kernel Threads
● Kernel threads –implemented by the OS
● Managed by the kernel (created through system calls)
● Context switching takes more time
● Examples – virtually all general purpose operating
systems, including:
● Windows
● Solaris
● Linux
● Mac OS X
User Threads and Kernel Threads
Multithreading Models
● 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
● Few systems currently use
this model
● Only one can access
kernel at a time
● Examples:
● Solaris Green Threads
One-to-One
● Each user-level thread maps to
kernel thread
● Creating a user-level thread creates
a kernel level thread
● More concurrency than many-to-one
● Number of threads per process
sometimes restricted due to
overhead
● Examples
● Windows
● Linux
● Solaris 9 and later
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
● Solaris prior to version 9
● Windows with the
ThreadFiber package
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
• Signal is generated by particular event
• Signal is delivered to a process
• Signal is handled by one of two signal handlers:
• default
• 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
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
Thread Cancellation (Cont.)
• Actual cancellation depends on thread state
• If thread has cancellation disabled, cancellation
remains pending until the thread enables it
• Default type is deferred
• Cancellation only occurs when thread
reaches the cancellation point
• On Linux systems, thread cancellation is
handled through the signals
Threads
Processes
End of Chapter 4