[go: up one dir, main page]

0% found this document useful (0 votes)
15 views7 pages

Notes On Threads and Multithreading

Uploaded by

pmahakalkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views7 pages

Notes On Threads and Multithreading

Uploaded by

pmahakalkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Notes on Threads and Multithreading

Need for Threads: Threads are lightweight units of execution within a process, enabling
concurrent execution of tasks within a single program. They address the limitations of single-
threaded processes by allowing responsiveness, resource sharing, economy in creation and
context switching, and scalability on multi-core systems.

Advantages of Threads:

 Responsiveness: A program can remain responsive even if part of it is blocked or


performing a long operation.

 Resource Sharing: Threads within a process share memory and resources, reducing
overhead.

 Economy: Cheaper to create and manage than processes.

 Scalability: Can utilize multiple CPU cores for parallel execution.

 Improved Throughput: More tasks can be completed in a given time.

Limitations of Threads:

 Complexity: Multithreaded programming can be more complex due to


synchronization issues (e.g., race conditions, deadlocks).

 Debugging: Debugging multithreaded applications can be challenging.

 Overhead: While lighter than processes, there is still overhead associated with
thread creation and context switching.
Types of threads

There are two types of threads,

1. User-level threads

2. Kernel-level threads

User-level threads: User-level threads are supported above the kernel without the kernel
support.

Kernel-level threads: Kernel-level threads are supported and managed by the operating
system.
Multithread Models
• Operating system uses user level thread and kernel level thread. User level
threads are managed without kernel support. Operating system support and
manage the kernel level threads.
• Modern operating system support kernel level threads. Kernel performs
multiple simultaneous tasks in operating system. In most of the application, user
threads are mapped with kernel level threads.
• Different methods of mapping is used in operating system are as follows:
1. One to one
2. Many to one
3. Many to many
1. One to one
• In this model, one user level thread maps with one kernel level thread. If there
are three user threads then system creates three separate kernel thread. This
model provides more concurrency because of separate thread. Fig. 2.9.1 shows
one to one mapping.
• In this method, the operating system allocates data structure that represents
kernel threads. Here multiple threads are run in parallel on multiprocessor
system.
• Number of threads in the system increases, the amount of memory required is
also increases.
• Windows 95/XP and Linux operating system uses this one to one thread
mapping.
• Only overhead in this method is creation of kernel level thread for user level
thread. Because of this overhead, system performance is slowdown.
2. Many to one
• Many to one mapping means many user thread maps with one kernel thread.
Fig. 2.9.2 shows many to one mapping.
• Operating system blocks the entire multi-threaded process when a single
thread blocks because the entire multi-threaded process is a single thread of
control. So when operating system re receive any a blocking I/O request, it
blocks the entire
• Thread library handle thread management in user space. The many-to-one
model does not allow individual processes to be split across multiple CPUs.
•This type of relationship provides an effective context-switching environment,
easily implementable even on simple kernels with no thread support.
• Green threads for Solaris and GNU portable threads implement the many-to-
one model in the past, but few systems continue to do so today.
• Advantage: System performance is improved by customizing the
thread algorithm.
3. Many to many
• In the many to many mapping, many user-level threads maps with equal
number of kernel threads. Fig. 2.9.3 shows many to many thread mapping.
• Many to many mapping is also called M-to-N. thread mapping. Thread
pooling is used to implement this method.
• Users can create required number of thread and there is no limitation for user.
Again here blocking Kernel system calls do not block the entire process.
• This model support for splitting processes across multiple processors.
Limitations: Operating system design becomes complicated.
Example 2.9.1: Provide two programming examples in which multithreading
does not provide better performance than a single-threaded solution.
Solution:
a. A Web server that services each request in a separate thread.
b. A parallelized application such as matrix multiplication where different parts
of the matrix may be worked on in parallel.
C. An interactive GUI program such as a debugger where a thread is used to
monitor user input, another thread represents the running application, and a
third thread monitors performance.

University Question
1. Explain with a neat diagram about various multi threading
models. AU May-22, Marks 6

You might also like