Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Semaphore (programming)
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Semaphore (programming)
(section)
Add topic