0% found this document useful (0 votes)
4 views19 pages

Chapter 04 - Threads

Chapter 4 discusses the concept of threads in computer science, highlighting their role as lightweight processes within a heavier process. It covers types of threads, including user and kernel threads, as well as multithreading models such as many-to-one, one-to-one, and many-to-many. Additionally, it addresses threading issues like thread cancellation, signal handling, and the use of thread pools for efficient resource management.

Uploaded by

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

Chapter 04 - Threads

Chapter 4 discusses the concept of threads in computer science, highlighting their role as lightweight processes within a heavier process. It covers types of threads, including user and kernel threads, as well as multithreading models such as many-to-one, one-to-one, and many-to-many. Additionally, it addresses threading issues like thread cancellation, signal handling, and the use of thread pools for efficient resource management.

Uploaded by

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

Chapter 4

Threads

Dr. Niroj Kumar Pani


nirojpani@gmail.com

Department of Computer Science Engineering & Applications


Indira Gandhi Institute of Technology
Sarang, Odisha
Chapter Outline…
◼ Threading Concept

◼ Types of Threads

◼ Multithreading Models

◼ Threading Issues

Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 4.2


Threading Concept
What are Threads?

◼ A thread, sometimes called a Lightweight Process (LWP), is a basic unit of CPU


utilization.

◼ A Process (Heavy Weight) may include multiple threads.

◼ A Thread comprises of:


➢ A thread ID
➢ A Program Counter
➢ A Register Set
➢ A Stack

◼ A thread shares with other threads belonging to the same process its code
section, data section, and other OS resources such as an open file etc.

Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 4.3


Single and Multithreaded Processes

[Fig. 4.1]

Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 4.4


The Need for Multithreading

[Fig. 4.2: Multithreaded Server Architecture]

Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 4.5


Benefits of Multithreaded Programming

◼ Responsiveness

◼ Resource Sharing

◼ Economy

◼ Scalability

Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 4.6


Types of Threads

Two types of threads are there:

1. User Threads:

➢ User threads are implemented by a thread library at user level.

➢ The kernel is unaware of user-level threads. It has no role in thread


creation, scheduling and management. The thread library provides
support for all these things.

➢ Therefore user-level threads are generally very fast to create and


manage [Advantage]

➢ Drawback: If the kernel is single threaded, then any user thread


performing a block system call will cause the entire process to block,
even if other threads are available to run within the process.

➢ Primary thread libraries: POSIX, Pthreads, Win32 threads, Java threads

Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 4.7


2. Kernel Threads:

➢ Kernel threads are directly supported by the OS. The kernel performs
thread creation, scheduling and management in kernel space.

➢ Therefore kernel threads are generally slower to create and manage


then are user threads. [Drawback]

➢ However since the kernel is managing the threads, if a thread performs


a blocking system call, the kernel can schedule another thread in the
application for execution [Advantage]

➢ Examples: Windows XP/2000, Solaris, Linux, Tru64 UNIX, Mac OS X

Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 4.8


Multithreading Models
1. Many-to-One
2. One-to-One
3. Many-to-Many

Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 4.9


Many-to-One Model

◼ Many user-level threads mapped to single kernel thread

◼ Examples:
➢ Solaris Green Threads
➢ GNU Portable Threads

◼ Drawbacks:
➢ The entire process will block
if a single user thread makes
a block system call.
➢ Since only one thread can access
the kernel at a time, multiple threads
are unable to run in parallel
on multiprocessors.

[Fig. 4.3: Many-to-One Multithreading Model]

Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 4.10


One-to-One Model

◼ Each user-level thread maps to kernel thread

◼ Examples:
➢ Windows NT/XP/2000
➢ Linux
➢ Solaris 9 and later

◼ Drawbacks:
➢ Creating a user thread requires creating the corresponding kernel
thread, which is a overhead. Because of this reason most
implementations of this model restrict the number of threads supported
by the system.

[Fig. 4.4:
One-to-One Multithreading Model]

Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 4.11


Many-to-Many Model

◼ Allows many user level threads to be mapped to many kernel threads.

◼ Examples:
➢ Solaris prior to version 9
➢ Windows NT/2000 with the Thread Fiber package

[Fig. 4.5:
Many-to-Many Multithreading Model]

Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 4.12


Threading Issues
◼ Semantics of fork ( ) and exec ( ) system calls

◼ Thread cancellation

◼ Signal handling

◼ Thread pools

◼ Thread-specific data

Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 4.13


Semantics of fork() and exec() System Calls

◼ Does fork ( ) duplicate the whole process (i.e. all threads within the process) or
only the calling thread?

◼ To avoid this situation some UNIX systems has chosen 2 versions of fork ( ) :

➢ One that duplicate only the calling tread


➢ Other that duplicate the entire process (i.e. all threads within the
process)

◼ exec ( ) system call has the same meaning: If a thread invokes the exec ( )
system call, the program specified in the parameter of exec ( ) will replace the
entire process including all the threads.

Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 4.14


Thread Cancellation

◼ Thread cancellation is the task of terminating a thread (Target Thread) before it


has finished.

◼ Example: If multiple threads are concurrently searching through a DB and one


of the threads returns the search result.

◼ Cancellation of a target thread can have two approaches:


➢ Asynchronous cancellation: OS terminates the target thread
immediately.
➢ Deferred cancellation: OS allows the target thread to periodically check
and determine whether if it should be terminated by itself.

◼ Difficulty with Cancellation occurs when:


➢ Resources have been allocated to a cancelled thread.
➢ The thread to be cancelled is in the middle of updating any data that is
shared with other thread.

Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 4.15


Signal Handling

◼ Signals are used in UNIX systems to notify a process that a particular event has
occurred.

◼ All Signal follow the same below pattern:

➢ A signal is generated by the occurrence of a particular event.


➢ A generated signal is delivered to a process.
➢ Once delivered the signal must be handled (by a Signal Handler)

◼ Issue with Signal Handling:


Where to deliver the signal? Following are the options:

➢ Deliver the signal to the thread to which the signal applies


➢ Deliver the signal to every thread in the process
➢ Deliver the signal to certain threads in the process
➢ Assign a specific thread to receive all signals for the process

Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 4.16


Thread Pool

◼ Need for Thread Pools?

1. If a process creates a new thread whenever a request comes some time


will be wasted for creating the thread prior to serving the request.
2. If a process creates a new thread whenever a request comes the OS
can’t put an upper bound to the number of threads concurrently active
in the system.

◼ The Idea Behind Thread Pools:

➢ Create a number of threads at process start up and place them into a


pool, where they sit and wait for work.
➢ When a server receives a request it awakens a thread form the pool and
pass the request to serve. Once the thread completes the work it returns
to the pool.
➢ If the pool contains no available threads the server waits, until one
becomes free.

Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 4.17


◼ Benefits:

1. Usually faster to service a request with an existing thread than create a


new thread
2. Allows the number of threads in the application(s) to be bound to the
size of the pool.

Thread Specific Data

◼ Threads that belongs to a process usually share the data of that process.

◼ However, each thread might need its own copy of certain data in some
circumstances. These are called thread specific data.

◼ Useful when you do not have control over the thread creation process (i.e.,
when using a thread pool)

Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 4.18


End of Chapter 4

You might also like