0% found this document useful (0 votes)
9 views31 pages

Unit-02 Sempahore and 3 Classical

The document explains semaphores, which are integer variables used for process synchronization in operating systems, detailing their operations (WAIT and SIGNAL) and types (counting and binary semaphores). It discusses classical concurrency problems such as the Readers-Writers Problem, Dining Philosopher Problem, and Sleeping Barber Problem, providing solutions using semaphores to ensure mutual exclusion and prevent deadlock. The document includes code examples and conditions that must be met for each problem to ensure proper synchronization among processes.

Uploaded by

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

Unit-02 Sempahore and 3 Classical

The document explains semaphores, which are integer variables used for process synchronization in operating systems, detailing their operations (WAIT and SIGNAL) and types (counting and binary semaphores). It discusses classical concurrency problems such as the Readers-Writers Problem, Dining Philosopher Problem, and Sleeping Barber Problem, providing solutions using semaphores to ensure mutual exclusion and prevent deadlock. The document includes code examples and conditions that must be met for each problem to ensure proper synchronization among processes.

Uploaded by

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

SEMAPHORE

A SEMAPHORE BASICALLY CONSISTS OF AN INTEGER VARIABLE S, COMMONLY SHARED BY


PROCESS. S IS A PROTECTED VARIABLE THAN CAN ONLY BE ACCESSED AND MANIPULATED BY
TWO OPERATIONS-
WAIT() or P() or LOCK() or DOWN()
SIGNAL() or V() or Release() or UP()
EACH SEMAPHORE HAS A QUEUE ASSOCIATED WITH IT KNOWN AS SEMAPHORE QUEUE.
THE CONCEPT OF QUEUE ENSURES THAT NO PROCESS GO INTO BUSY WAITING (CONTINOUS LOOP).
CONTINOUS LOOP IS WASTAGE OF CPU CYCLE.

MOVING THE PROCESS TO THE SEMAPHORE QUEUE IS CALLED BLOCK OPERATION AND CHANGES THE STATE FROM
RUNNING TO WAITING STATE.

REMOVING THE PROCESS FROM QUEUE AND PLACING IT IN THE READY STATE IS CALLED WAKEUP.

BOTH BLOCK AND WAKEUP OPERATION PERFORMED BY THE OPERATING SYSTEM AS A BASIC SYSTEM CALL.
COUNTING SEMPAHORE
The value of a counting semaphore can range over an unrestricted domain.
• It is also known as general semaphore.
• The semaphore is initialized to the number of resources available. Each process that
• wishes to use a resource performs a wait() operation on the semaphore (thereby decrementing the count).
• When a process releases a resource, it performs a signal() operation (incrementing the count).
• When the count for the semaphore goes to 0, all resources are being used.
• After that, processes that wish to use a resource will block until the count becomes greater than 0.
BINARY SEMAPHORE
• The value of a binary semaphore can range only between 0 and 1.
• binary semaphores are known as mutex locks, as they are locks that provide mutual exclusion.
• Ιn this, queue is used to hold the processes waiting on the semaphore.
• FIFO, policy is used to remove the processes from the queue.
• binary semaphores are used to deal with the critical-section problem for multiple processes. The n processes share a semaphore,
mutex, initialized to 1.
Classical Problem in Concurrency
or Classical Problem in Synchronization
• Readers- Writers Problem
• Dining Philosopher Problem
• Sleeping Barber Problem
Reader-Writers Problem
• There is data containing some files, records, etc that is shared among the number
of concurrent processes.

• The processes that only read the data from that common shared data area are
called Reader Processes.

• The processes that perform write operation ( writing new data value or updating
the data value ) on the data stored in a common shared area are called writers
processes.
Various Conditions that need to take care in
Readers-writers case are:
• Any number of reader processes can simultaneously read the data from common
shared data.
( READ-READ ALLOWED)
• Only one Writer at a time may write to that common shared data area
( WRITE-WRITE NOT ALLOWED )
• If one of the writer process is writing the common shared data area, then no
reader process are allowed to read.
( WRITE-READ NOT ALLOWED )
• If there is at least one reader reading the common data area, no writer process
are allowed to write in that common data area.
( READ-WRITE NOT ALLOWED )
Reader-Writers Problem Solution Using
Semaphores

