Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Semaphore (programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Library analogy== Suppose a physical [[library]] has ten identical study rooms, to be used by one student at a time. Students must request a room from the front desk. If no rooms are free, students wait at the desk until someone relinquishes a room. When a student has finished using a room, the student must return to the desk and indicate that the room is free. In the simplest implementation, the [[clerk]] at the [[Receptionist|front desk]] knows only the number of free rooms available. This requires that all of the students use their room while they have signed up for it and return it when they are done. When a student requests a room, the clerk decreases this number. When a student releases a room, the clerk increases this number. The room can be used for as long as desired, and so it is not possible to book rooms ahead of time. In this scenario, the front desk count-holder represents a counting semaphore, the rooms are the resource, and the students represent processes/threads. The value of the semaphore in this scenario is initially 10, with all rooms empty. When a student requests a room, they are granted access, and the value of the semaphore is changed to 9. After the next student comes, it drops to 8, then 7, and so on. If someone requests a room and the current value of the semaphore is 0,<ref name="LittleBookOfSemaphores">[http://greenteapress.com/semaphores/ ''The Little Book of Semaphores''] Allen B. Downey</ref> they are forced to wait until a room is freed (when the count is increased from 0). If one of the rooms was released, but there are several students waiting, then any method can be used to select the one who will occupy the room (like [[FIFO (computing and electronics)|FIFO]] or randomly picking one). And of course, a student must inform the clerk about releasing their room only after really leaving it. ===Important observations=== When used to control access to a [[pool (computer science)|pool]] of resources, a semaphore tracks only ''how many'' resources are free. It does not keep track of ''which'' of the resources are free. Some other mechanism (possibly involving more semaphores) may be required to select a particular free resource. The paradigm is especially powerful because the semaphore count may serve as a useful trigger for a number of different actions. The librarian above may turn the lights off in the study hall when there are no students remaining, or may place a sign that says the rooms are very busy when most of the rooms are occupied. The success of the protocol requires applications to follow it correctly. Fairness and safety are likely to be compromised (which practically means a program may behave slowly, act erratically, [[hang (computing)|hang]], or [[crash (computing)|crash]]) if even a single process acts incorrectly. This includes: * requesting a resource and forgetting to release it; * releasing a resource that was never requested; * holding a resource for a long time without needing it; * using a resource without requesting it first (or after releasing it). Even if all processes follow these rules, ''multi-resource [[deadlock (computer science)|deadlock]]'' may still occur when there are different resources managed by different semaphores and when processes need to use more than one resource at a time, as illustrated by the [[dining philosophers problem]].
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)