0% found this document useful (0 votes)
11 views10 pages

Module 2 Threads

Module 2 discusses multithreaded programming, defining threads as basic units of CPU utilization that share resources within a process. It highlights the benefits of multithreading, such as improved responsiveness and resource sharing, and outlines various threading models including many-to-one, one-to-one, and many-to-many. The module also covers thread libraries, threading issues, and the differences between processes and threads.

Uploaded by

haric0874
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)
11 views10 pages

Module 2 Threads

Module 2 discusses multithreaded programming, defining threads as basic units of CPU utilization that share resources within a process. It highlights the benefits of multithreading, such as improved responsiveness and resource sharing, and outlines various threading models including many-to-one, one-to-one, and many-to-many. The module also covers thread libraries, threading issues, and the differences between processes and threads.

Uploaded by

haric0874
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

Operating Systems Module 2

MODULE 2
MULTITHREADED PROGRAMMING
Overview
A thread is a basic unit of CPU utilization.

It consists of

• thread ID
• PC
• register-set and
• stack.
• It shares with other threads belonging to the same process its code-section &data-section.

• A traditional (or heavy weight) process has a single thread of control.

• If a process has multiple threads of control, it can perform more than one task at a time such
a process is called multithreaded process

Figure 2.7 Single-threaded and multithreaded processes

2.1 Motivation for Multithreaded Programming


1. The software packages that run on modern PCs are multithreaded. An application is
implemented as a separate process with several threads of control. For ex: A word processor
may have

• First thread for displaying graphics

• Second thread for responding to keystrokes and

• Third thread for performing grammar checking.

As process creation takes more time than thread creation it is more efficient to use process
that contains multiple threads. So, that the amount of time that a client has to wait for its request to
Department of CSE,RNSIT. Page 1
Operating Systems Module 2

be serviced from the web server will be less.

Threads also play an important role in remote procedure call.

2. In some situations, a single application may be required to perform several similar tasks.
For ex: A web- server may create a separate thread for each client requests. This allows
the server to service several concurrent requests.

3. RPC servers are multi-threaded. When a server receives a message, it services the message
using separate concurrent threads.

4. Most OS kernels are multithreaded.

Several threads operate in kernel, and each thread performs a specific task, such as managing
devices or interrupt handling.

Multithreaded Server Architecture

2.1.1 Benefits of Multithreaded Programming

• Responsiveness A program may be allowed to continue running even if part of it is


blocked. Thus, increasing responsiveness to the user.

• Resource Sharing By default, threads share the memory (and resources) of the process to
which they belong. Thus, an application is allowed to have several different threads of activity
within the same address-space.

• Economy Allocating memory and resources for process-creation is costly. Thus, it is


more economical to create and context-switch threads.

• Utilization of Multiprocessor Architectures In a multiprocessor archi t e ct ure, threads


may be running in parallel on different processors. Thus, parallelism will be increased.

2.2 MULTITHREADING MODELS


Department of CSE,RNSIT. Page 2
Operating Systems Module 2
• • Support for threads may be provided at either,

1. The user level, for user threads or

2. By the kernel, for kernel threads.

• User-threads are supported above the kernel and are managed without kernel support.
Kernel- threads are supported and managed directly by the OS.

• Three ways of establishing relationship between user-threads &kernel-threads:

1. Many-to-one model
2. One-to-one model and
3. Many-to-many model.

Department of CSE,RNSIT. Page 3


Operating Systems Module 2

2.2.1 Many-to-One Model


Many user-level threads are mapped to one kernel thread.
Advantages:
• Thread management is done by the thread library in user space,
so it is efficient.

Disadvantages:
• The entire process will block if a thread makes a blocking
system-call.
• Multiple threads are unable to run in parallel on multiprocessors.
For example:

• Solaris green threads


• GNU portable threads.

2.2.2 One-to-One Model


Each user thread is mapped to a kernel thread.

Advantages:
• It provides more concurrency by allowing another thread
to run when a threadmakes a blocking system-call.
• Multiple threads can run in parallel on multiprocessors.
Disadvantage:
• Creating a user thread requires creating the corresponding kernel thread.

For example: Windows NT/XP/2000, Linux

