A standard optimization is to use negative values in the semaphore counter to indicate the number of processes waiting this avoids having to test separately if the queue is empty or not. When a process increments the counter, it will wake a blocked process at the front of the queue. When a process sees a zero or negative value in the semaphore, instead of spinning it adds itself to the end of the wait queue and blocks (informs the scheduler that it is no longer ready to run). Semaphores are integrated with scheduling by including a wait queue along with the counter value. (With a second semaphore that tracks empty space in the buffer we can also block the producer when the buffer is full.) Similarly in a producer-consumer model we can use signal whenever the producer adds a new item to the buffer and wait when the consumer is ready to remove one this blocks the consumer when there is nothing to do. We can also use signal to indicate that some resource is available for example, if we have a group of worker threads waiting for things to do (e.g., kernel threads waiting to run user threads in a many-to-many thread model), we can have them all execute wait on a semaphore that is initially zero and use signal to indicate that some new job has shown up. A semaphore initially set to a higher value will allow multiple threads in simultaneously. A semaphore that is initially set to 1 acts like a lock: only one thread can acquire it at a time. We can use semaphores to protect a resource or collection of resources. built-in atomic semaphore operations) it may be possible to avoid this. Binary semaphores can be used to implement conditional critical regions by using the split binary semaphore (SBS) technique. Note the use of a mutex to protect the semaphore from lost updates.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |