0% found this document useful (0 votes)
32 views27 pages

OS Chapter 4 Final

The document discusses threads and multithreaded programming. Threads are lightweight processes that execute concurrently within a process and share resources like memory. Multithreaded programming allows a process to perform multiple tasks simultaneously using threads.

Uploaded by

Apeksha Sharma
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)
32 views27 pages

OS Chapter 4 Final

The document discusses threads and multithreaded programming. Threads are lightweight processes that execute concurrently within a process and share resources like memory. Multithreaded programming allows a process to perform multiple tasks simultaneously using threads.

Uploaded by

Apeksha Sharma
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/ 27

CHAPTER -4

THREADS
 It is a path of execution.

 Keeps track of which instruction to execute next

 Information of stack: stores history of execution

 Information of system registers: current working variables

 It is called light weight process(LWP) and a process can have multiple


threads
 Threads share information with peer threads:
1. Code segment
2. Data segment

Note: if one thread makes changes in the code segment area than
all the other threads can see that.
Multithreaded Programming
 Each Thread belongs to a process and no thread exist outside the process so each
thread represent separate flow of execution.

 A thread comprises the following:


1. Thread Id,
2. Program counter (that keeps track of which instruction to execute next)
3. System registers (which hold its current working variables)
4. Stack (which contains the execution history)

 A thread shares with its peer threads few information like code segment, data
segment and open files. When one thread alters a code segment memory item, all
other threads see that.

 If a process has multiple threads of control, it can perform more than one task at a
time.
Threads have been successfully used in implementing network servers and web server.
They also provide a suitable foundation for parallel execution of applications on shared
memory multiprocessors. The following figure shows the working of a single-threaded and
a multithreaded process.
Difference between Process and Thread

S.N Process Thread


1. System calls involved in process No system calls involved in user level thread
creation creation
2 Process is heavy weight or resource Thread is light weight, taking lesser resources
intensive. than a process.
3 Process switching needs interaction Thread switching does not need to interact with
with operating system. operating system.
4 Different process have different copies All threads can share same set of open files, child
of data, files and code section processes.

5 Blocking a process will not block Blocking a user level thread will block entire
another process. Processes are process. However threads are interdependent.
independent.
6 Multiple processes without using Multiple threaded processes use fewer resources.
threads use more resources.
7 In multiple processes each process One thread can read, write or change another
operates independently of the others. thread's data.

8 Context switching is slower. Context switching is faster.


Benefits of Multithreading in Operating System
Responsiveness –
Multithreading in an interactive application may allow a program to continue running
even if a part of it is blocked or is performing a lengthy operation, thereby increasing
responsiveness to the user.

In a non multi threaded environment, a server listens to the port for some request and
when the request comes, it processes the request and then resume listening to another
request. The time taken while processing of request makes other users wait unnecessarily.
Instead a better approach would be to pass the request to a worker thread and continue
listening to port.

For example, a multi threaded web browser allow user interaction in one thread while an
video is being loaded in another thread.

So instead of waiting for the whole web-page to load the user can continue viewing some
portion of the web-page.
Benefits of Multithreading in Operating System

Resource Sharing –
Processes may share resources only through techniques such as-
Message Passing
Shared Memory
Such techniques must be explicitly organized by programmer.

However, threads share the memory and the resources of the process to which
they belong by default.
The benefit of sharing code and data is that it allows an application to have
several threads of activity within same address space.
Benefits of Multithreading in Operating System
Economy –
Allocating memory and resources for process creation is a costly job in terms of time and
space.

Since, threads share memory with the process it belongs, it is more economical to create
and context switch threads. Generally much more time is consumed in creating and
managing processes than in threads.

In Solaris, for example, creating process is 30 times slower than creating threads and context
switching is 5 times slower.
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.

Single threaded process can run only on one processor regardless of how many processors
are available.

Multi-threading on a multiple CPU machine increases parallelism.


Multicore Programming
 Multicore programming helps to create concurrent systems for deployment on
multicore processor and multiprocessor systems.

 A multicore processor system is basically a single processor with multiple execution


cores in one chip. It has multiple processors on the motherboard or chip.

 Concurrent systems that we create using multicore programming have multiple tasks
