0% found this document useful (0 votes)
857 views

Operating Systems: Semaphores

- Semaphores are used for process synchronization and allow processes to access shared resources. They maintain a count value that is modified by wait() and signal() operations. - There are counting and binary semaphores. Counting semaphores can range over any value while binary semaphores are limited to 0 or 1, acting like mutex locks. - Processes use wait() to decrement the semaphore value and signal() to increment. This allows controlling access to shared resources and synchronizing process execution. However, improper usage can lead to deadlocks or starvation where processes wait indefinitely.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
857 views

Operating Systems: Semaphores

- Semaphores are used for process synchronization and allow processes to access shared resources. They maintain a count value that is modified by wait() and signal() operations. - There are counting and binary semaphores. Counting semaphores can range over any value while binary semaphores are limited to 0 or 1, acting like mutex locks. - Processes use wait() to decrement the semaphore value and signal() to increment. This allows controlling access to shared resources and synchronizing process execution. However, improper usage can lead to deadlocks or starvation where processes wait indefinitely.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Operating Systems

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.

Dr.B.Hemantha Kumar. RVR&JC CE 4


Semaphores
• Semaphore Usage:
• Counting semaphores can be used to control access to a
given resource consisting of a finite number of
instances. The semaphore is initialized to the number of
resources available
• Each process that wishes to use a resource performs a
wait() operation on the semaphore (thereby
decrementing the count).
• When a process releases a resource, it performs a
signal() operation (incrementing the count). When the
count for the semaphore goes to 0, all resources are
being used. After that, processes that wish to use a
resource will block until the count becomes greater
than 0.

Dr.B.Hemantha Kumar. RVR&JC CE 5


Semaphores
• Semaphore Usage: We can also use semaphores to
solve various synchronization problems.
• For example, consider two concurrently running
processes: P1 with a statement S1 and P2 with a
statement S2. Suppose we require that S2 be executed
only after S1 has completed. We can implement this
scheme readily by letting P1 and P2 share a common
semaphore synch, initialized to 0.,
// In process P1 : // In process P2 :
S1; wait(synch);
signal(synch); S2;
• Because synch is initialized to 0, P2 will execute S2 only
after P1 has invoked signal(synch), which is after
statement S1 has been executed.
Dr.B.Hemantha Kumar. RVR&JC CE 6
Semaphores
Semaphore Implementation :
• Must guarantee that no two processes can execute
wait() and signal() on the same semaphore at the same
time .
• This implementation to the critical section problem
leads to busy waiting .
• To overcome the r busy waiting, we can modify the
definition of the wait() and signal() operations as
follows:

Dr.B.Hemantha Kumar. RVR&JC CE 7


Semaphores
Semaphore Implementation :
• When a process executes the wait() operation and finds that
the semaphore value is not positive, it must wait. However,
rather than engaging in busy waiting, the process can block
itself.
• The block operation places a process into a waiting queue
associated with the semaphore, and the state of the process
is switched to the waiting state.
• Then control is transferred to the CPU scheduler, which
selects another process to execute.
• A process that is blocked, waiting on a semaphore S, should
be restarted when some other process executes a signal()
operation.
• The process is restarted by a wakeup() operation, which
changes the process from the waiting state to the ready
state. The process is then placed in the ready queue.

Dr.B.Hemantha Kumar. RVR&JC CE 8


Semaphores
Semaphore Implementation : To implement
semaphores under this definition, we define a
semaphore as follows:
• Each semaphore has an integer value and a list of
processes list. When a process must wait on a
semaphore, it is added to the list of processes. A
signal() operation removes one process from the list
of waiting processes and awakens that process.

typedef struct {
int value;
struct process *list;
} semaphore;

Dr.B.Hemantha Kumar. RVR&JC CE 9


Semaphores
Semaphore Implementation.
wait(semaphore *S) { typedef struct {
S->value--; int value;
if (S->value < 0) { struct process *list;
add this process to S->list; } semaphore;
block();
}
}
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P);
}
} Dr.B.Hemantha Kumar. RVR&JC CE 10
Semaphores
Semaphore Implementation.
• The block() operation suspends the process that invokes
it.
• The wakeup(P) operation resumes the execution of a
blocked process P.
• These two operations are provided by the operating
system as basic system calls.
• Deadlocks and Starvation: The implementation of a
semaphore with a waiting queue may result in a
situation where two or more processes are waiting
indefinitely if you are not using them properly

Dr.B.Hemantha Kumar. RVR&JC CE 11


Semaphores
Semaphore Implementation. P0 P1
Deadlocks and Starvation: wait(S); wait(Q);
• For example two processes, wait(Q); wait(S);
P0 and P1, each accessing two . .
semaphores, S and Q, . .
set to the value 1: . .
Suppose that P0 executes signal(S); signal(Q);
wait(S) and then signal(Q); signal(S);
P1 executes wait(Q).
• When P0 executes wait(Q), it must wait until P1 executes
signal(Q). Similarly, when P1 executes wait(S), it must wait
until P0 executes signal(S). Since these signal() operations
cannot be executed, P0 and P1 are deadlocked.
• Another problem related to deadlocks is indefinite blocking
or starvation, a situation in which processes wait indefinitely
within the semaphore.
Dr.B.Hemantha Kumar. RVR&JC CE 12

You might also like