Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 45

UNIT-2: CONCURENT PROCESS

Operating System Concepts with Java – 8th Edition 6.1 Silberschatz, Galvin and Gagne
UNIT-2: CONCURRENT PROCESS

 Principle of concurrency
 IPC
 IPC models
 1.shared memory
 2. message passing
 The Critical-Section Problem
 Peterson’s Solution
 Dekkers solution

Operating System Concepts with Java – 8th Edition 6.2 Silberschatz, Galvin and Gagne
CONCURRENY
 Concurrency in operating systems refers to the ability of an
operating system to handle multiple tasks or processes at the
same time.
 With the increasing demand for high performance computing,
concurrency has become a critical aspect of modern computing
systems.
Principles of Concurrency
 The principles of concurrency in operating systems are
designed to ensure that multiple processes or threads can
execute efficiently and effectively, without interfering with
each other or causing deadlock.
 Operating systems that support concurrency can execute
multiple tasks simultaneously, leading to better resource
utilization, improved responsiveness, and enhanced user
experience
 However, concurrency also introduces new challenges such as
race conditions, deadlocks, and priority inversion, which need
to be managed effectively to ensure the stability and reliability
of the system.

Operating System Concepts with Java – 8th Edition 6.3 Silberschatz, Galvin and Gagne
Interprocess Communication
 Processes within a system may be independent or
cooperating
 Independent process cannot affect or be affected by the
execution of another process
 Cooperating process can affect or be affected by other
processes, including sharing data
 Reasons for cooperating processes:
 Information sharing
 Computation speedup
 Modularity
 Convenience
 Cooperating processes need interprocess
communication (IPC)
 Two models of IPC
 Shared memory
 Message passing 6.4
Operating System Concepts with Java – 8th Edition Silberschatz, Galvin and Gagne
IPC Models

Operating System Concepts with Java – 8th Edition 6.5 Silberschatz, Galvin and Gagne
Interprocess Communication – Shared Memory

 An area of memory shared among the processes that


wish to communicate.
 The communication is under the control of the users
processes not the operating system.
 Shared memory system may lea to condition called Race
condition
 A Race Condition is a situation that occurs when two or
more threads or processes access a shared resource,
such as a file or a variable, at the same time.
 Race condition may lead to starvation,deadlock or datain
consistency
 Major issues is to provide mechanism that will allow the
user processes to synchronize their actions when they
access shared memory[Mutual Exclusion]

Operating System Concepts with Java – 8th Edition 6.6 Silberschatz, Galvin and Gagne
Interprocess Communication – Message Passing

 Mechanism for processes to communicate and to synchronize their


actions .
 Message system processes communicate with each other without
resorting to shared variables
 IPC facility provides two operations:
 send(message)
 receive(message)
 The message size is either fixed or variable
 If processes P and Q wish to communicate, they need to:
 Establish a communication link between them
 Exchange messages via send/receive
 Implementation issues:
 How are links established?
 Can a link be associated with more than two processes?
 How many links can there be between every pair of communicating
processes?
 What is the capacity of a link?
 Is the size of a message that the link can accommodate fixed or
variable?
 Is a link unidirectional or bi-directional?

Operating System Concepts with Java – 8th Edition 6.7 Silberschatz, Galvin and Gagne
 Implementation of communication link
 Physical:
 Shared memory
 Hardware bus
 Network
 Logical:
 Direct or indirect
 Synchronous or asynchronous
 Automatic or explicit buffering

Operating System Concepts with Java – 8th Edition 6.8 Silberschatz, Galvin and Gagne
Direct Communication
 Processes must name each other explicitly:
 send (P, message) – send a message to process P
 receive(Q, message) – receive a message from
process Q
 Properties of communication link
 Links are established automatically
 A link is associated with exactly one pair of
communicating processes
 Between each pair there exists exactly one link
 The link may be unidirectional, but is usually bi-
directional