executing in parallel. This is known as concurrent execution.
Concurrency
Concurrency means that an application is making progress on more than one
task at the same time (concurrently). Well, if the computer only has one CPU the
application may not make progress on more than one task at exactly the same
time, but more than one task is being processed at a time inside the
application. It does not completely finish one task before it begins the next.
Instead, the CPU switches between the different tasks until the tasks are complete.

It is possible to have a concurrent application


even though it only has a single thread
running inside it.
Parallelism

Parallelism means that an application splits its tasks up into smaller subtasks which can be
processed in parallel, for instance on multiple CPUs at the exact same time.

To achieve true parallelism your


application must have more than one
thread running, or at least be able to
schedule tasks for execution in other
threads, processes, CPUs, graphics
cards etc.
Concurrency vs. Parallelism

Concurrent execution on single-core system:

Parallelism on a multi-core system:


Types of Parallelism
Data Parallelism
 Data Parallelism means concurrent execution of the same task on each
multiple computing core.

Let’s take an example, summing the contents of an array of size N. For a single-core system,
one thread would simply sum the elements [0] . . . [N − 1]. For a dual-core system, however,
thread A, running on core 0, could sum the elements [0] . . . [N/2 − 1] and while thread B,
running on core 1, could sum the elements [N/2] . . . [N − 1]. So the Two threads would be
running in parallel on separate computing cores.

Task Parallelism
 Task Parallelism means concurrent execution of the different task on multiple
computing cores.

Consider again our example above, an example of task parallelism might involve two
threads, each performing a unique statistical operation on the array of elements. Again The
threads are operating in parallel on separate computing cores, but each is performing a
unique operation.
Programming Challenges
For application programmers, the challenge is to switch existing
programs with its style new programs that are multithreaded. In
general, 5 areas of challenges are there in programming for
multicore systems −

Identifying tasks − This involves examining applications to seek out areas that may be
divided into separate, concurrent tasks. Ideally, tasks are independent of one another
and therefore will run in parallel on individual cores.

Balance − Whereas distinctive tasks that may run in parallel, programmers should
make sure that the tasks perform equal work of equal worth. In some instances, a
definite task might not contribute the maximum amount worth to the method as
different tasks. employing a separate execution core to run that task might not be well
worth the cost.

Data splitting − Even as applications are divided into separate tasks, the data accessed
and manipulated by the tasks should be divided to run on separate cores.
Data dependency − The data accessed by the tasks should be examined for
dependencies between 2 or additional tasks. once one task depends on data
from another, programmers should make sure that the execution of the tasks
is synchronal to accommodate the data dependency.

Testing and debugging − Once a program is running in parallel on multiple


cores, many alternative execution paths are attainable. Testing and debugging
such concurrent programs is inherently tougher than testing and debugging
single-threaded applications.

Because of these challenges, several software system developers argue that


the arrival of multicore systems would require an entirely new approach to
coming up with software system systems within the future.
Types of Thread

Threads are implemented in following two ways −

 User Level Threads − User managed threads.


 Kernel Level Threads − Operating System managed threads acting on kernel, an
operating system core.

User - Level Threads


The user-level threads are implemented by users and the kernel is not aware of the
existence of these threads. It handles them as if they were single-threaded processes. User-
level threads are small and much faster than kernel level threads. They are represented by a
program counter(PC), stack, registers and a small process control block. Also, there is no
kernel involvement in synchronization for user-level threads.

Kernel-Level Threads
Kernel-level threads are handled by the operating system directly and the thread
management is done by the kernel. The context information for the process as well as the
process threads is all managed by the kernel. Because of this, kernel-level threads are
slower than user-level threads.
Difference between User-Level & Kernel-Level Thread

S.N. User-Level Threads Kernel-Level Thread

1 User-level threads are faster to create Kernel-level threads are slower to


and manage. create and manage.

2 Implementation is by a thread library Operating system supports creation of


at the user level. Kernel threads.

3 User-level thread is generic and can Kernel-level thread is specific to the


run on any operating system. operating system.

4 Multi-threaded applications cannot Kernel routines themselves can be


take advantage of multiprocessing. multithreaded.
Multithreading Models

