Semaphores in Operating System:


Semaphores are integer variables used by processes to send signals to other processes. Semaphores can only be accessed by two operations:


P (Wait or Down)


V (Signal or Up)


The definition of wait is as follows:


Wait(S)


{


while (S <= 0); //do nothing


S--;


}


The definition of signal is as follows:
Signal (S)


{


S++;


}


Any change in the value of semaphore, in wait and signal operations, must be executed indivisibly. If one process is changing the value of a semaphore, no other process is allowed to make any change simultaneously.


Use of Semaphores:


Semaphores are used to solve the critical section problem for n processes. The processes use a shared semaphore mutex initialized to 1. The implementation of semaphore for mutual exclusion is as follows:


while (1)


{


wait (mutex);


critical section;


signal (mutex);


remainder section;


}


How it Works?


Here, mutex is a shared semaphore that is initialized to 1. Suppose P0 executes wait operation on semaphore. It can enter its critical section because the value of mutex is 1. It will be decreased by 1 and P0 will enter its critical section. At this point, if Pi also tries to enter its critical section and executes wait operation, it will have to wait because the value of mutex is now 0. It will wait until P0 execute signal operation and increases the value of mutex by 1. So, only one process at a time can enter its critical section, which guarantees mutual exclusion.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Blog Archive

Powered by Blogger.

- Copyright © 2013 Taqi Shah Blogspot -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -