Unit-02 Sempahore and 3 Classical
Unit-02 Sempahore and 3 Classical
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
While(1)
{
Wait(wrt); //Ensure mutual exclusion amon writers
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
• 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.
Semaphore chopsticks[5];
<CRITICAL SECTION>
think
} while ( true );
Solution to Dining Philosopher
Let value of i = 0( initial value ) &
Semaphore chopstick[i]=1
• 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:
• 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
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
}