Some operating system provide a combined user level thread and Kernel level thread
facility. Solaris is a good example of this combined approach. In a combined system,
multiple threads within the same application can run in parallel on multiple processors
and a blocking system call need not block the entire process. Multithreading models are
three types

 Many to many relationship.


 Many to one relationship.
 One to one relationship.
Many to Many Model
 The many-to-many model multiplexes any number of user threads onto an equal or
smaller number of kernel threads.

 The following diagram shows the many-to-many threading model where 6 user level
threads are multiplexing with 6 kernel level threads.

 In this model, developers can create as many user threads as necessary and the
corresponding Kernel threads can run in parallel on a multiprocessor machine. This model
provides the best accuracy on concurrency and when a thread performs a blocking system
call, the kernel can schedule another thread for execution.
Many to One Model
 Many-to-one model maps many user level threads to one Kernel-level thread. Thread
management is done in user space by the thread library.

 When thread makes a blocking system call, the entire process will be blocked.

 Only one thread can access the Kernel at a time, so multiple threads are unable to run in
parallel on multiprocessors .

 If the user-level thread libraries are implemented in the operating system in such a way
that the system does not support them, then the Kernel threads use the many-to-one
relationship modes.
One to One Model
 There is one-to-one relationship of user-level thread to the kernel-level thread. This
model provides more concurrency than the many-to-one model.

 It also allows another thread to run when a thread makes a blocking system call. It
supports multiple threads to execute in parallel on microprocessors.

 Disadvantage of this model is that creating user thread requires the corresponding
Kernel thread. OS/2, windows NT and windows 2000 use one to one relationship model
int main()
#include<iostream> {
#include<pthread.h> int r1,r2;
#include<iostream> cout<<"Process id: "<<getpid();
#include<sys/wait.h> cout<<"\nBefore creation of thread\n";
using namespace std;
pthread_t t1,t2;
void* routine(void *args)
{ r1=pthread_create(&t1,NULL,routine,NULL);
cout<<"Hello from threads\n"; r2=pthread_create(&t2,NULL,routine,NULL);
cout<<"Process Id:\n"<<getpid(); if (r1==0)
} cout<<"Thread1 created successfully:\n";
else
cout<<"Problem in creating thread1:\n";
if (r2==0)
cout<<"Thread2 created successfully:\n";
else
cout<<"Problem in creating thread2:\n";

pthread_join(t1,NULL);
pthread_join(t2,NULL);
cout<<"Thread termonated\n";
return 0;
}
#include<iostream> int main()
#include<pthread.h> {
#include<iostream> int r;
#include<sys/wait.h> cout<<"Process id: "<<getpid();
using namespace std; cout<<"\nBefore creation of thread\n";

void* routine(void* args) pthread_t t;


{
cout<<"Hello from thread1\n"; r=pthread_create(&t,NULL,routine,NULL);
sleep(20);
} if (r==0)
cout<<"Thread created successfully:\n";
else
cout<<"Problem in creating thread:\n";

pthread_join(t,NULL);

cout<<"Thread termonated\n";
return 0;
}
#include<iostream>
#include<pthread.h>
#include<iostream>
void* fibonacci(void*);
using namespace std;

int n;
void* fibonnacci(void* arg)
{
int c, first = 0, second = 1, next;
for ( c = 0 ; c <n; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
cout << next << endl;
}
}
int main()
{

pthread_t t;

cout << "Enter the number of terms of Fibonacci series you want" << endl;
cin >> n;
cout << "First " << n << " terms of Fibonacci series are :- " << endl;

pthread_create (&t , NULL , fibonacci, NULL);

pthread_join(t,NULL);

pthread_exit(NULL);

return 0;
}
1. What is a Thread?
A thread is a single sequence stream within a process. Because threads have some of
the properties of processes, they are sometimes called lightweight processes.

Threads are a popular way to improve the application through parallelism. For
example, in a browser, multiple tabs can be different threads. MS Word uses multiple
threads, one thread to format the text, another thread to process inputs, etc.

2. What are the differences between process and thread?


A thread has its own program counter (PC), a register set, and a stack space. Threads
are not independent of one another, like processes.

As a result, threads share with other threads their code section, data section, and OS
resources like open files and signals.

Q3. What are the benefits of multithreaded programming?

It makes the system more responsive and enables resource sharing. It leads to the use
of multiprocess architecture. It is more economical and preferred.

You might also like