Operating System Concepts with Java – 8th Edition 6.9 Silberschatz, Galvin and Gagne
Indirect Communication
 Messages are directed and received from mailboxes (also referred
to as ports)
 Each mailbox has a unique id
 Processes can communicate only if they share a mailbox
 Properties of communication link
 Link established only if processes share a common mailbox
 A link may be associated with many processes
 Each pair of processes may share several communication links
 Link may be unidirectional or bi-directional
 Operations
 create a new mailbox (port)
 send and receive messages through mailbox
 destroy a mailbox
 Primitives are defined as:
send(A, message) – send a message to mailbox A
receive(A, message) – receive a message from mailbox A

Operating System Concepts with Java – 8th Edition 6.10 Silberschatz, Galvin and Gagne
Synchronization
 Message passing may be either blocking or non-blocking
 Blocking is considered synchronous
 Blocking send -- the sender is blocked until the
message is received
 Blocking receive -- the receiver is blocked until a
message is available
 Non-blocking is considered asynchronous
 Non-blocking send -- the sender sends the message
and continue
 Non-blocking receive -- the receiver receives:
 A valid message, or
 Null message
 Different combinations possible
 If both send and receive are blocking, we have a
rendezvous

Operating System Concepts with Java – 8th Edition 6.11 Silberschatz, Galvin and Gagne
Inter process synchronization
 Synchronization is an essential part of interprocess
communication. It refers to a case where the data used
to communicate between processors is control
information.
 It is either given by the interprocess control mechanism
or handled by the communicating processes.
 In a multi-process system, synchronization is necessary
to ensure data consistency and integrity, and to avoid
the risk of deadlocks and other synchronization
problems.
 Process synchronization problem arises in the case of
Cooperative process also because resources are shared
in Cooperative processes.

Operating System Concepts with Java – 8th Edition 6.12 Silberschatz, Galvin and Gagne
Race Condition
 When more than one process is executing the same code or
accessing the same memory or any shared variable in that
condition there is a possibility that the output or the value of
the shared variable is wrong so for that all the processes doing
the race to say that my output is correct this condition known
as a race condition.
 Several processes access and process the manipulations over
the same data concurrently, then the outcome depends on the
particular order in which the access takes place.
 A race condition is a situation that may occur inside a critical
section. This happens when the result of multiple thread
execution in the critical section differs according to the order
in which the threads execute.
 Race conditions in critical sections can be avoided if the
critical section is treated as an atomic instruction. Also, proper
thread synchronization using locks or atomic variables can
prevent race conditions.

Operating System Concepts with Java – 8th Edition 6.13 Silberschatz, Galvin and Gagne
Critical Section Problem
 A critical section is a code segment that can be accessed by only
one process at a time.
 The critical section contains shared variables that need to be
synchronized to maintain the consistency of data variables.
 So the critical section problem means designing a way for
cooperative processes to access shared resources without
creating data inconsistencies.
 There can be only one process in the critical section at a time.
 In the above diagram, the entry section handles the entry into the
critical section. It acquires the resources needed for execution by
the process. The exit section handles the exit from the critical
section. It releases the resources and also informs the other
processes that the critical section is free.

Operating System Concepts with Java – 8th Edition 6.14 Silberschatz, Galvin and Gagne
Solutions to the Critical Section Problem
 A solution developed for the critical section should have
the following properties
 Mutual Exclusion: If a process is executing in its critical
section, then no other process is allowed to execute in
the critical section.
 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.
 Some of the software-based solutions for critical section
problems are Peterson's solution,Dekker’s
solution semaphores, monitors. Some of the hardware-
based solutions for the critical section problem involve
atomic instructions such as TestAndSet,compare and
swap, Unlock and Lock.
Operating System Concepts with Java – 8th Edition 6.15 Silberschatz, Galvin and Gagne
Peterson’s Solution
 Peterson’s Solution is a classical software-based solution to
the critical section problem. In Peterson’s solution, we have
two shared variables:
 boolean flag[2]: The flag array is used to indicate if a process
is ready to enter the critical section Initialized to FALSE,
initially no one is interested in entering the critical section
 int turn: The process whose turn is to enter the critical
section.

