INtime® 3.1 Software - tenAsys
INtime® 3.1 Software - tenAsys
INtime® 3.1 Software - tenAsys
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
INtime <strong>3.1</strong> <strong>Software</strong><br />
Semaphores<br />
A semaphore is a counter that takes positive integer values. Threads use semaphores<br />
for synchronization by sending units to and receiving units from the semaphores.<br />
When a thread sends n units to a semaphore, the value of the counter increases by n;<br />
when a thread receives n units from a semaphore, the value of the counter decreases<br />
by n.<br />
The next figure shows a typical example of a binary (one-unit) semaphore used for<br />
synchronization.<br />
Figure 3-2. Threads using a semaphore for synchronization<br />
A<br />
<br />
B<br />
<br />
To ensure that thread A can do its work before thread B starts running, thread A creates a<br />
semaphore that contains one unit. To enable synchronization, threads A and B should request and<br />
obtain the unit before running.<br />
Thread A begins to run and obtains the semaphore unit, leaving the semaphore empty. While the<br />
semaphore has no units, thread B cannot run.<br />
When thread A completes, it returns the unit to the semaphore. Thread B can now obtain the unit<br />
and start running.<br />
Semaphores:<br />
• Enable synchronization; they don't enforce it. If threads do not request and obtain<br />
units from the semaphore before running, synchronization does not occur. Each<br />
thread must return the unit to the semaphore when it is no longer needed.<br />
Otherwise, threads can be permanently prevented from running.<br />
• Provide mutual exclusion from data or a resource as follows:<br />
1. Thread A requests one unit from a binary semaphore, and uses the resource<br />
when it receives the unit.<br />
2. Thread B requests one unit from the semaphore before using the resource.<br />
Thread B must wait at the semaphore until thread A returns the unit.<br />
• Enable mutual exclusion; they do not enforce it.<br />
• Have a queue where threads wait for units. The queue may be FIFO- or prioritybased.<br />
There are system calls to create and delete semaphores, and to send and<br />
receive units.<br />
36