User:Dennisritchie123

Semaphores
Semaphores are the classic method for restricting access to shared resources (e.g. storage) in a multi-processing environment. They were invented by Dijkstra.

A semaphore is a protected variable(or ADT) which can only be accessed using the following operations:

P(s) Semaphore s;          { while (s==0);   /* wait until s>0 */ s=s-1; }         V(s) Semaphore s;         { s=s+1; }         Init(s, v)          Semaphore s;          Int v;          { s=v; }

P and V stand for Dutch “Proberen” to test and “Verhogen” to increment.

The value of a semaphore is the number of units of the resources which are free. If there is only one resource a “binary semaphore” with values 0 or 1 is used.

The P operation busy-waits or sleeps until a resource is available whereupon it immediately claims one. V is the inverse, it simply makes a resource available again after the process has finished using it. Init is only used to initialize the semaphore before any requests are made.

No other process can access the semaphore when P or V are executing.

To avoid busy waiting, a semaphore may have an associated queue of processes(FIFO). If a process does a P on a semaphore which is zero the process is added to the semaphore’s queue. When another process increments the semaphore by doing a V and there are tasks on the queue, one is taken off and resumed.

We can implement mutual-exclusion using semaphores:

P(s) - wait(s), V(s) – signal(s)

do { wait (s); critical section; signal (s); }while(1);

Let us take semaphore value to 1;

If process P1 wants to enter its critical section it has to wait. The value of s is decremented to 0. It signals after executing its critical section. The value of s is incremented to 1.

Other process P2 wants to enter its critical section while P1 is in its critical section, it uses the shared semaphore value 0 and performs wait operation first. P2 continues to loop in the while until P1 executes signal(s);

So, only one process will be executing at a time.

Disadvantage
The main disadvantage here is that processes require busy-waiting (looping in while). This continual looping is clearly a problem in a real multiprogramming system (where a single CPU is shared among multiple processes). Busy waiting wastes CPU cycles that some other may use them. This type of semaphore is called spinlock.

Implementation
To overcome the need for busy waiting, we can modify the definition of wait and signal semaphore operations.When a process executes the wait operation and finds that the semaphore value is not >0, it is entered into waiting queue associated with the semaphore and the state of the process is switched to the waiting state. Then the control is transferred to the CPU scheduler, which selects another process to execute.

When another process executes a signal operation, any process from the waiting queue has to be restarted by a wakeup operation. The wakeup operation changes the process from the waiting state to the ready state. The process is then placed in the ready queue.

The operation system provides block and wakeup(P) system calls. The block operation suspends the process that invokes it. The wakeup(P ) operation resumes the execution of blocked process P.

Retrieved from "http://en.wikipedia.org/wiki/Wikipedia:Articles_for_creation/Today"