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!
===Passing the baton pattern=== The "Passing the baton" pattern<ref>{{cite book |last1=Andrews |first1=Gregory R. | date=1999|publisher= Addison-Wesley|title=Foundations of Multithreaded, Parallel, and Distributed Programming}}</ref><ref>{{cite book |last1=Carver |first1=Richard H. |last2=Thai |first2=Kuo-Chung |date=2005|publisher=Wiley|title=Modern Multithreading: Implementing, Testing, and Debugging Multithreaded Java and C++/Pthreads/Win32 Programs}}</ref><ref>{{cite book |last1=Maurer |first1=Christian | date=2021|publisher= Springer|title=Nonsequential and Distributed Programming with Go}}</ref> proposed by Gregory R. Andrews is a generic scheme to solve many complex concurrent programming problems in which multiple processes compete for the same resource with complex access conditions (such as satisfying specific priority criteria or avoiding starvation). Given a shared resource, the pattern requires a private "priv" semaphore (initialized to zero) for each process (or class of processes) involved and a single mutual exclusion "mutex" semaphore (initialized to one). The pseudo-code for each process is: <syntaxhighlight lang="cpp"> void process(int proc_id, int res_id) { resource_acquire(proc_id, res_id); <use the resource res_id>; resource_release(proc_id, res_id); } </syntaxhighlight> The pseudo-code of the resource acquisition and release primitives are: <syntaxhighlight lang="cpp"> void resource_acquire(int proc_id, int res_id) { P(mutex); if(<the condition to access res_id is not verified for proc_id>) { <indicate that proc_id is suspended for res_id>; V(mutex); P(priv[proc_id]); <indicate that proc_id is not suspended for res_id anymore>; } <indicate that proc_id is accessing the resource>; pass_the_baton(); // See below } </syntaxhighlight> <syntaxhighlight lang="cpp"> void resource_release(int proc_id, int res_id) { P(mutex); <indicate that proc_id is not accessing the resource res_id anymore>; pass_the_baton(); // See below } </syntaxhighlight> Both primitives in turn use the "pass_the_baton" method, whose pseudo-code is: <syntaxhighlight lang="cpp"> void pass_the_baton(int res_id) { if <the condition to access res_id is true for at least one suspended process> { int p = <choose the process to wake>; V(priv[p]); } else { V(mutex); } } </syntaxhighlight> '''Remarks''' The pattern is called "passing the baton" because a process that releases the resource as well as a freshly reactivated process will activate at most one suspended process, that is, shall "pass the baton to it". The mutex is released only when a process is going to suspend itself (resource_acquire), or when pass_the_baton is unable to reactivate another suspended process.
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)