2.2.3 Many-to-Many Model


• Many user-level threads are multiplexed to a smaller number of kernel threads.
Advantages:
• Developers can create as many user threads as necessary
• The kernel threads can run in parallel on a multiprocessor.
• When a thread performs a blocking system-call, kernel can schedule another thread for
execution.

2.2.4 Two Level Model

• A variation on the many-to-many model is the two level-model


• Similar to M:N, except that it allows a user thread to be bound to kernel thread.
For example:
• HP-UX
• Tru64 UNIX

Department of CSE,RNSIT. Page 4


Operating Systems Module 2

Figure 2.8 (a) Many-to-many model (b) : Two-level model

2.3 Thread Libraries


It provides the programmer with an API for the creation and management of threads.

Two ways of implementation:

1. First Approach: Provides a library entirely in user space with no kernel support. All code
and data structures for the library exist in the user space.

2. Second Approach: Provides a library entirely in user space with no kernel support. All
code and data structuresfor the library exist in the user space.

Three main thread libraries:

1. POSIX Pthreads: extension of posix standard, they may be provided as either a user nor
kernel library.

2. Win32: is a kernel level library available on windows systems.

3. Java threads: API allows creation and management directly in Java programs. However,
on windows java threads are implemented using win32 and on UNIX and Linux using
Pthreads.

2.3.1 Pthreads

• This is a POSIX standard API for thread creation and synchronization.


• This is a specification for thread-behavior, not an implementation.
• OS designers may implement the specification in any way they wish.
• Commonly used in: UNIX and Solaris.

Multithreaded C program using the Pthreads API


#include <pthread.h>
#include <stdio.h>
int sum; /* this data is shared by the thread(s) */
void *runner(void *param); /* the thread */
int main(int argc, char *argv[])
Department of CSE,RNSIT. Page 5
Operating Systems Module 2

{
pthread_t tid; /* the thread identifier */
pthread_attr_t attr; /* set of thread attributes */

if (argc != 2)
{
fprintf(stderr,"usage: [Link] <integer value>\n");
return -1;
}
if (atoi(argv[1]) < 0)
{
fprintf(stderr,"%d must be>= 0\n",atoi(argv[1]));
return -1;
}
pthread_attr_init(&attr); /* get the default attributes */
pthread_create(&tid,&attr,runner,argv[1]); /* create the thread */
pthread_join(tid,NULL); /* wait for the thread to exit */
printf("sum = %d\n",sum);
}
void *runner(void *param) /* The thread will begin control in this function */
{
int i, upper= atoi(param);
sum = 0;
for (i = 1; i <= upper; i++)
sum += i;
pthread_exit(0) ;
}

Win32 threads
• The Win32 thread library is a kernel-level library available on Windows systems.

• Implements the one-to-one mapping

• Each thread contains: A thread id, Register set, Separate user and kernel stacks and Private
data storage area

• The register set, stacks, and private storage area are known as the context of the threads. The
primary data structures of a thread include:

• ETHREAD (executive thread block)


• KTHREAD (kernel thread block)
• TEB (thread environment block)
Java Threads

• Threads are the basic model of program-execution in


• Java program and
• Java language.

Department of CSE,RNSIT. Page 6


Operating Systems Module 2

• The API provides a rich set of features for the creation and management of threads.

• All Java programs comprise at least a single thread of control.

• Two techniques for creating threads:

Create a new class that is derived from the Thread class and override its run() method.

Define a class that implements the Runnable interface. The Runnable interface is defined as
follows:

• When a class implements Runnable, it must define a run() method. The code implementing
the run() method runs as a separate thread.

• Creating a Thread object does not specifically create the new thread but it is the start()
method that actually creates the new thread. Calling the start() method for the new object
does two things:

• It allocates memory and initializes a new thread in the JVM.

• It calls the run() method, making the thread eligible to be run by the JVM.

• As Java is a pure object-oriented language, it has no notion of global data. If two or more
threads have to share data means then the sharing occurs by passing reference to the shared
object to the appropriate threads.

• This shared object is referenced through the appropriate getSum() and setSum() methods.

• As the Integer class is immutable, that is, once its value is set it cannot change, a new
sum class is designed.

