Unit-2 Chapter 1 Multithreaded Programming
Unit-2 Chapter 1 Multithreaded Programming
Chapter-1
Multithreaded Programming.
• A thread refers to a single sequential flow of activities being executed in
a process; it is also known as the thread of execution or the thread of
control.
• There can be multiple threads in a single process.
• A thread has three components namely Program counter, register set, and
stack space.
• Thread is also termed as a lightweight process as they share resources and
are faster compared to processes. Threads are not independent of one
another like processes are, as they share the code section, data section,
OS resources etc. But like processes a thread has its own program
counter(PC),register set and stack space.
• Context switching is faster in threads.
•
Advantages of Threading
•
Types of Thread
User-level threads are implemented and managed by the user and the
kernel is not aware of it. Is implemented in the user level library, they are
not created using the system calls. The kernel-level thread manages user-
level threads as if they are single-threaded processes?
Advantages:
Simple representation since thread has only program counter, register set,
stack space.
Kernel level threads are implemented and managed by the OS. Kernel knows
and manages the threads. Instead of thread table in each process, the kernel
itself has thread table (a master one) that keeps track of all the threads in the
system. There is a thread control block and process control block in the system
for each thread and process in the kernel-level thread.
The kernel-level thread offers a system call to create and manage the threads
from user-space. The implementation of kernel threads is more difficult than the
user thread. Context switch time is longer in the kernel thread. If a kernel
thread performs a blocking operation, the other thread execution can continue.
• Kernel level threads are implemented using system calls and Kernel level
threads are recognized by the OS.
• Kernel-level threads are slower to create and manage compared to user-
level threads.
• Context switching in a kernel-level thread is slower.
• Even if one kernel-level thread performs a blocking operation, it does not
affect other threads. Eg: Window Solaris.
Multithreading:
Multiple threads run behind the scenes in most of the applications you use
regularly. At any given time, you may have numerous tabs open in the system
and every tab displaying different types of content. Many threads of execution
are used to display animations, load content, play a video, etc.
Advantages of Multithreading:
In a non multi threaded environment, a server listens to the port for some
request and when the request comes, it processes the request and then resume
listening to another request. The time taken while processing of request makes
other users wait unnecessarily. Instead a better approach would be to pass the
request to a worker thread and continue listening to port.
• Message Passing
• Shared Memory
threads share the memory and the resources of the process to which they belong
by default. The benefit of sharing code and data is that it allows an application
to have several threads of activity within same address space.
A single-threaded task could only run on one of them, no matter how many
CPUs are available. On a multi-CPU machine, multithreading enhances
concurrency. The CPU switches among threads so quickly in single-processor
architecture that it creates the illusion of parallelism, but only one thread is
running at a particular time.
The many to one model maps many user levels threads to one kernel
thread. Thread management is done by the kernel library in user space, so it is
efficient. The entire process will block if a thread makes a blocking system call.
Also, because only one thread can access the kernel at a time, multiple threads
are unable to run in parallel on multicore systems.
In the above figure, the many to one model associates all user-level threads to
single kernel-level threads.
The many to many model multiplexes many user level threads to a smaller or
equal number of Kernel threads. The number of Kernel threads may be specific
to either a particular application or a particular machine. In this type of model,
there are several user-level threads and several kernel-level threads. The number
of kernel threads created depends upon a particular application. The developer
can create as many threads at both levels but may not be the same. In this
model, if any thread makes a blocking system call, the kernel can schedule
another thread for execution. Though this model allows the creation of multiple
kernel threads, true concurrency cannot be achieved by this model. This is
because the kernel can schedule only one process at a time.
Many to many versions of the multithreading model associate several user-level
threads to the same or much less variety of kernel-level threads in the above
figure.
Threading issue:
There are a number of issues that arise with threading. Some of them are
mentioned below:
A process can use fork() system call to create a new process. calling process is
called parent process. New process is called child process. child is the exact
memory image of the parent process.
(The fork() system call is used to make a new one process that is an properly
copy (duplicate) of the calling process that is known as the parent process. After
the fork() call, two processes are made: the parent process and the newly made
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.
Either the fork() can duplicate all the threads of the parent process in the child
process or the fork() would only duplicate that thread from parent process that
has invoked it.
Which version of fork() must be used totally depends upon the application.
Next system call i.e. exec() system call when invoked replaces the program
along with all its threads with the program that is specified in the parameter to
exec(). Typically the exec() system call is lined up after the fork() system call.
The exec() call replaces the whole process that called it including all the threads
in the process with a new program.
1) If program uses exec() system call after fork() then only calling thread is
copied.
2) Thread cancellation:
The process of prematurely aborting an active thread during its run is called
‘thread cancellation’.
The target thread is now the thread that we want to cancel. Thread cancellation
can be done in two ways:
Suppose the target thread exits when updating the information that is being
shared with other threads.
On the other hand, in synchronous thread cancellation the target thread receives
this message first and then checks its flag to see if it should cancel itself now or
later. They are called the Cancellation Points of threads under which thread
cancellation occurs safely.
Every signal has a default signal handler that the kernel runs when handling that
signal.
Threads belonging to a process share the data of the process. this data sharing
provides one of the benefits of multithreaded programming. However, in some
circumstances, each thread might need its own copy of certain data. We will call
such data thread-local storage (or TLS.) While thread-specific data avoids data
interference between threads, it also hinders direct communication between
threads using shared data.
Once the task is completed, then a thread returns to the pool, and getting to
become available for the further task. This mechanism reduces the overhead
of creating and destroying threads, resulting in improved performance and
reduced resource consumption.
S.
No. Parameters User Level Thread Kernel Level Thread
Implementation of
Implementation of User
3. Implementation Kernel-Level thread is
threads is easy.
complicated.