2.3. Interprocesscommunication
2.3. Interprocesscommunication
ic h
wh
n
c tio
l se
ti ca tion
c ri n di
o f co
o n ce
c uti o ra
e t
E x ad s
le
Process Pi:
repeat
disable interrupts
critical section
enable interrupts
remainder section
forever
Lock variable
do {
acquire lock
critical section
release lock
remainder section
}
while (TRUE);
When process 0 leaves the critical region, it sets turn to 1, to allow process 1 to enter its critical
region.
Suppose that process 1 finishes its critical region quickly, so both processes are in their noncritical
regions, with turn set to 0. Now process 0 executes its whole loop quickly, exiting its critical region and
setting turn to 1. At this point turn is 1 and both processes are executing in their noncritical regions.
Suddenly, process 0 finishes its noncritical region and goes back to the top of its loop. Unfortunately, it
is not permitted to enter its critical region now, because turn is 1 and process 1 is busy with its
noncritical region. It hangs in its while loop until process 1 sets turn to 0.
So while this algorithm does avoid all races, it is not really a serious
candidate as a solution because it violates condition 3.
while (TRUE) {
while (TRUE){ while(turn != 1)
while(turn != 0) critical_region();
critical_region(); turn = 1; turn = 0;
noncritical_region();
noncritical_region(); }
}
02/04/2025 Bahir Dar University OS(CoSc2034) 36
Peterson’s Solution
• It is two process solution #define FALSE 0
• it consists of two procedures written in #define TRUE 1
ANSI C. #define N 2
• Each process calls enter_region with its
own process number, 0 or 1, as int turn;
parameter to access shared data.
• the process calls leave_region to int interested[N
indicate that it is completed accessing void enter_ region(int process);
shared data and to allow the other
process to enter, if it so desires. {
int other
• The two processes share two variables:
other = 1 - process;
• int turn;
• Boolean flag[2]
interested[process] = TRUE;
• The variable turn indicates whose turn it turn = other;
is to enter the critical section. while (turn==process &&
• The flag array is used to indicate if a interested[other]==TRUE;
process is ready to enter the critical }
section. flag[i] = true implies that
process Pi is ready!
Void leave_region(int process
02/04/2025 Bahir Dar University OS(CoSc2034) 37
{
Semaphore
o Semaphore is special variable which is used for signaling.
o Two and more processes can cooperate by means of simple signals, such that a process
is forced to stop at a specified place until it has received a specific signal.
o If a process is waiting for a signal, it is suspended until that signal is sent
o Semaphore has an integer value that may be initialized to a nonnegative number
o Two standard operations are used on semaphore: wait() and signal()
o The wait operation on a semaphore checks to see if the value is greater than 0. If so, it
decrements the value and just continues. If the value is 0, the process is block without
completing the wait operation for the moment.
o The signal operation increments the value of the semaphore addressed. If one or more
processes were sleeping on that semaphore, unable to complete an earlier wait
operation, one of them is chosen by the system (e.g., at random) and is allowed to
complete its wait.
o Wait and signal operations indivisible atomic action which means the operation cannot
be interrupted
o Queue is used to hold processes waiting on the semaphore
02/04/2025 Bahir Dar University OS(CoSc2034) 38
Semaphore(con’t…)
o Semaphore can be :
Counting semaphore – integer value can range over an unrestricted
domain
Binary semaphore – integer value can range only between 0 and 1;
can be simpler to implement. Also known as mutex locks
Binary semaphore used to provides mutual exclusion
o Must guarantee that no two processes can execute wait () and signal () on
the same semaphore at the same time.
o Thus, implementation becomes the critical section problem where the
wait and signal code are placed in the critical section.
o One bad (or malicious) process can fail the entire collection of
processes.
o Monitor defined so far not sufficiently powerful for modeling some synchronization
scheme.
o Conditional constructs mechanism is used to provide such synchronization.
o Conditional variable is defined like:condition x, y;
o The operations that invoked on condition variable are:
o x.wait () – a process that invokes the operation is suspended.
o x.signal () – resumes one of processes (if any) that invoked x.wait ()
void consumer(void)
{
int item;
while (TRUE) { /* infinite loop */
down(&full); /* decrement full count */
ode
down(&mutex); /* enter critical region */ e rc
um
item = remove_ item( ); /* take item from buffer */ ons
c
up(&mutex); /* leave critical region */
up(&empty); /* increment count of empty slots */
consume_item(item); /* do something with the item */
}
}
procedure consumer;
Begin
while true do
Begin
item = ProducerConsumer.remove;
consume _item( item)
end;
end;
02/04/2025 Bahir Dar University OS(CoSc2034) 50
Readers and writers problem
o There is a data area shared among a number of processes.
o The data area could be a file, a block of main memory, or even a bank of
processor registers.
o There are a number of processes that only read the data area (readers)
and a number that only write to the data area (writers).
The conditions that must be satisfied are as follows:
1. Any number of readers may simultaneously read the file.
2. Only one writer at a time may write to the file.
3. If a writer is writing to the file, no reader may read it.
The readers and writers problem have several variation.
First reader-writer problem: No reader will be kept waiting unless writer
has a permission to access the shared object.
Second readers -writers problem: Once reader ready, that writer perform
its write a soon as possible.
o in next slide the solution for the first readers –writers problem is
02/04/2025
presented. Bahir Dar University OS(CoSc2034) 51
Readers and writers problem
Readers and consumers problem using semaphore Writers code
int readcount=0; /* number of readers concurrently use shared
object void writer()
semaphore mutex= 1; /* provide mutual exclusion for readcount {
Semaphore wrt= 1; /* provide mutual exclusion for readers while (true){
void reader()
Wait (wrt);
{
while (true){
WRITEUNIT();
Wait(mutex); Signal (wrt);
readcount++; }
if(readcount == 1) }
wait(wrt);
signal (mutex);
READUNIT();
wait (mutex);
Readcount--;
if(readcount == 0) Readers code
signal (wrt);
signal (mutex);
}
}
02/04/2025 Bahir Dar University OS(CoSc2034) 52
End
Assignment:
1. Write semaphore and monitor based solution for second readers-
writers problem(writer priority)
2. Write semaphore and monitor based solution for Dining
philosopher problem.
Deadline for assignment submission: 26/04/2016
Assignment is done with in three group.