1 / 17

1.8 Semaphore Statement

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).

bela
Download Presentation

1.8 Semaphore Statement

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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)

  2. 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)

  3. 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.

  4. Semaphore statements • request(r), release(r) • request(r): • <await r>0; r:=r-1> • release(r): • r:=r+1

  5. 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

  6. 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

  7. Mutual exclusion of multiple processes by semaphores. • Fig. 1.10, p. 62

  8. 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

  9. 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.

  10. 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.

  11. 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.

  12. 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.

  13. 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

  14. Comparing Region statements to Semaphores • Region statement comparing to Semaphores: • More structured • More powerful • More expensive implementation

  15. 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.

  16. 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

  17. 1.10 Model 3: Message-Passing Text • We saw • Diagrams • Shared-variables text • Now • Message-passing text (like CSP)

More Related