Semaphores
,Overview
• A Semaphore is a kernel primitive object. Semaphore
operations:
• Can change a task’s state.
• Are fast.
• Three semaphore types available:
• Binary semaphores allow a task to pend until a given event occurs
(e.g., an interrupt).
• Mutual exclusion semaphores allow a task to acquire an exclusive lock
on a shared resource (e.g., a file or a device).
• Counting semaphores are available:
Less commonly used.
See manual pages for details.
,The Synchronization Problem
myGetData ( )
{
requestData( );
Task waitForData( );
getData( );
}
• Task may need to wait for an event to occur.
• Busy waiting (i.e., polling) is inefficient.
• Pending until the event occurs is better.
, The Synchronization Solution
• Create a binary semaphore for the event.
• Binary semaphores exist in one of two states:
• Full (event has occurred).
• Empty (event has not occurred).
• Task waiting for the event calls semTake( ) and blocks until
semaphore is given.
• Task or interrupt service routine detecting the event calls
semGive( ), which unblocks the waiting task.
,Overview
• A Semaphore is a kernel primitive object. Semaphore
operations:
• Can change a task’s state.
• Are fast.
• Three semaphore types available:
• Binary semaphores allow a task to pend until a given event occurs
(e.g., an interrupt).
• Mutual exclusion semaphores allow a task to acquire an exclusive lock
on a shared resource (e.g., a file or a device).
• Counting semaphores are available:
Less commonly used.
See manual pages for details.
,The Synchronization Problem
myGetData ( )
{
requestData( );
Task waitForData( );
getData( );
}
• Task may need to wait for an event to occur.
• Busy waiting (i.e., polling) is inefficient.
• Pending until the event occurs is better.
, The Synchronization Solution
• Create a binary semaphore for the event.
• Binary semaphores exist in one of two states:
• Full (event has occurred).
• Empty (event has not occurred).
• Task waiting for the event calls semTake( ) and blocks until
semaphore is given.
• Task or interrupt service routine detecting the event calls
semGive( ), which unblocks the waiting task.