Semaphore

A semaphore, is a protected variable (an entity grouping several variables that may or may not be numerical) which constitutes the classic method for restricting access to shared resources, such as shared memory , in a multi programming environment (a system where several programs may be executing, or taking turns to execute, at once). Semaphores exist in many variants, though usually the term refers to a counting semaphore, since a binary semaphore is better known as a mutex. A counting semaphore is a counter for a set of available resources, rather than a locked/unlocked flag of a single resource. It was invented by Dijkstra . Semaphores are the classic solution to preventing race conditions in the dining philosopher problem, although they do not prevent resource deadlock.

Semaphores can only be accessed using the following operations. Those marked atmic should not be interrupted (that is, if the system decides that the "turn is up" for the program doing this, it shouldn't stop it in the middle of those instructions) for the reasons explained below.

P(Semaphore s) // Acquire Resource
{
wait until s > 0, then s := s-1;
/* must be atomic once s > 0 is detected */
}

V(Semaphore s) // Release Resource
{
s := s+1; /* must be atomic */
}

Init(Semaphore s, Integer v)
{
s := v;
}

0 comments:

Design by.....Embedded School