Operating Systems: Semaphores
Operating Systems: Semaphores
Semaphores
Semaphores
• Semaphore: It is a synchronization tool used to
synchronize two or more process.
• A semaphore (S ) is an integer variable that can
be accessed only through two standard atomic
operations: wait() and signal(). Suppose S is a
semaphore then the operations are as follows:
//wait() is defined as:
wait(S) { // signal() is defined as :
while (S <= 0) signal(S) {
; // busy wait S++;
S--; }
}
Dr.B.Hemantha Kumar. RVR&JC CE 2
Semaphores
• All modifications to the integer value of the semaphore
in the wait() and signal() operations must be executed
indivisibly. That is, when one process modifies the
semaphore value, no other process can simultaneously
modify that same semaphore value.
• In addition, in the case of wait(S), the testing of the
integer value of S (S ≤ 0), as well as its possible
modification (S--), must be executed without
interruption.
//wait() is defined as: // signal() is defined as :
wait(S) { signal(S) {
while (S <= 0)
; // busy wait
S++;
S--; }
}
Dr.B.Hemantha Kumar. RVR&JC CE 3
Semaphores
• Semaphore Usage:
• Two types of semaphores are existing, counting and
binary semaphores.
• The value of a counting semaphore can range over an
unrestricted domain.
• The value of a binary semaphore can range only
between 0 and 1. Thus, binary semaphores behave
similarly to mutex locks.
• In fact, on systems that do not provide mutex locks,
binary semaphores can be used instead for providing
mutual exclusion.
typedef struct {
int value;
struct process *list;
} semaphore;