Threads Week 3
Threads Week 3
HNDIT
3052
Operating
Systems
Threads
HNDIT 3052 Operating Systems
Threads
Topic: Threads
Subtopics:
▪The Thread Model
▪POSIX Threads
▪Implementation of Thread
HNDIT 3052 Operating Systems
Threads
• What is a Thread?
• Let us take an example of a human body. A human
body has different parts having different
functionalities which are working parallelly (
Eg: Eyes, ears, hands, etc). Similarly in computers, a
single process might have multiple functionalities
running parallelly where each functionality can be
considered as a thread.
HNDIT 3052 Operating Systems
Components of Thread
A thread has the following three components:
1. Program Counter
2. Register Set
3. Stack space
HNDIT 3052 Operating Systems
Process vs Thread
• Process simply means any program in execution
while the thread is a segment of a process. The
main differences between process and thread are
mentioned below:
HNDIT 3052 Operating Systems
HNDIT 3052 Operating Systems
The above diagram shows how the resources are shared in two different
processes vs two threads in a single process.
HNDIT 3052 Operating Systems
Another Different comparison of Thread & Process.
HNDIT 3052 Operating Systems
HNDIT 3052 Operating Systems
Note: In some cases where the thread is processing a bigger workload compared to a
process’s workload then the thread may take more time to terminate. But this is an extremely
rare situation and has fewer chances to occur.
HNDIT 3052 Operating Systems
Types of Thread
• 1. User Level Thread:
• 2. Kernel level Thread:
HNDIT 3052 Operating Systems
User Threads and Kernel Threads
1. User threads - management done by user-level
threads library.
• Three primary thread libraries:
• POSIX Pthreads
• Windows threads
• Java threads
2. Kernel threads - Supported by the Kernel
• Examples – virtually all general purpose operating
systems, including:
• Windows
• Solaris
• Linux
• Tru64 UNIX
• Mac OS X
HNDIT 3052 Operating Systems
The above diagram shows the functioning of user-level threads in user space
and kernel-level threads in kernel space.
HNDIT 3052 Operating Systems
Advantages of Threading:
1. Threads improve the overall performance of a
program.
2. Threads increases the responsiveness of the
program
3. Context Switching time in threads is faster.
4. Threads share the same memory and resources
within a process.
5. Communication is faster in threads.
6. Threads provide concurrency within a process.
7. Enhanced throughput of the system.
8. Since different threads can run parallelly,
threading enables the utilization of the
multiprocessor architecture to a greater extent
and increases efficiency.
HNDIT 3052 Operating Systems
Issues with Threading
There are a number of issues that arise with threading.
Some of them are mentioned below:
• The semantics of fork() and exec() system
calls: The fork() call is used to create a duplicate child
process.
• During a fork() call the issue that arises is whether the
whole process should be duplicated or just the thread
which made the fork() call should be duplicated.
• The exec() call replaces the whole process that called
it including all the threads in the process with a new
program.
HNDIT 3052 Operating Systems
Multithreading Models
• Some operating system provide a combined user
level thread and Kernel level thread facility. Solaris
is a good example of this combined approach. In a
combined system, multiple threads within the same
application can run in parallel on multiple
processors and a blocking system call need not
block the entire process. Multithreading models are
three types.
1. Many to many relationship.
2. Many to one relationship.
3. One to one relationship.
HNDIT 3052 Operating Systems
i. Many to Many Multithreading Model:
• The many-to-many model multiplexes any number
of user threads onto an equal or smaller number of
kernel threads.
HNDIT 3052 Operating Systems
i. Many to Many Multithreading Model...
• The following diagram shows the many-to-many threading model where
6 user level threads are multiplexing with 6 kernel level threads. In this
model, developers can create as many user threads as necessary and
the corresponding Kernel threads can run in parallel on a
multiprocessor machine. This model provides the best accuracy on
concurrency and when a thread performs a blocking system call, the
kernel can schedule another thread for execution.
HNDIT 3052 Operating Systems
i. Many to Many Multithreading 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.
Fiber:
o A fiber is a unit of execution that must be
manually scheduled by the application.
o Fibers run in the context of the threads that
schedule them.
o Each thread can schedule multiple fibers. In
general, fibers do not provide advantages
over a well-designed multithreaded
application.
o However, using fibers can make it easier to
port applications that were designed to
schedule their own threads.
HNDIT 3052 Operating Systems
POSIX Threads in OS :
• The POSIX thread libraries are a C/C++ thread API based
on standards. It enables the creation of a new
concurrent process flow. It works well on multi-
processor or multi-core systems, where the process
flow may be scheduled to execute on another
processor, increasing speed through parallel or
distributed processing. Because the system does not
create a new system, virtual memory space and
environment for the process, threads needless
overhead than “forking” or creating a new process.
While multiprocessor systems are the most effective,
benefits can also be obtained on uniprocessor systems
that leverage delay in I/O and other system processes
that may impede process execution.
#include <pthread.h>
#include <pthread.h>
HNDIT 3052 Operating Systems
#include <pthread.h>
POSIX Threads in OS :
• To utilise the PThread interfaces, we must include the
header pthread.h at the start of the CPP script.
#include <pthread.h>
Implementation of Thread
• There are two ways to implement a thread,
they're either in user space or in the Kernel.
• The corresponding code and the data structures
used are stored in the user space. If an API is
invoked, it results in a local system call in user
space, rather than a system call.
HNDIT 3052 Operating Systems
HNDIT 3052 Operating Systems
Implementation of User-level Threads
Implementation at user-level
– User-level Thread Control Block (TCB), ready
queue, blocked queue, and dispatcher.
– Kernel has no knowledge of the threads (it only
sees a single process).
– If a thread blocks waiting for a resource held by
another thread, its state is save and the
dispatcher switches to another ready thread.
– Thread management (create, exit, yield, wait)
are implemented in a runtime support library.
HNDIT 3052 Operating Systems
Pros - User-level Threads
HNDIT 3052 Operating Systems
Cons - User-level Threads
HNDIT 3052 Operating Systems
Cons - User-level Threads
HNDIT 3052 Operating Systems
HNDIT 3052 Operating Systems
HNDIT 3052 Operating Systems
HNDIT 3052 Operating Systems
HNDIT 3052 Operating Systems
HNDIT 3052 Operating Systems