Friday, January 5, 2007

Semaphores and Monitors

Semaphores:


A semaphore is a protected variable (or ADT) and constitutes the classic method for restricting access to shared resources (e.g. storage) in a multiprogramming environment. They were invented by Edsger Dijkstra

The value of the semaphore is initialized to the number of equivalent shared resources it is implemented to control. In the special case where there is a single equivalent shared resource, the semaphore is called a binary semaphore.

Semaphores are the classic solution to the dining philosophers problem, although they do not prevent all deadlocks.

The simplest kind of semaphore is the "binary semaphore," used to control access to a single resource. It is essentially the same as a mutex. It is always initialized with the value 1. When the resource is in use, the accessing thread calls P(S) to decrease this value to 0, and restores it to 1 with the V operation when the resource is ready to be freed (number represents how many processes can have access).

Monitors:

only one process at a time is allowed "inside'' the monitor where "inside'' means a call of an operation in the monitor is in progress or accepted

Two "life simplifying'' assumptions regarding condition variables

  1. the signaler must exit the monitor immediately after signaling by executing a return statement so that nothing can change before the signaled process wakes up

  2. the signaled process is given priority to proceed inside the monitor over those processes waiting to enter the monitor through an operation call so that in this way the signaled process finds the condition still true when it resumes
This is called the signal and exit signaling discipline.

Monitors can be implemented with semaphores and vice versa

Monitors Vs. Semaphores:

You use monitors in applications, not in operating systems.


to be continued ...

No comments: