Unit 2 - OS
Unit 2 - OS
Ghaziabad
Operating Systems
BCS-401
Unit: II
Operating System
Mr. H.S.Tomer
B Tech :4th Sem Assistant Professor
CSE(AI&ML)
1
Course Outcomes
Course outcome: After completion of this course students will be able to:
3
Process Synchronization
5
Process Synchronization
When two or more process cooperates with each other, their order
of execution must be preserved otherwise there can be conflicts in
their execution and inappropriate outputs can be produced.
6
Process Synchronization
Concurrency:- Concurrency in operating systems refers to the ability of an
operating system to handle multiple tasks or processes at the same time.
8
Process Synchronization
9
Process Synchronization
11
Process Synchronization
12
Process Synchronization
13
Process Synchronization
15
Process Synchronization
16
Process Synchronization
•Data can only be consumed by the consumer if and only if the memory buffer
is not empty. In case it is found that the buffer is empty, the consumer is not
allowed to use any data from the memory buffer.
18
Process Synchronization
19
Process Synchronization
20
Process Synchronization
21
Process Synchronization
22
Process Synchronization
23
Process Synchronization
24
Process Synchronization
Critical Section:- The regions of a program that try to access shared
resources and may cause race conditions are called critical section. To avoid
race condition among the processes, we need to assure that only one process
at a time can execute within the critical section.
The critical section problem is used to design a set of protocols which
can ensure that the Race condition among the processes will never arise.
Proper use of critical sections and process synchronization
mechanisms is essential in concurrent programming to ensure proper
synchronization of shared resources and avoid race conditions, deadlocks, and
other synchronization-related issues.
25
Process Synchronization
26
Process Synchronization
In order to synchronize the cooperative processes, our main task is to solve
the critical section problem. We need to provide a solution in such a way that
the following conditions can be satisfied:-
Mutual Exclusion :-Mutual exclusion implies that only one process can be
inside the critical section at any time. If any other processes require the
critical section, they must wait until it is free.
Progress :-Progress means that if one process doesn't need to execute into
critical section then it should not stop other processes to get into the critical
section.
Bounded Waiting :-We should be able to predict the waiting time for every
process to get into the critical section. The process must not be endlessly
waiting for getting into the critical section.
27
Process Synchronization
Advantages of critical section in process synchronization:
1. Prevents race conditions: By ensuring that only one process can execute
the critical section at a time, race conditions are prevented, ensuring
consistency of shared data.
2. Provides mutual exclusion: Critical sections provide mutual exclusion to
shared resources, preventing multiple processes from accessing the same
resource simultaneously and causing synchronization-related issues.
3. Reduces CPU utilization: By allowing processes to wait without wasting
CPU cycles, critical sections can reduce CPU utilization, improving overall
system efficiency.
4. Simplifies synchronization: Critical sections simplify the
synchronization of shared resources, as only one process can access the
resource at a time, eliminating the need for more complex synchronization
28
mechanisms.
Process Synchronization
29
Process Synchronization
30
Process Synchronization
The mutual exclusion problem is to design a pre-protocol and a post-
protocol based on either hardware or software that-
•Prevent two processes from being in their critical sections at the same time.
•Hence the desirable no deadlock and no starvation properties.
Allow critical sections to be executed atomically
31
Process Synchronization
Solutions to Mutual Exclusion Problem:
•Software Solutions: Correctness does not rely on any other
assumptions
• Two process solution
1. Algorithm 1.
2. Algorithm 2
3. Algorithm 3 (Dekker’s Algo)
4. Algorithm 4 (Peterson’s Algo)
•. N-Process solution
1. Lampor’t Bakery Algorithm.
32
Process Synchronization
• Hardware Solutions: Rely on some special machine instructions.
1. Disabling Interrupts
2. Special Machine Instructions
3. Test and Set Lock.
• Operating System Solutions: Provide some functions and data structures
to programmes.
1. Semaphores
2. Mutex
33
Process Synchronization
34
Process Synchronization
35
Process Synchronization
36
Process Synchronization
Dekker’s Algorithm:--
Dekker’s algorithm is the first solution of critical section problem. There are
many versions of this algorithms, the 5th or final version satisfies the all the
conditions below and is the most efficient among all of them. The solution to
critical section problem must ensure the following three conditions:
•Mutual Exclusion
•Progress
•Bounded Waiting
37
Process Pi Process Pj
do { do {
flag [i] = true; flag [j] = true;
while (flag[j]) while (flag[i])
{ {
If (turn = = j) If (turn = = i)
{ {
flag[i] = false; flag[j] = false;
while (turn = = j); while (turn = = i);
flag[i] = true; flag[j] = true;
} }
} }
critical section critical section
turn = j; turn = i;
flag[i] = false; flag[j] = false;
remainder section remainder section
} }
38
while (true) while (true)
Process Synchronization
Peterson’s Algorithm:-- This Algorithm satisfies all the 3 conditions of the
critical section problem.
The process has 2 variables:-
1. int turn
2. Boolean flag[2]
initially
flag[0]=flag[1]=false
turn- immaterial
39
Process Synchronization
40
Process Synchronization
41
Process Synchronization
42
Process Synchronization
43
Process Synchronization
44
Process Synchronization
45
Process Synchronization
46
Process Synchronization
47
Process Synchronization
48
Process Synchronization
Semaphores:- are integer variables share between the threads that are used to
solve the critical section problem by using two atomic operations, wait and
signal that are used for process synchronization.
The definitions of wait and signal are as follows −
Wait:- The wait operation decrements the value of its argument S, if it is
positive. If S is negative or zero, then no operation is performed.
49
Process Synchronization
Types of Semaphores
There are two main types of semaphores i.e. counting semaphores and binary
semaphores. Details about these are given as follows −
Counting Semaphores :- These are integer value semaphores and have an
unrestricted value domain. Used when a recourse has multiple instances. If
the resources are added, semaphore count automatically incremented and if
the resources are removed, the count is decremented.
Binary Semaphores :-The binary semaphores are like counting semaphores
but their value is restricted to 0 and 1. The wait operation only works when
the semaphore is 1 and the signal operation succeeds when semaphore is 0. It
is sometimes easier to implement binary semaphores than counting
semaphores.
50
Process Synchronization
51
Process Synchronization
52
Process Synchronization
53
Process Synchronization
54
Process Synchronization
55
Process Synchronization
2. Dining-Philosophers Problem
56
Process Synchronization
2. Dining-Philosophers Problem
57
Process Synchronization
58
Process Synchronization
59
Process Synchronization
3. Sleeping Barber Problem
A barbershop consists of a waiting room with n chairs and the barber room containing the
barber chair. If there are no customers to be served, the barber goes to sleep. If a customer
enters the barbershop and all chairs are occupied, then the customer leaves the shop. If the
barber is busy but chairs are available, then the customer sits in one of the free chairs. If the
barber is asleep, the customer wakes up the barber.
60
Process Synchronization
We use 3 semaphores.:-
Chairs :- n
Semaphore Customers (0) :- counts waiting customers
Semaphore Barbers (0) :- is the barbers idle 0 otherwise 1
Semaphore Mutex (1) :- is used for mutual exclusion.
int waiting (0) :- A shared data variable counts waiting customers copy of semaphore
customers needed because we can’t access the value of semaphores directly.
61
Process Synchronization
Customer Process
Barber Process
do()
do()
{
wait(mutex); {
if (waiting <chair ) wait(customers);
{ wait(mutex);
waiting = waiting + 1 waiting = waiting - 1 ;
signal(customers); signal(barbers);
signal(mutex); signal(mutex);
wait(barbers); cut_hair();
get_haircut(); }while(true)
}
else
signal(mutex);
}while(true) 62
Process Synchronization
63
Process Synchronization
Customer Process
Barber Process
do()
do()
{
wait(mutex); {
if (waiting <chair ) wait(customers);
{ wait(mutex);
waiting = waiting + 1 waiting = waiting - 1 ;
signal(customers); signal(barbers);
signal(mutex); signal(mutex);
wait(barbers); cut_hair();
get_haircut(); }while(true)
}
else
signal(mutex);
}while(true) 64
Process Synchronization
65
Process Synchronization
66
Process Synchronization
Interprocess communication is the mechanism provided by the operating
system that allows processes to communicate with each other. This
communication could involve a process letting another process know that some
event has occurred or transferring of data from one process to another.
67
Process Synchronization
Interprocess communication is the mechanism provided by the operating
system that allows processes to communicate with each other. This
communication could involve a process letting another process know that some
event has occurred or transferring of data from one process to another.
68
Process Synchronization
Shared Memory Model
Shared memory is the memory that can be simultaneously accessed by multiple
processes. This is done so that the processes can communicate with each other. All
POSIX systems, as well as Windows operating systems use shared memory.
Advantage of Shared Memory Model
Memory communication is faster on the shared memory model as compared to the
message passing model on the same machine.
Disadvantages of Shared Memory Model
Some of the disadvantages of shared memory model are as follows −
All the processes that use the shared memory model need to make sure that they
are not writing to the same memory location.
Shared memory model may create problems such as synchronization and memory
protection that need to be addressed.
69
Process Synchronization
70
Process Synchronization
Direct Communication:-
In this type of communication process, usually, a link is created or established
between two communicating processes. However, in every pair of
communicating processes, only one link can exist.
Indirect Communication
Indirect communication can only exist or be established when processes share
a common mailbox, and each pair of these processes shares multiple
communication links. These shared links can be unidirectional or bi-
directional.
71
Process Synchronization
72
Process Synchronization
73
Process Synchronization
74
Faculty Video Links, Youtube & NPTEL Video Links and Online
Courses Details
• https://www.youtube.com/watch?v=_zOTMOubT1M
• https://www.youtube.com/playlist?list=PLmXKhU9FNesSF
vj6gASuWmQd23Ul5omtD
• https://www.youtube.com/watch?v=x_UpLHXF9dU
• https://www.youtube.com/watch?v=cviEfwtdcEE
• https://nptel.ac.in/courses/106108101