A semaphore can be viewed as a protected variable
whose value can be modified only with the
rtems_semaphore_create
,
rtems_semaphore_obtain
, and
rtems_semaphore_release
directives. RTEMS
supports both binary and counting semaphores. A binary semaphore
is restricted to values of zero or one, while a counting
semaphore can assume any non-negative integer value.
A binary semaphore can be used to control access to a
single resource. In particular, it can be used to enforce
mutual exclusion for a critical section in user code. In this
instance, the semaphore would be created with an initial count
of one to indicate that no task is executing the critical
section of code. Upon entry to the critical section, a task
must issue the rtems_semaphore_obtain
directive to prevent other tasks from entering the critical section.
Upon exit from the critical section, the task must issue the
rtems_semaphore_release
directive to
allow another task to execute the critical section.
A counting semaphore can be used to control access to
a pool of two or more resources. For example, access to three
printers could be administered by a semaphore created with an
initial count of three. When a task requires access to one of
the printers, it issues the rtems_semaphore_obtain
directive to obtain access to a printer. If a printer is not currently
available, the task can wait for a printer to become available or return
immediately. When the task has completed printing, it should
issue the rtems_semaphore_release
directive to allow other tasks access to the printer.
Task synchronization may be achieved by creating a
semaphore with an initial count of zero. One task waits for the
arrival of another task by issuing a rtems_semaphore_obtain
directive when it reaches a synchronization point. The other task
performs a corresponding rtems_semaphore_release
operation when it reaches its synchronization point, thus unblocking
the pending task.
Copyright © 1988-2008 OAR Corporation