Operating System Concepts with Java – 8th Edition 6.16 Silberschatz, Galvin and Gagne
 Peterson’s Solution preserves all three conditions:
 Mutual Exclusion is assured as only one process can access
the critical section at any time.
 Progress is also assured, as a process outside the critical
section does not block other processes from entering the
critical section.
 Bounded Waiting is preserved as every process gets a fair
chance.
 Disadvantages of Peterson’s solution:
 It involves busy waiting.(In the Peterson’s solution, the code
statement- “while(flag[j] && turn == j);” is responsible for
this. Busy waiting is not favored because it wastes CPU
cycles that could be used to perform other tasks.)
 It is limited to 2 processes.
 Peterson’s solution cannot be used in modern CPU
architectures.

Operating System Concepts with Java – 8th Edition 6.17 Silberschatz, Galvin and Gagne
Dekker’solution
 Dekker was a Dutch mathematician who introduced a software-
based solution for the mutual exclusion problem. The Dekker’s
algorithm was developed for an algorithm for mutual exclusion
between two processes.
 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 [Mutual Exclusion , progress,
Bounded Waiting] and is the most efficient among all of them.
 Ist attempt: If one pocess fails other cannot get in to critical
section(violates progress property)
 IInd attempt: violates mutual exclusion
 IIIrd attempt: give rise to deadlock
 IVrth attempt: tries to resolve the issue of deadlock
 Vth attempt: final solution satisfying all three property.
 Two data structures are used
 1. int turn – global var which defines the process whose turn is to
enter the critical section.
 2. boolean flag[2] - The flag array
6.18
is used to indicate if a process is
Operating System Concepts with Java – 8th Edition Silberschatz, Galvin and Gagne
P0 process P1 process
do{ do{
flag[0] = true; flag[1] = true;
while (flag[1] ) while (flag[0] )
{ if turn = 1; { if turn = 0;
{ flag[0] = false; { flag[1] = false;
while(turn == 1 ); while (turn == 0);
flag[0] = true; flag[1] = true;
} }
} }
critical section critical section
turn = 1; turn = 0;
flag[0] = false; flag[1] = false;
Remainder section; Remainder section;
}while(true);
Operating System Concepts with Java – 8th Edition 6.19
} while(true); Silberschatz, Galvin and Gagne
Synchronization Hardware
 Many systems provide hardware support for critical
section code

 Uniprocessors – could disable interrupts


 Currently running code would execute without
preemption
 Generally too inefficient on multiprocessor systems
 Operating systems using this not broadly scalable

 Modern machines provide special atomic hardware


instructions
 Atomic = non-interruptable
 Either test memory word and set value
 Or swap contents of two memory words

Operating System Concepts with Java – 8th Edition 6.20 Silberschatz, Galvin and Gagne
Test and set (hardware synchronization)
 Test and Set Lock (TSL) is a synchronization mechanism.
 It uses a test and set instruction to provide the synchronization among
the processes executing concurrently.
 It is an instruction that returns the old value of a memory location and
sets the memory location value to 1 as a single atomic operation.
 If one process is currently executing a test-and-set, no other process is
allowed to begin another test-and-set until the first process test-and-set
is finished.
 It is implemented as-

 Lock value = 0 means the critical section is currently vacant and no process is
present inside it.
 Lock value = 1 means the critical section is currently occupied and a process is
present inside it.

Operating System Concepts with Java – 8th Edition 6.21 Silberschatz, Galvin and Gagne
 The characteristics of this test and set synchronization
mechanism are-
 It ensures mutual exclusion.
 It is deadlock free.
 It does not guarantee bounded waiting and may cause
starvation.
 It suffers from spin lock.
 It is not architectural neutral since it requires the
operating system to support test-and-set instruction.
 It is a busy waiting solution which keeps the CPU
busy when the process is actually waiting.

Operating System Concepts with Java – 8th Edition 6.22 Silberschatz, Galvin and Gagne
Semaphores
 Method to prevent race condition.
 Semaphores are integer variables 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.