• The reader-writers problem solution consists of two semaphores


Mutex
Wrt
• Variable Number of Readers
Writer Process
• Initially, Mutex=1, wrt=1 and Number_of_Reader=0

While(1)
{
Wait(wrt); //Ensure mutual exclusion amon writers

<Critical Section> //write operation is performed hereby writer process

Signal(wrt); //Unblock either waiting writer process or any one waiting reading process

}
Conclusion of writer process

• The wait(wrt) function will reduce the value to “0” and this will block
other processes to enter critical section.

• The signal (wrt) function will be called and the value will be again set
to “1” and now other processes will be allowed to enter in critical
section
Reader Process
While (1)
{
Wait(mutex); //ensure mutual exclusion among readers
NumberofReaders++; //another reader process is there
if( numberof Readers==1)
{
wait(wrt); //block writer process, have to wait
}
Signal(mutex) //signal other waiting reader process
<critical section> //read operation is performed hereby reader process
Wait(mutex) //ensure mutual exclusion among readers
NumberofReaders--; //number of readers count is decremented by one
If (NumberofReaders==0) // last reader means all reader process has finished
{
Signal(wrt); //unblock waiting , writer process
}
Signal(mutex); //signal other waiting reader process
}
• We are using the MUTEX variable to change something in NumberofReader
variable. This is done because if some process is changing something in the
NumberofReader variable , then no other process should be allowed to use that
variable. So as to achieve mutual exclusion, we are using Mutex variable.

• Initially , we are calling the wait(mutex) function and this will reduce the value of
mutex by one. After that, the NumberofReader value will be incremented.

• If the NumberofReader is equal ==1, i.e. the reader process is the first process in
this case, no other process demanding the write operation will be allowed to
enter into the Critical Section. So the wait(wrt) will be called and the value of the
writer variable will be decreased to “0”. And no other process demanding for
write operation will be allowed.

• After changing the NumberofReader variable the value of the mutex variable will
be increased by one, so that other processes should be allowed to change the
value of NumberofReader value.
• The read operation by various processes will be continued and after that
when the read operation is done, then again we have to change the count,
the value of NumberofReaders & decreases the value by one.
• If the NumberofReaders becomes 0, then we have to increase the value of
the writer variable by one by calling the signal(wrt) Function. This is done
because if the NumberofReaders is 0 then other writer processes should be
allowed to enter into the CS to write data in file
Dining Philosopher Problem

• There is a dining room containing a circular table with five chairs.


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 and five


chopsticks for each philosopher.
• To eat a philosopher needs both their right and left chopstick. A philosopher can
only set 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 &
starts thinking again.

• Here philosophers are processes and chopsticks are shared data.

• The problem is to design a solution ( a concurrent algorithm ) such that mutual


exclusion among philosophers ( processes ) is there & also no philosophers will
starve to acquire chopsticks ( shared data)
• The chopstick (shared data) are represented using array of semaphores as:

Semaphore chopsticks[5];

Where chopsticks are initialized to 1.

The philosopher i ( Process i ) is represented by the following code where i can be


0 , 1 , 2 , 3 , or 4.
Dining Philosopher Code
do
{
wait ( chopstick [ i ] ) ; //pick up (acquire) left chopstick
wait ( chopstick [ ( i + 1 ) % 5 ] ) ; //pick up (right) left chopstick

<CRITICAL SECTION>

signal ( chopstick [ i ] ) ; //put down left chopstick


signal ( chopstick [ ( i + 1 ) % 5 ] ) ; //put down right chopstick

think

} while ( true );
Solution to Dining Philosopher
Let value of i = 0( initial value ) &
Semaphore chopstick[i]=1

For Philosopher-0 (Process-0)