• The parent threads in Java uses join() method to wait for the child threads to finish before
proceeding.

2.3.2 THREADING ISSUES

fork() and exec() System-calls


• fork() is used to create a separate, duplicate process.
• If one thread in a program calls fork(),then
1. Some systems duplicate all threads and
2. Other systems duplicate only the thread that invoked the fork().

Department of CSE,RNSIT. Page 7


Operating Systems Module 2

• If a thread invokes the exec(), the program specified in the parameter to exec() will
replace the entire process including all threads.

2.3.3 Thread Cancellation

• This is the task of terminating a thread before it has been completed.

• Target thread is the thread that is to be cancelled.

• Thread cancellation occurs in two different cases:

Asynchronous cancellation: One thread immediately terminates the target thread.


Deferred cancellation: The target thread periodically checks whether it should be terminated.

Signal Handling

• In UNIX, a signal is used to notify a process that a particular event has occurred.

• All signals follow this pattern:

1. A signal is generated by the occurrence of a certain event.


2. A generated signal is delivered to a process.
3. Once delivered, the signal must be handled.

• A signal handler is used to process signals.

• A signal may be received either synchronously or asynchronously, depending on the source.

1. Synchronous signals

• Delivered to the same process that performed the operation causing the signal.

E.g. illegal memory access and division by 0.

2. Asynchronous signals

• Generated by an event external to a running process.

E.g. user terminating a process with specific keystrokes<ctrl><c>.

Every signal can be handled by one of two possible handlers:

1. A Default Signal Handler : Run by the kernel when handling the signal.
2. A User-defined Signal Handler

• Overrides the default signal handler.

• In single-threaded programs, delivering signals is simple (since


signals are always delivered to a process).

Department of CSE,RNSIT. Page 8


Operating Systems Module 2

• In multithreaded programs, delivering signals is more complex.


Then, the following options exist:
1. Deliver the signal to the thread to which the signal applies.

2. Deliver the signal to every thread in process

3. Deliver the signal to certain threads in the process.

4. Assign a specific thread to receive all signals for the process.

2.3.4 THREAD POOLS

The basic idea is to

• create a no. of threads at process-startup and


• place the threads into a pool (where they sit and wait for work).
Procedure:
• When a server receives a request, it awakens a thread from the pool.
• If any thread is available, the request is passed to it for service.
• Once the service is completed, the thread returns to the pool.
Advantages:
• Servicing a request with an existing thread is usually faster than waiting tocreate a thread.

The pool limits the no. of threads that exist at any one point.
• No. of threads in the pool can be based on actors such as
• no. of CPUs
• amount of memory and
• expected no. of concurrent client-requests.

2.3.5 THREAD SPECIFIC DATA

• Threads belonging to a process share the data of the process.

• this sharing of data provides one of the benefits of multithreaded programming.

• In some circumstances, each thread might need its own copy of certain data. We will call
such data

thread-specific data.
• For example, in a transaction-processing system, we might service each transaction in a
separate thread.

Department of CSE,RNSIT. Page 9


Operating Systems Module 2

• Furthermore, each transaction may be assigned a unique identifier. To associate each thread
with its unique identifier, we could use thread-specific data.

SCHEDULER ACTIVATIONS
• Both M:M and Two-level models require communication to maintain the appropriate number
of kernel threads allocated to the application.

• Scheduler activations provide upcalls a communication mechanism from the kernel to the
thread library

• This communication allows an application to maintain the correct number kernel threads

• One scheme for communication between the user-thread library and the kernel isknown as
scheduler activation.

Differences
The differences between process and thread are,
Process Thread

1. It is called heavyweight process. It is called lightweight process.

2. Process switching needs interface with OS. Thread switching does not need interface with OS.

3. Multiple processes use more resources than Multiple threaded processes use fewer resources
multiple threads. than multiple processes.
4. In multiple process implementations each All threads can share same set of open files.
process executes same code but has its own
memory and file resources.
5. If one server process is blocked no other While one server thread is blocked and waiting,
server process can execute until the first second thread in the same task could run.
process unblocked.
6. In multiple processes each process operates One thread can read, write or even completely
independently of others. wipeout another threads stack.

Department of CSE,RNSIT. Page 10

You might also like