written 5.8 years ago by | • modified 2.6 years ago |
Subject : Microcontroller and Embedded Programming
Topic : Embedded/Real Time Operating System
Difficulty : Medium
written 5.8 years ago by | • modified 2.6 years ago |
Subject : Microcontroller and Embedded Programming
Topic : Embedded/Real Time Operating System
Difficulty : Medium
written 5.8 years ago by |
SEMAPHORES :
A semaphore is a mechanism for controlling concurrent access to a shared resource. Instead of using operations to read and write a shared variable, a semaphore encapsulates the necessary shared data, and allows access only by a restricted set of operations.
There are two operations on a semaphore S. Worker processes can wait() or signal() a semaphore. For historical reasons, the wait and signal operations are sometimes abbreviated as P and V respectively.
Types of semaphores: 1) Binary semaphore 2) Counting semaphore 3) MUTEX
1. BINARY SEMAPHORE: The operation of binary semaphore can be understood by the fig(a) As shown in fig(a) initially the key for resource is available i.e. count=’1’.Task ‘A’ attempts to acquire this and gets the same. Now task ‘B’ needs the same resource, but finds the key is not available i.e. count=’0’ and hence doesn’t get the resource. Once task ‘A’ completes its requirement of the shared resource, it releases the shared resource by releasing the key and the task ‘B’ gets the same.
To perform above process we need two operations:
1. Wait(): a process performs a wait operation to tell the semaphore that it wants exclusive access to the shared resource. If the semaphore is empty, then the semaphore enters the full state and allows the process to continue its execution immediately. If the semaphore is full, then the semaphore suspends the process.
2. Signal(): a process performs a signal operation to inform the semaphore that it is finished using the shared resource. If there are processes suspended on the semaphore, the semaphore wakes up one of them. If there are no processes suspended on the semaphore, the semaphore goes into empty state.
3. The various functions required to work with semaphore are :
● create a semaphore
● delete a semaphore
● acquire a semaphore
● release a semaphore
● query a semaphore
2. COUNTING SEMAPHORE:
When multiple copies of a resource are available, multiple task can access the same simultaneously. In such eases we use a counting semaphore. The count value depends on the number of resources available at any given time. Example: We have multiple car parking possible in a mall. The counting semaphore can be used over here that keeps a track of number of parking lots free. Every time a car enters the lot the count decrements and the count increments every time the car leaves the parking lot. If the count is zero new car doesn’t get the access to the parking lot. Fig(b) shows the behavior of counting semaphore for a shared resource.
Fig 11.2 Counting Semaphore As shown in fig(b) there is a count maintained which is two in this case and hence there are two keys available. Thus a maximum of two tasks can access the shared resources at any given time.