0 ratings0% found this document useful (0 votes) 99 views4 pagesSemaphores
This Material provides a basic introduction about Semaphores in Operating systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
gmbedded/Real-Time Operating System Concepts 191
Ina preemptive kernel, response time = interrupt latency + time to save CPU registers context.
syem’s worst-case interrupt response time has to
syieroperating systenvembedded sotware bbe considered while evaluating the performance -
Interrupt Recovery Time: Time required for CPU to 0 the ‘odethighest pri
ei called interrupt recovery time, to return to the interrupted codefhighest priority
In non-preemprive kernel, interrupt recovery time = time to restore the CPU context + time to execute
the return instruction from the interrupted instruction.
In preemptive kernel, interrupt recovery time = time tovcheck whether a high priority task is ready
+ time to restore CPU context of the highest priority task + time to execute the return instruction
from the interrupt instruction.
Task CPU cpu Task
Running Context Context Running
Saving Saving
| interrust Lag
Recovery
1
1
1
Interrupt 1
Request 1
1
Interrupt hag
Latency |
g— —>
r interrupt
1 Response Time
Fig. 7.7 Interrupt Latency, Interrupt Response Time and Interrupt Recovery Time
The interrupt latency, interrupt response time and interrupr recovery time are shown in Fig. 7.7.
7.4 Semaphores
When multiple tasks are running, two or mote tasks may need ¢o share the same resource. As shown
in Fig. 7.8(a), consider a case where two tasks want to write to a display. Assume that taskl wants
to display the message “temperature is 50” and task2 has to display the message “humidity is 4096"
The display is a shared resource and if there is no synchronization between the asks, then a garbled
message is displayed such as “cemphumieratiditurey is is 50 40%", To access a shared resource, there
should be a mechanism so that there is discipline, This is known as resource synchronization.
Scanned with CamScar192
Display
Task 1 Task 2
Fig, 78 (a): Resource Synchronization
Fig. 7.8 (b): Task Synchronization
Now consider another situation shown in Fig, 7.8(b), In this case, one task reads the data from an
ADC and writes it co memory. Another task reads that data and sends it to a DAC. The read operation
takes place only after write operation and it has to be done very fast with minimal time delay. In this
case, there should be a mechanism for task to inform task2 that it has done its job. This has to°be
done through a well-defined procedure. This is known as task synchronization
‘Semaphor
is-a-kernel abject that is used for both resource synchronization and task synchronization.
~Scannea with vamscalEmbedded Real-Time Operating System Concepts 193
Q TO Semeohors
La Se
© ©
Task 1 Task 2
© © © ©
Task 1 Task 1 Task 1 Task 2 Task 2 Task 2
Acquires Uses Releases Acquires Uses Releases
‘Semaphore Display Semaphore ‘Semaphore Display + Semaphore
Fig. 7.9: Display Semaphore
Consider a situation shown in Fig. 7.9. Two tasks want to access a display. Display is a shared
resource, To control the access, is created. The semaphore is like a key to enter a house,
and hence the semaphore is represented as a ‘key’. If taskl wants to access the printer, it acquires the
semaphore, uses the printer and then releases the semaphore. If both the tasks want to access a
resource simultaneously, the kernel has to give the semaphore only to one of the tasks. This allocation
may be based on the priority of the task or on first-come-first-served basis. If a number of tasks have
to access the same resource then the tasks are kept in a queue and each task can acquire the semaphore
semaphor
one by one.
Pool of Butlers
f=] | tf fg
Fig. 7.10: Counting Semaphore
4 — canned with CamscarConsider another example in which a pool of 10 buffers is available,
The buffers need to be shared by a number of tasks, as shown in
Fig. 7.10. Any task can write to a buffer. Using a binary semaphore
Semaphore is just an integer. does not work in this case. So, a counting semaphore is used. The
Semaphores are of two types: initial value of the semaphore is set to 10. Whenever a task acquires
counting semaphore and binary | the semaphore, the value is decremented by 1 and whenever a task
semaphore. Counting releases the semaphore, it is incremented by 1. When the value is
semaphore will have an i we wae A ilable.
Pr an integer 0, it is an indication that the shared resource is no longer available.
value greater than 1. Binary
take the values
semaphore wi
of either 0 or 1.
A counting semaphore is like having multiple keys to enter a house.
CamScai
ocanne