wait(S)
{ while (S<=0);
S--;
}
Signal : The signal operation increments the value of its argument
S.
signal(S)
{
S++;

}
Operating System Concepts with Java – 8th Edition 6.23 Silberschatz, Galvin and Gagne
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. These semaphores are used to coordinate the
resource access, where the semaphore count is the number of
available resources. 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. Also known as
mutex locks

Operating System Concepts with Java – 8th Edition 6.24 Silberschatz, Galvin and Gagne
 Advantages of Semaphores
 Semaphores allow only one process into the critical section. They
follow the mutual exclusion principle strictly and are much more
efficient than some other methods of synchronization.
 There is no resource wastage because of busy waiting in
semaphores as processor time is not wasted unnecessarily to
check if a condition is fulfilled to allow a process to access the
critical section.
 Semaphores are implemented in the machine independent code of
the microkernel. So they are machine independent.
 Disadvantages of Semaphores
 Semaphores are complicated so the wait and signal operations
must be implemented in the correct order to prevent deadlocks.
 Semaphores are impractical for last scale use as their use leads to
loss of modularity. This happens because the wait and signal
operations prevent the creation of a structured layout for the
system.
 Semaphores may lead to a priority inversion where low priority
processes may access the critical section first and high priority
processes
Operating System Concepts with late
th
Java – 8 Edition 6.25 Silberschatz, Galvin and Gagne
 A counting semaphore S is initialized to 10. Then, 6 P
operations and 4 V operations are performed on S. What is
the final value of S?
 Soution:
 P operation also called as wait operation decrements the
value of semaphore variable by 1.
 V operation also called as signal operation increments the
value of semaphore variable by 1.

 Thus,
 Final value of semaphore variable S
 = 10 – (6 x 1) + (4 x 1)
 = 10 – 6 + 4
 =8

Operating System Concepts with Java – 8th Edition 6.26 Silberschatz, Galvin and Gagne
Classical Problems of Synchronization
 Producer consumer Problem

 Readers and Writers Problem

 Dining-Philosophers Problem
 Sleeping barber problem

Operating System Concepts with Java – 8th Edition 6.27 Silberschatz, Galvin and Gagne
Producer-Consumer Problem
 The Producer-Consumer problem is a classical multi-process
synchronization problem, that is we are trying to achieve
synchronization between more than one process.
 There is one Producer who is producing some items, whereas there
is one Consumer that is consuming the items produced by the
Producer.
 The same memory buffer is shared by both producers and
consumers which is of fixed-size.
 The task of the Producer is to produce the item, put it into the
memory buffer, and again start producing items. Whereas the task of
the Consumer is to consume the item from the memory buffer.
 The producer should produce data only when the buffer is not full. In
case it is found that the buffer is full, the producer is not allowed to
store any data into the memory buffer.
 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.
 Accessing memory buffer should not be allowed to producer and
