Semaphores have two basic operations

Pend : wait for event

Post : signal occurrence of event

Reply to this note

Please Login to reply.

Discussion

Task : pend semaphore

ISR : post semaphore

Pend Operation

Decrements counter and executed waiting task

Or else moves ready task into waiting state

——————————————————

Post Operation

Increments counter to signal occurrence of an event

Or else moves waiting task into ready state

A pend operation can only be performed by a task, not by an ISR

OS Services > Service APIs

Service vs API

Semaphores are used to synchronize tasks

Semaphores use a counter to track the number of events

Posting a semaphore notifies a task about an event

A semaphore pend allows a task to wait on an event

Potential problem of semaphore is priority invertion

Low priority task holds some resources which are needed by high priority task.

1. Low priority task is running and holding the key resource which is also needed by high priority task (high priority is in waiting state for an event to occur and another condition to run the high priority task is to have the key resource which is now holding by the low priority)

2. ISR occurs and the event needed by the high priority task is coming in. The high priority task begins to run, but the key resource is holding by low priority. Then high priority task releases the CPU control.

3. The OS scheduler finds a middle priority task in ready tasks list and give CPU control to middle priority task

4. Middle priority task completes and low priority task continues to run (since high priority task still waiting for the key resource which is still held by low priority task)

5. Low priority task finishes and releases the key resource. High priority task begins to run

This is priority inversion