0% found this document useful (0 votes)
88 views3 pages

Defining Semaphores

This document defines semaphores, their operations, and use for managing shared resources and task synchronization. It explains that a semaphore acts like a key to a shared resource, allowing only one task access at a time. There are three main semaphore operations - create, acquire, and release. Binary semaphores handle one resource while counting semaphores can manage multiple instances of a resource. Semaphores ensure tasks wait their turn to access a shared resource or synchronize with other tasks.

Uploaded by

Amit Saha
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)
88 views3 pages

Defining Semaphores

This document defines semaphores, their operations, and use for managing shared resources and task synchronization. It explains that a semaphore acts like a key to a shared resource, allowing only one task access at a time. There are three main semaphore operations - create, acquire, and release. Binary semaphores handle one resource while counting semaphores can manage multiple instances of a resource. Semaphores ensure tasks wait their turn to access a shared resource or synchronize with other tasks.

Uploaded by

Amit Saha
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/ 3

DEFINING SEMAPHORES, OPERATIONS AND USE

What is a Semaphore ?

Consider a situation where there are two person who wants to share a bike. At one time only one
person can use the bike. The one who has the bike key will get the chance to use it. And when
this person gives the key to 2nd person, then only 2nd person can use the bike.

Semaphore is just like this Key and the bike is the shared resource. Whenever a task wants
access to the shared resource, it must acquire the semaphore first. The task should release the
semaphore after it is done with the shared resource. Till this time all other tasks have to wait if
they need access to shared resource as semaphore is not available. Even if the task trying to
acquire the semaphore is of higher priority than the task acquiring the semaphore, it will be in
wait state until semaphore is released by the lower priority task.

Use of Semaphore

1. Managing Shared Resource

1. Task Synchronization

Apart from managing shared resource, task synchronization can also be performed with
the help of a semaphore. In this case semaphore will be like a flag not key.

1. Unilateral Rendezvous

This is one way synchronization which uses a semaphore as a flag to signal another task.

1. Bilateral Rendezvous

This is two way synchronization performed using two semaphores. A bilateral


rendezvous is similar to a unilateral rendezvous, except both tasks must synchronize with
one another before proceeding.
Types of semaphore

1. Binary Semaphore

Binary semaphore is used when there is only one shared resource.

1. Counting Semaphore

To handle more then one shared resource of same type, counting semaphore is used.

1. Mutual Exclusion Semaphore or Mutex


To avoid extended priority inversion, mutexes can be used. You can check Mutex
Working here.
Operations on Semaphore

Basically, there are 3 operations related to the semaphore:

1. Create
2. Acquire
3. Release

API Details
To create semaphore
{{#Widget:LibTable}}
Defination vSemaphoreCreateBinary( xSemaphoreHandle xSemaphore)

Input xSemaphoreHandle : It is a handle to the created semaphore.


Arguments
It can be used futher to use other semaphore APIs.
Return Value none

It creates binary semaphore.


Description
It writes non NULL value to the argument xSemaphoreHandle if semaphore is
created.
xSemaphoreHandle Sem_A = NULL;
Usage
vSemaphoreCreateBinary(Sem_A);
To acquire semaphore
{{#Widget:LibTable}}
Defination xSemaphoreTake( xSemaphoreHandle xSemaphore, portTickType xBlockTime)

Input xSemaphoreHandle : It is a handle to the semaphore being obtained.


Arguments
portTickType : The time in ticks to wait for the semaphore to become available.
Return Value TRUE if semaphore is obtained else FLASE.

It is used to request to acquire the semaphore.


Description
The macro portTICK_RATE_MS can be used to convert this the portTickType to
a real time. A block time of zero can be used to poll the semaphore.
Usage xSemaphoreTake(Sem_A,50);

To release semaphore
{{#Widget:LibTable}}
Defination xSemaphoreGive( xSemaphoreHandle xSemaphore )

Input Arguments xSemaphoreHandle : It is a handle to the semaphore being released.

Return Value TRUE if semaphore is released else FLASE.

It is used to release the semaphore.


Description
Don't use this API to release semaphore from ISR.
Usage xSemaphoreGive(Sem_A);

You might also like