Operating System Chapter 3 [ Multithreaded Programming]
Introduction to Threads:
Threads: A Thread is a light weighted, smallest sequence of programmed instructions that can be
manage independently by a scheduler which is typically the part of the OS. Thread are created by
some process.
Why Threads? A process with multiple threads make a great server. Threads uses very little
resources of an OS in which they are working.
Within a program, a Thread is a separate execution path. It is a lightweight process that the
operating system can schedule and run concurrently with other threads.
There is a way of thread execution inside the process of any operating system. Apart from this,
there can be more than one thread inside a process. Each thread of the same process makes use of
a separate program counter and a stack of activation records and control blocks.
The process can be split down into so many threads. For example, in a browser, many tabs
can be viewed as threads. MS Word uses many threads - formatting text from one thread,
processing input from another thread, etc.
Need of Thread:
It takes far less time to create a new thread in an existing process than to create a new
process.
Threads can share the common data, they do not need to use Inter- Process communication.
Context switching is faster when working with threads.
It takes less time to terminate a thread than a process.
ANURADHA K N , DEPT OF BCA , SDC,
1
TUMKUR
Operating System Chapter 3 [ Multithreaded Programming]
Threads are executed one after another but gives the illusion as if they are executing in parallel.
Each thread has different states.
Each thread has 3 components,
A program counter
A register set
A stack space
Types of Threads:
1. User Level Thread.
2. Kernel Level Thread.
1.User Level thread (ULT) – It is implemented in the user level library; they are not created
using the system calls. Thread switching does not need to call OS and to cause interrupt to Kernel.
Kernel doesn’t know about the user level thread and manages them as if they were single-threaded
processes.
Advantages of ULT –
Can be implemented on an OS that doesn’t support multithreading.
Simple representation since thread has only program counter, register set, stack space.
Simple to create since no intervention of kernel.
Thread switching is fast since no OS calls need to be made.
Limitations of ULT –
No or less co-ordination among the threads and Kernel.
If one thread causes a page fault, the entire process blocks.
2.Kernel Level Thread (KLT) – 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. In addition kernel also maintains the traditional process table to keep track of the
processes. OS kernel provides system call to create and manage threads.
Advantages of KLT –
Since kernel has full knowledge about the threads in the system, scheduler may decide to
give more time to processes having large number of threads.
Good for applications that frequently block.
ANURADHA K N , DEPT OF BCA , SDC,
2
TUMKUR
Operating System Chapter 3 [ Multithreaded Programming]
Limitations of KLT –
Slow and inefficient.
It requires thread control block so it is an overhead.
ANURADHA K N , DEPT OF BCA , SDC,
3
TUMKUR
Operating System Chapter 3 [ Multithreaded Programming]
Benefits of Threads
Enhanced throughput of the system: When the process is split into many threads, and each
thread is treated as a job, the number of jobs done in the unit time increases. That is why the
throughput of the system also increases.
Effective Utilization of Multiprocessor system: When you have more than one thread in one
process, you can schedule more than one thread in more than one processor.
Faster context switch: The context switching period between threads is less than the process
context switching. The process context switch means more overhead for the CPU.
Responsiveness: When the process is split into several threads, and when a thread completes its
execution, that process can be responded to as soon as possible.
Communication: Multiple-thread communication is simple because the threads share the same
address space, while in process, we adopt just a few exclusive communication strategies for
communication between two processes.
Resource sharing: Resources can be shared between all threads within a process, such as code,
data, and files. Note: The stack and register cannot be shared between threads. There is a stack and
register for each thread.
What is multithreading?
Multithreading is the ability of a program or an operating system to enable more than one user at
a time without requiring multiple copies of the program running on the computer. Multithreading
can also handle multiple requests from the same user. Multithreading is a function of the CPU that
permits multiple threads to run independently while sharing the same process resources.
Multithreading allows the execution of multiple parts of a program at the same time.
Multithreading Models
The user threads must be mapped to kernel threads, by one of the following strategies:
Many to One Model
One to One Model
Many to Many Model
ANURADHA K N , DEPT OF BCA , SDC,
4
TUMKUR
Operating System Chapter 3 [ Multithreaded Programming]
Many to One Model
In the many to one model, many user-level threads are all mapped onto a single kernel thread.
Thread management is handled by the thread library in user space, which is efficient in nature.
In this case, if user-level thread libraries are implemented in the operating system in some way
that the system does not support them, then the Kernel threads use this many-to-one relationship
model.
Many-to-One One-to-one Many-to-many
One to One Model
The one to one model creates a separate kernel thread to handle each and every user thread.
Most implementations of this model place a limit on how many threads can be created.
Linux and Windows from 95 to XP implement the one-to-one model for threads.
This model provides more concurrency than that of many to one Model.
Many to Many Model
The many to many model multiplexes any number of user threads onto an equal or smaller number
of kernel threads, combining the best features of the one-to-one and many-to-one models.
Users can create any number of threads.
Blocking the kernel system calls does not block the entire process.
Processes can be split across multiple processors.
ANURADHA K N , DEPT OF BCA , SDC,
5
TUMKUR
Operating System Chapter 3 [ Multithreaded Programming]
Advantages Of Multithreading
1. Responsiveness
Multithreading in an interactive application enables a program to continue running even if a section
is blocked or executing a lengthy process, increasing user responsiveness.
For instance, a multithreaded web browser permits the user interaction in one thread while a video
is loading in another thread. As a result, instead of waiting for the entire web page to load, the user
can continue viewing a section of a web page.
2. Resource Sharing
Processes can only share the resources only via two techniques such as:
Message Passing
Shared Memory
The programmer must explicitly structure such strategies. On the other hand, by default, threads
share the memory and resources of the process they belong to.
The advantage of sharing code and data is that it permits an app to execute multiple code threads
in the same address space.
3. Economy
Allocating memory and resources for process creation is an expensive procedure because it is a
time and space-consuming task.
4. Scalability
The benefits of multi-programming greatly increase in case of multiprocessor architecture, where
threads may be running parallel on multiple processors. If there is only one thread then it is not
possible to divide the processes into smaller tasks that different processors can perform.
5. Better Communication
Thread synchronization functions could be used to improve inter-process communication.
Moreover, sharing huge amounts of data across multiple threads of execution inside the same
address space provides extremely high-bandwidth, low-latency communication across various
tasks within an application.
ANURADHA K N , DEPT OF BCA , SDC,
6
TUMKUR
Operating System Chapter 3 [ Multithreaded Programming]
6. Utilization of multiprocessor architecture
The advantages of multithreading might be considerably amplified in a multiprocessor
architecture, where every thread could execute in parallel on a distinct processor.
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.
7. Minimized system resource usage
Threads have a minimal influence on the system's resources. The overhead of creating,
maintaining, and managing threads is lower than a general process.
Thread Library
A thread library provides the programmer with an Application program interface for creating and
managing thread.
The main thread libraries which are used are given below −
POSIX (Portable Operating System Interface) threads − Pthreads, the threads extension of the
POSIX standard, may be provided as either a user level or a kernel level library.
WIN 32 thread − The windows thread library is a kernel level library available on windows
systems.
JAVA thread − The JAVA thread API allows threads to be created and managed directly as JAVA
programs.
ANURADHA K N , DEPT OF BCA , SDC,
7
TUMKUR
Operating System Chapter 3 [ Multithreaded Programming]
Threading Issues:
The fork() and exec() system calls
The fork() is used to create a duplicate process. The meaning of the fork() and exec() system calls
change in a multithreaded program.
Signal Handling
Signal is used in Operating systems to notify a process that a particular event has occurred.
All signals, whether synchronous or asynchronous, follow the same pattern as given below −
A signal is generated by the occurrence of a particular event.
The signal is delivered to a process.
Once delivered, the signal must be handled.
Cancellation
Thread cancellation is the task of terminating a thread before it has completed.
For example − If multiple database threads are concurrently searching through a database and one
thread returns the result the remaining threads might be cancelled.
A target thread is a thread that is to be cancelled, cancellation of target thread may occur in two
different scenarios −
Asynchronous cancellation − One thread immediately terminates the target thread.
Deferred cancellation − The target thread periodically checks whether it should terminate,
allowing it an opportunity to terminate itself in an ordinary fashion.
Thread polls
whenever the server receives a request it creates a separate thread to service the request.
Some of the problems that arise in creating a thread are as follows −
The amount of time required to create the thread prior to serving the request
If all concurrent requests are allowed to be serviced in a new thread, there is no bound on
the number of threads concurrently active in the system.
Unlimited thread could exhaust system resources like CPU time or memory.
A thread pool is used to create a number of threads at process start-up and place them into a pool,
where they sit and wait for work.
ANURADHA K N , DEPT OF BCA , SDC,
8
TUMKUR