Operating System Concepts with Java – 8th Edition 6.28 Silberschatz, Galvin and Gagne
Producer-Consumer Problem
 Producer Code  Consumer Code
 {  {
 int itemp;  int itemc;
 while( true ) {  while( true ) {
 Produceitem(itemp);  while(count==0);//buffer empty
; /* Do nothing */
while(count==n);//buffer full
; /* Do nothing */ itemc= buffer[ out ]
out = ( out + 1 ) % n;
buffer[ in ] = itemp;  Count=count-1; ;//[Ist -> load
in = ( in + 1 ) % n; Rp,m[count]
 Count=count+1;//[Ist -> load  Iind-> INCR Rp;
Rp,m[count]
 IIIrd ->store
 } Iind-> INCR Rp; m[count], Rp;
 IIIrd ->store
m[count], Rp;  Consumeditem(itemc);
 }

Operating System Concepts with Java – 8th Edition 6.29 Silberschatz, Galvin and Gagne
Producer-Consumer Problem(using semaphores)

 void producer( void )  void consumer(void)


{
wait ( empty ); {
wait(S); wait ( empty );
Produce_item(item P)
wait(S);
buffer[ in ] = item P; itemC = buffer[ out
in = (in + 1)mod n
];
signal(S); out = ( out + 1 )
signal(full);
mod n;
signal(S);
} signal(empty);
Operating System Concepts with Java – 8th Edition 6.30 Silberschatz, Galvin and Gagne
THE DINING PHILOSOPHERS PROBLEM
 The dining philosopher's problem is the classical problem
of synchronization .
 Five philosophers are sitting around a circular table and
their job is to think and eat alternatively.
 A bowl of noodles is placed at the center of the table
along with five chopsticks for each of the philosophers.
 To eat a philosopher needs both their right and a left
chopstick.
 A philosopher can only eat if both immediate left and
right chopsticks of the philosopher is available. In case if
both immediate left and right chopsticks of the
philosopher are not available then the philosopher puts
down their (either left or right) chopstick and starts
thinking again.
 To eat a philosopher PICKS left first then right.

Operating System Concepts with Java – 8th Edition 6.31 Silberschatz, Galvin and Gagne
Dining-Philosophers Problem

 Shared data
 Bowl of rice (data set)
 Semaphore chopStick [5] initialized to 1

Operating System Concepts with Java – 8th Edition 6.32 Silberschatz, Galvin and Gagne
THE DINING PHILOSOPHERS PROBLEM
 Void Philosopher
 {
 while(1)
 {
 take_chopstick[i];
 take_chopstick[ (i+1) % 5] ;

 . .
 . EATING THE NOODLE
 .
 put_chopstick[i] );
 put_chopstick[ (i+1) % 5] ;
 .
 . THINKING
 }
 }

Operating System Concepts with Java – 8th Edition 6.33 Silberschatz, Galvin and Gagne
THE DINING PHILOSOPHERS PROBLEM
 Suppose Philosopher P0 wants to eat, it will enter in
Philosopher() function, and
execute take_chopstick[i]; by doing this it holds C0
chopstick after that it execute take_chopstick[ (i+1) %
5]; by doing this it holds C1 chopstick( since i =0,
therefore (0 + 1) % 5 = 1)
 Similarly suppose now Philosopher P1 wants to eat, it
will enter in Philosopher() function, and
execute take_chopstick[i]; by doing this it holds C1
chopstick after that it execute take_chopstick[ (i+1) %
5]; by doing this it holds C2 chopstick( since i =1,
therefore (1 + 1) % 5 = 2)
 But Practically Chopstick C1 is not available as it has
already been taken by philosopher P0, hence the above
code generates problems and produces race condition.

Operating System Concepts with Java – 8th Edition 6.34 Silberschatz, Galvin and Gagne
THE DINING PHILOSOPHERS PROBLEM
 The solution of the Dining Philosophers Problem
 We use a semaphore to represent a chopstick and this truly acts as a
solution of the Dining Philosophers Problem. Wait and Signal
operations will be used for the solution of the Dining Philosophers
Problem, for picking a chopstick wait operation can be executed
while for releasing a chopstick signal semaphore can be executed.
 void Philosopher
 {
 while(1)
 {
 Wait( take_chopstickC[i] );
 Wait( take_chopstickC[(i+1) % 5] ) ;
 . .
 . EATING THE NOODLE
 .
 Signal( put_chopstickC[i] );
 Signal( put_chopstickC[ (i+1) % 5] ) ;
 .
 . THINKING
Operating
 System
} Concepts with Java – 8th Edition 6.35 Silberschatz, Galvin and Gagne
THE DINING PHILOSOPHERS PROBLEM
 From the above solution of the dining philosopher problem, we have
proved that no two neighboring philosophers can eat at the same
point in time.
 But the drawback of the above solution is that this solution can lead
to a deadlock condition. This situation happens if all the
philosophers pick their left chopstick at the same time, which leads
to the condition of deadlock and none of the philosophers can eat.
 Solution of deadlock
 Maximum number of philosophers on the table should not be more
than four.
 Only in case if both the chopsticks ( left and right ) are available at
the same time, only then a philosopher should be allowed to pick
their chopsticks
 All the four starting philosophers ( P0, P1, P2, and P3) should pick
the left chopstick and then the right chopstick, whereas the last
philosopher P4 should pick the right chopstick and then the left
chopstick.
Operating System Concepts with Java – 8th Edition 6.36 Silberschatz, Galvin and Gagne
SLEEPING BARBER PROBLEM
 The Sleeping Barber problem is a classic problem in
process synchronization.
 The sleeping barber dilemma was first posed by Dijkstra
in 1965.
 In this problem, the barbershop has one barber, one
barber chair, and a waiting room with N chairs for the
customers..
 If there is no customer, then the barber sleeps in his own
chair.
 When a customer arrives, he has to wake up the barber.
 If there are many customers and the barber is cutting a
customer’s hair, then the remaining customers either
wait if there are empty chairs in the waiting room or they
leave if no chairs are empty.

Operating System Concepts with Java – 8th Edition 6.37 Silberschatz, Galvin and Gagne
SLEEPING BARBER PROBLEM

Operating System Concepts with Java – 8th Edition 6.38 Silberschatz, Galvin and Gagne
SLEEPING BARBER PROBLEM

Operating System Concepts with Java – 8th Edition 6.39 Silberschatz, Galvin and Gagne
SLEEPING BARBER PROBLEM
 Solution:
 Three semaphores are used in the solution to this issue.
 CUSTOMERS : First is for the customer which counts the number
of customers present in the waiting room (customer in the barber
chair is not included because he is not waiting).
 Barber : Second, the barber 0 or 1 is used to tell whether the
barber is idle or is working
 And the third mutex is used to provide the mutual exclusion
which is required for the process to execute..
 Semaphore Customers = 0;
 Semaphore Barber = 0;
 Mutex Seats = 1;

Operating System Concepts with Java – 8th Edition 6.40 Silberschatz, Galvin and Gagne
SLEEPING BARBER PROBLEM
 When the barber shows up in the morning, he executes the
procedure barber, causing him to block on the semaphore
customers because it is initially 0. Then the barber goes to
sleep until the first customer comes up.
 When a customer arrives, he executes customer procedure
the customer acquires the mutex for entering the critical
region, if another customer enters thereafter, the second
one will not be able to anything until the first one has
released the mutex.
 The customer then checks the chairs in the waiting room if
waiting customers are less then the number of chairs then
he sits otherwise he leaves and releases the mutex.
 If the chair is available then customer sits in the waiting
room and increments the variable waiting value and also
increases the customer’s semaphore this wakes up the
barber if he is sleeping.
 At this point, customer and barber are both awake and the
barber is ready to give that person a haircut. When the
haircut is over, the customer exits the procedure and if there
are no customers in waiting room barber sleeps.
Operating System Concepts with Java – 8th Edition 6.41 Silberschatz, Galvin and Gagne
Barber { Customer {
while(true) { while(true)
/* waits for a customer { /* protects seats so only 1 customer tries to sit
(sleeps). */ in a chair if that's the case.*/
down(Customers);
down(Seats); //This line should not
be here.
/* mutex to protect the number of available if(FreeSeats > 0) {
seats.*/
/* sitting down.*/
down(Seats);
FreeSeats--;
/* notify the barber. */
/* a chair gets free.*/
up(Customers);
FreeSeats++;
/* release the lock */
up(Seats);
/* bring customer for haircut.*/
/* wait in the waiting room if barber is
up(Barber); busy. */
down(Barber);
/* release the mutex on the // customer is having hair
chair.*/
cut}
up(Seats);
} else { /* release the lock */
/* barber is cutting hair.*/
up(Seats);
}
// customer leaves }
}
}

Operating System Concepts with Java – 8th Edition }
6.42 Silberschatz, Galvin and Gagne
Operating System Concepts with Java – 8th Edition 6.43 Silberschatz, Galvin and Gagne
Operating System Concepts with Java – 8th Edition 6.44 Silberschatz, Galvin and Gagne
End of Chapter 6

Operating System Concepts with Java – 8th Edition 6.45 Silberschatz, Galvin and Gagne

You might also like