• Suppose Philosopher P0 wants to eat, it will enter in Philosopher() function, and
execute Wait( take_chopstickC[i] ); by doing this it holds C0 chopstick and
reduces semaphore C0 to 0,
• after that it execute Wait( take_chopstickC[(i+1) % 5] ); by doing this it
holds C1 chopstick( since i =0, therefore (0 + 1) % 5 = 1) and reduces semaphore
C1 to 0
For Philosopher-1 (Process-1)

• Suppose Philosopher P1 wants to eat, it will enter in Philosopher() function, and


execute Wait( take_chopstickC[i] ); by doing this it will try to hold C1
chopstick but will not be able to do that, since the value of semaphore C1 has
already been set to 0 by philosopher P0,
• Therefore it will enter into an infinite loop because of which philosopher P1 will
not be able to pick chopstick C1
For Philosopher-2 (Process-2)

• Philosopher P2 wants to eat, it will enter in Philosopher() function, and


execute Wait( take_chopstickC[i] ); by doing this it holds C2 chopstick and
reduces semaphore C2 to 0,
• After that, it executes Wait( take_chopstickC[(i+1) % 5] ); by doing this it
holds C3 chopstick( since i =2, therefore (2 + 1) % 5 = 3) and reduces semaphore
C3 to 0.
Conclusion
• A philosopher can only eat if both immediate left and right chopsticks of the
philosopher are available else philosopher needs to wait. Also at one go two
independent philosophers can eat simultaneously (i.e., philosopher P0 and P2, P1
and P3 & P2 and P4 can eat simultaneously as all are the independent processes
and they are following the above constraint of dining philosopher problem)
Problem with Dining Philosopher

• No two neighboring philosophers can eat at the same point in time.

• 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.
To avoid deadlock, some of the solutions are as follows -

• A Maximum number of philosophers on the table should not be more than four.

• If both the chopsticks ( left and right ) are available at the same time, only then a
philosopher should be allowed to pick their chopsticks.

• A philosopher at an even position should pick the right chopstick and then the left
chopstick while a philosopher at an odd position should pick the left chopstick and
then the right chopstick.
SLEEPING BARBER PROBLEM
• There is a barber shop having
one barber,
one barber chair
N chairs for waiting customer

Various Conditions that need to take care in sleeping barber problem case are:

• If there are no customers in the shop, the barber goes to sleep.


• When a customer enters the shop, the customer wakes up the barber.
• When more customers enters the shop while barber is cutting a customer’s hair,
they sits on one of the empty chairs ( if there are empty chair available ) or the
customer leaves if all the chairs are occupied
Variables used and their initial values:
• Typedef int semaphore ;

• Semaphore customer = 0 ; //no of customer waiting for hair cut

• Sempahore barber = 0 ; //Barber waiting for customer

• Semaphore mutex = 1 ;
/* for mutual exclusion among chairs available. The variable
NumberofEmptyChairs will be incremented or decremented only after acquiring
the semaphore mutex */

• int NumberofEmptyChairs = N ;
/* Total number of seats available at barber shop for customer*/
Barber Process
While(1)
{
wait(customers); //wait for customers and go to sleep if number of customers is 0
wait(mutex); //mutex protect the number of available seats,
means lock chairs so as to got one free chair as a customer had arrived in the shop

NumberofEmptyChairs ++; // one chair gets empty

signal(mutex); //got one empty chair so release the mutex on the chairs, no need to lock chairs anymore
signal(barber); //barber is ready to cut hair
}

<CRITICAL SECTION>
Customers Process
While (1)
{
Wait(mutex); //mutex protect the no of available seats, means the customer tries to get a empty chair
If(NumberofEmptyChairs>0) //if any chair is available
{
NumberofEmptyChairs--; //sit down on a chair & decremented the count by one
signal(mutex); //got one empty chair to release the mutex on the chair no need to lock the chairs anymore
signal(customers); //notify the barber
wait(barber); //wait if barber is busy
}
else //there are no empty seats available
{
signal(mutex); //release the mutex on chairs
} //customer leaves the shop without a haircut due to unavailability of chair
}

You might also like