0% found this document useful (0 votes)
20 views

OS Multi-Threading Concepts

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

OS Multi-Threading Concepts

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

Multi-Threaded Programming

A Thread is a basic unit of CPU utilization.

It comprises
 a thread ID
 a program counter
 a register set and
 a stack
It shares with other threads belonging to the same process
its:

 code section
 data section and
 other OS resources such as open files & signals

A traditional process(or heavyweight) has a single


thread of control. If a process has multiple threads
of control, It can perform more than one task at
time
Motivation
Many software packages that run on modern desktop PC’s are
multi-threaded.

An application typically is implemented as a separate process with several


threads of control.
Ex :
1) A web browser might have one thread for displaying and another for
retrieving data from the network.

2) A word processor may have a threads for


• displaying graphics
• responding to key strokes from the user
• performing spelling and grammar checking in the background
• Consider a Web server application which accepts client
requests for Web pages, images, sound and so forth.

• A busy server may have several(perhaps thousands)


of clients concurrently accessing it.

• If the Web server ran as a traditional single-threaded


process, It would be able to service only one client at a
time

• In this case, client might wait for enormous amount of


time for its request to be serviced
Before thread became popular solution existed
was:
 to have the server run as a single process that accept requests.
 When the server receives a request, It creates a separate
process to service that request.

If the new process will perform the same tasks as the


existing process, why incur the overhead?(Because the
Process creation is time consuming and resource
intensive)
The Efficient approach is to multithread the Web server process. i.e, To use
one process that contains multiple threads.

The server will create a separate thread that listens for client requests.

When a request is made, rather than creating another process, the server will
create a new thread to service the request and resume listening for additional
requests.

• RPC(Remote Procedure Call) Systems – Servers are Multithreaded


• Java’s RMI systems work similarly

Finally, most operating system kernels are now multithreaded; several threads operate
in the kernel, and each thread performs a specific task, such as managing devices or
interrupt handling.

For example, Solaris creates a set of threads in the kernel specifically for interrupt
handling; Linux uses a kernel thread for managing the amount of free memory in the
system.
Benefits:
The benefits of multithreaded programming can
be broken down into 4 categories

• Responsiveness: Multithreading an interactive application


may allow a program to continue running even if part of it is
blocked or is performing a lengthy operation, thereby
increasing the responsiveness to the user.

• Resource Sharing: Processes may only share resources via IPC


models. By default, threads share the memory and the
resources of the process to which they belong. The benefit of
sharing code and data is that it allows an application to have
several different threads of activity within the same address
space.
• Economy: Allocating memory and resources for process
creation is costly. Since threads share resources of the
process to which they belong, It is more economical to
create and context-switch threads. Creating and
managing processes is much more time consuming than
threads.

• Scalability. The benefits of multithreading can be greatly


increased in a multiprocessor architecture, where threads
may be running in parallel on different processors. A
single threaded process can only run on one CPU, no
matter how many are available. Multithreading on a
multi-CPU machine increases concurrency.
Multicore Programming
• A recent trend in system design has been to
place multiple computing cores on a single
chip, where each core appears as a separate
processor to the operating system

• On a system with multiple cores, however,


concurrency means that the threads can run
in parallel, as the system can assign a separate
thread to each core
• Designers of operating systems must write
scheduling algorithms that use multiple
processing cores to allow the parallel
execution

• For application programmers, the challenge is


to modify existing programs as well as design
new programs that are multithreaded to take
advantage of multicore systems.
Five areas that present challenges in programming for multicore systems are:

1) Dividing activities. This involves examining applications to find areas that


can be divided into separate, concurrent tasks and thus can run in parallel on
individual cores.

2) Balance : Programmers must also ensure that the tasks perform equal work of
equal value.

3) Data splitting. Just as applications are divided into separate tasks, the data
accessed and manipulated by the tasks must be divided to run on
separate cores.

4) Data dependency. programmers must ensure that the execution of the tasks is
synchronized to accommodate the data dependency.

5) Testing and debugging. When a program is running in parallel on multiple cores,


there are many different execution paths. Testing and debugging such concurrent
programs is inherently more difficult than testing and debugging single-threaded
applications.
Multi-threading Models
User threads are supported above the kernel and are
managed without kernel support
Kernel threads are supported and managed directly by
the operating system.

