0
3.7kviews
Describe semaphore with suitable example.
1 Answer
0
137views

Semaphores: It is a system of sending message by using flags. Multiple concurrent threads of execution with an application must be able to synchronize their execution & co-ordinate mutually exclusive access to shared resources. To fulfill these requirement RTOS kernel provides a semaphore object that one or more threads of execution can acquire or release for the purpose of synchronization or mutual exclusion. Semaphore is like a key that allows a test to carry out some operation or to access a resource A kernel supports many different types of semaphores

Binary: Binary semaphores are used for both mutual exclusion and synchronization purposes. A binary semaphore is used to control sharing a single resource between tasks. Its internal counter can have only the values of 1 (available) and 0 (unavailable). A semaphore test passes if the count is 1, in which case, the current task is allowed to proceed.

enter image description here

Counting: it is a semaphore that increments when an IPC is given by a task. It decrements when a waiting task unblocks and starts running.

enter image description here

Mutex: Mutexes are binary semaphores that include a priority inheritance mechanism. Mutexes are the better choice for implementing simple mutual exclusion (hence 'MUT'ual 'EX'clusion). When used for mutual exclusion the mutex acts like a token that is used to guard a resource. When a task wishes to access the resource it must first obtain ('take') the token. When it has finished with the resource it must 'give' the token back - allowing other tasks the opportunity to access the same resource.

Example of using semaphores for Synchronization:

Assume two concurrent processes P1 and P2 having statements S1 and S2. We want in any case S1 should execute first. this can be achieved easily by initialize Sem=0;

In process P1

{

// Execute whatever you want to do

// before executing P2

S1;

signal(Sem);

}

in process P2

{

wait(Sem);

S2;

}

Please log in to add an answer.