0% found this document useful (0 votes)
99 views5 pages

What Is Semaphore

A semaphore is a variable that coordinates access to shared resources between threads. It uses atomic wait and signal operations to control access. There are two main types: counting semaphores, where a count tracks the number of available resources, and binary semaphores, which can be 0 or 1. Wait operations decrement or block on zero, while signal operations increment. Semaphores provide a flexible way for threads to synchronize access to critical sections and shared resources.

Uploaded by

Vaijayanthi S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views5 pages

What Is Semaphore

A semaphore is a variable that coordinates access to shared resources between threads. It uses atomic wait and signal operations to control access. There are two main types: counting semaphores, where a count tracks the number of available resources, and binary semaphores, which can be 0 or 1. Wait operations decrement or block on zero, while signal operations increment. Semaphores provide a flexible way for threads to synchronize access to critical sections and shared resources.

Uploaded by

Vaijayanthi S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

What is Semaphore?

Semaphore is simply a variable that is non-negative and shared between threads. A


semaphore is a signaling mechanism, and a thread that is waiting on a semaphore can be
signaled by another thread. It uses two atomic operations, 1) Wait, and 2) Signal for the
process synchronization.

A semaphore either allows or disallows access to the resource, which depends on how it is
set up.

Characteristic of Semaphore
Here, are characteristic of a semaphore:

 It is a mechanism that can be used to provide synchronization of tasks.


 It is a low-level synchronization mechanism.
 Semaphore will always hold a non-negative integer value.
 Semaphore can be implemented using test operations and interrupts, which should
be executed using file descriptors.

Types of Semaphores
The two common kinds of semaphores are

 Counting semaphores
 Binary semaphores.

Counting Semaphores
This type of Semaphore uses a count that helps task to be acquired or released numerous
times. If the initial count = 0, the counting semaphore should be created in the unavailable
state.
However, If the count is > 0, the semaphore is created in the available state, and the
number of tokens it has equals to its count.

Binary Semaphores
The binary semaphores are quite similar to counting semaphores, but their value is
restricted to 0 and 1. In this type of semaphore, the wait operation works only if
semaphore = 1, and the signal operation succeeds when semaphore= 0. It is easy to
implement than counting semaphores.

Example of Semaphore
The below-given program is a step by step implementation, which involves usage and
declaration of semaphore.

Shared var mutex: semaphore = 1;


Process i
begin
.
.
P(mutex);
execute CS;
V(mutex);
.
.
End;

Wait and Signal Operations in Semaphores


Both of these operations are used to implement process synchronization. The goal of this
semaphore operation is to get mutual exclusion.
Wait for Operation
This type of semaphore operation helps you to control the entry of a task into the critical
section. However, If the value of wait is positive, then the value of the wait argument X is
decremented. In the case of negative or zero value, no operation is executed. It is also called
P(S) operation.

After the semaphore value is decreased, which becomes negative, the command is held up
until the required conditions are satisfied.

Copy CodeP(S)
{
while (S<=0);
S--;
}
Signal operation
This type of Semaphore operation is used to control the exit of a task from a critical section.
It helps to increase the value of the argument by 1, which is denoted as V(S).

Copy CodeP(S)
{
while (S>=0);
S++;
}

Counting Semaphore vs. Binary Semaphore


Here, are some major differences between counting and binary semaphore:

Counting Semaphore Binary Semaphore

No mutual exclusion Mutual exclusion

Any integer value Value only 0 and 1

More than one slot Only one slot

Provide a set of Processes It has a mutual exclusion mechanism.

Difference between Semaphore vs. Mutex


Parameters Semaphore Mutex

Mechanism It is a type of signaling mechanism. It is a locking mechanism.


Data Type Semaphore is an integer variable. Mutex is just an object.

The wait and signal operations can modify a It is modified only by the process
Modification
semaphore. or release a resource.

If no resource is free, then the process requires a If it is locked, the process has to w
Resource
resource that should execute wait operation. It should should be kept in a queue. This ne
management
wait until the count of the semaphore is greater than 0. accessed only when the mutex is

You can have multiple program th


Thread You can have multiple program threads.
but not simultaneously.

Value can be changed by any process releasing or Object lock is released only by the
Ownership
obtaining the resource. has obtained the lock on it.

Types of Semaphore are counting semaphore and


Types Mutex has no subtypes.
binary semaphore and

Semaphore value is modified using wait () and signal ()


Operation Mutex object is locked or unlocke
operation.

It is occupied if all resources are being used and the


In case if the object is already loc
Resources process requesting for resource performs wait ()
requesting resources waits and is
Occupancy operation and blocks itself until semaphore count
system before lock is released.
becomes >1.

Advantages of Semaphores
Here, are pros/benefits of using Semaphore:

 It allows more than one thread to access the critical section


 Semaphores are machine-independent.
 Semaphores are implemented in the machine-independent code of the microkernel.
 They do not allow multiple processes to enter the critical section.
 As there is busy waiting in semaphore, there is never a wastage of process time and
resources.
 They are machine-independent, which should be run in the machine-independent
code of the microkernel.
 They allow flexible management of resources.
Disadvantage of semaphores
Here, are cons/drawback of semaphore

 One of the biggest limitations of a semaphore is priority inversion.


 The operating system has to keep track of all calls to wait and signal semaphore.
 Their use is never enforced, but it is by convention only.
 In order to avoid deadlocks in semaphore, the Wait and Signal operations require to
be executed in the correct order.
 Semaphore programming is a complicated, so there are chances of not achieving
mutual exclusion.
 It is also not a practical method for large scale use as their use leads to loss of
modularity.
 Semaphore is more prone to programmer error.
 It may cause deadlock or violation of mutual exclusion due to programmer error.

You might also like