Understanding Semaphores and Their Role in Process Synchronization
This text delves into semaphores, critical tools for managing access to shared resources in parallel processes. It discusses the mechanisms of semaphore statements, including request and release actions, to enforce mutual exclusion within critical sections. It highlights the importance of coordination statements that ensure exclusive execution and outlines the function of region statements as a structured approach to synchronization. Examples, such as the producer-consumer problem, illustrate the practical application of semaphores and region statements in achieving effective concurrency control in computing.
Understanding Semaphores and Their Role in Process Synchronization
E N D
Presentation Transcript
1.8 Semaphore Statement • One of the main problems in coordinating parallel processes is the management of access to shared resources. • Mechanisms • Grouped statements (1.7) • Semaphore statement (1.8) • Region statement (1.9)
An example for a critical section, which shall be executed atomically: Fig. 1.8, P.57 (splitting one statement to two, to make it possible to be interleaved with others)
To prevent unwanted interferences, we can use • grouped statements • It is a locking device. Disallows any action by a parallel process until the process executing the sequence completes. • We want as much parallel processing as possible. • General constructs that achieve locking and coordination between processes are referred to as synchronization constructs.
Semaphore statements • request(r), release(r) • request(r): • <await r>0; r:=r-1> • release(r): • r:=r+1
Use of semaphores for mutual exclusion • Mutual exclusion problem: devise a protocol, that contains critical, non-critical, and coordination statements; where coordination statements guarantee exclusive execution of the critical sections. • Example: Fig. 1.9, p. 60
A location containing a request(r) statement: is a checkpoint, synchronizing the process with other processes containing request(r) and release(r) statements, on the same semaphore variable r. • An important assumption: variables used in the protocol for coordination between the processes (semaphore variables) are not modified in the critical sections. • Problem 1.4
Mutual exclusion of multiple processes by semaphores. • Fig. 1.10, p. 62
Other uses of Semaphores • To protect critical sections: request-release pairs appear within the same process. • Semaphores can be used in other signaling and synchronizations. • Fig. 1.12, p. 64
Example: producer-consumer • Fig.1.12, p.64 • Producer computes x, then append x to the end of buffer b, cycle back. • Consumer removes elements from the top of the buffer, deposits them in y, cycle back.
Semaphores • r: protect the critical section, buffer • ne: “number of empties”, ensuring that producer cannot deposit more than N items (that have not been removed by consumer). • nf: “ number of fulls”, ensuring that consumer does not take from an empty buffer. • nf and ne: unidirectional signaling devices. • nf, a signal generated by producer’s release and sensed by consumer’s request. • ne, a signal generated by consumer’s release and sensed by producer’s request.
1.9 Region statement • Semaphores provide powerful synchronization constructs. • But they are unstructured and do not enforce a disciplined methodology. • Region statement: protect critical sections, and provide additional testing capabilities.
Example, Fig. 1.13, p. 68 • Problem 1.5 • Resource declaration • r: resource protecting(y1,y2, …,yn) • Region statement • region r when c do S • S does not contain any cooperation statement or other region statement.
Shared variables, must belong to precisely one resource. • c is the guard condition, and S is the body of the region statement. • The only shared variables that may be referenced within c or S must belong to r. • The region statement will be successfully executed, if the condition c be true and no other statement protected by r is currently executing. • See page 67
Comparing Region statements to Semaphores • Region statement comparing to Semaphores: • More structured • More powerful • More expensive implementation
implementation • For a semaphore: • keep track of the current value of the semaphore and • the identity of the processes waiting for it (request). • For a region statement: • keep track of the current status of the resource and • The identities of the waiting processes and • The status of their individual guard conditions.
Synchronization within Selection Statements • In the previous examples: • synchronization statements were essential, • and they appear in the context of concatenation. • It may not be essential, or several synchronizations may be possible • See Fig. 1.14, p. 69 • Producer-consumer with l producers, n consumers and m buffers
1.10 Model 3: Message-Passing Text • We saw • Diagrams • Shared-variables text • Now • Message-passing text (like CSP)