3 ways of establishing relationship between the user


threads and kernel threads are:
1)Many-to-one model
2)One-to-one model
3)One-to-many model
• The many-to-one model maps many user-level threads to one kernel
thread.
• Thread management is done by the thread library in user space, so it is
efficient; but 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 multiprocessors.
• Ex: Green Threads – a thread library available for Solaris
• The one-to-one model maps each user thread to a kernel thread. It
provides more concurrency than the many-to-one model by allowing
another thread to run when a thread makes a blocking system call; it also
allows multiple threads to run in parallel on multiprocessors.
• The only drawback to this model is that creating a user thread requires
creating the corresponding kernel thread.
• Ex: Linux, along with the family of Windows operating systems, implement
the one-to-one model.
• 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 (an application may be allocated more
kernel threads on a multiprocessor than on a uniprocessor).
One popular variation on the many-to-many model still multiplexes many
user-level threads to a smaller or equal number of kernel threads but also
allows a user-level thread to be bound to a kernel thread. This variation,
sometimes referred to as the two-level model

Ex: Irix,HP-UX, Tru-64 Unix, Solaris systems(older than Solaris 9)


Thread Libraries
• A Thread Library provides the programmer with an API for creating and
managing threads.

There are two primary ways of implementing a thread library.

• The first approach is to provide a library entirely in user space with no


kernel support. All code and data structures for the library exist in user space.
This means that invoking a function in the library results in a local function
call in user space and not a system call.

• The second approach is to implement a kernel-level library supported


directly by the operating system. In this case, code and data structures for
the library exist in kernel space. Invoking a function in the API for the library
typically results in a system call to the kernel.
• Three main thread libraries are in use today:
(1) POSIX Pthreads,
(2) Win32 and
(3) Java

Pthreads, the threads extension of the POSIX standard, may be


provided as either a user- or kernel-level library.

The Win32 thread library is a kernel-level library available on Windows


systems.

The Java thread API allows threads to be created and managed directly in
Java programs.
#include <pthread.h>
#include <stdio.h>
int sum; I* this data is shared by the thread(s) *I
void *runner(void *param); I* the thread *I
int main(int argc, char *argv[])
{
pthread_t tid; I* the thread identifier *I
pthread_attr_t attr; I* set of thread attributes *I
if (argc != 2) {
fprintf(stderr,"usage: a.out <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); I* get the default attributes *I

pthread_create(&tid,&attr,runner,argv[1]); I* create the thread *I

pthread_join(tid,NULL); I* wait for the thread to exit *I


printf("sum = %d\n",sum);
}
I* The thread will begin control in this function *I

void *runner(void *param)


{
int i, upper= atoi(param);
sum = 0;
for(i = 1; i <= upper; i++)
sum += i;
pthread_exi t ( 0) ;
}

Multithreaded C program using the Pthreads API.


#include <windows.h>
#include <stdio.h>
DWORD Sum; I* data is shared by the thread(s) *I
DWORD WINAPI Summation(LPVOID Param) I* the thread runs in this separate function *I
{
DWORD Upper = *(DWORD*)Param;
for (DWORD i = 0; i <= Upper; i++)
Sum += i;
return 0;
}

int main(int argc, char *argv[])


{
DWORD Threadid;
HANDLE ThreadHandle;
int Param;
I* perform some basic error checking *I
if (argc != 2) {
fprintf(stderr,"An integer parameter is required\n");
return -1;
}
Param = atoi(argv[1]);
if (Param < 0) {
fprintf(stderr,"An integer>= 0 is required\n");
return -1;
}

II create the thread


ThreadHandle = CreateThread(
NULL, II default security attributes
0, II default stack size
Summation, II thread function
&Param, II parameter to thread function
0, II default creation flags
&Threadid); II returns the thread identifier
if (ThreadHandle != NULL) {
WaitForSingleObject(ThreadHandle,INFINITE); II now wait for the thread to finish
CloseHandle(ThreadHandle); II close the thread handle
printf("sum = %d\n" ,Sum);
}
}

Multithreaded C program using the Win32 API.


• Threads are the fundamental model of
program execution in a Java program
• The Java language and its API provide a rich
set of features for the creation and
management of threads.

You might also like