1 / 20

Chapter 6 Process Synchronization

Chapter 6 Process Synchronization. Bernard Chen Spring 2007. Outline. Background The Critical-Section Problem Peterson’s Solution. Background . Concurrent access to shared data may result in data inconsistency

paul2
Télécharger la présentation

Chapter 6 Process Synchronization

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. Chapter 6Process Synchronization Bernard Chen Spring 2007

  2. Outline • Background • The Critical-Section Problem • Peterson’s Solution

  3. Background • Concurrent access to shared data may result in data inconsistency • In this chapter, we discuss various mechanisms to ensure the orderly cooperating process that share a logical address space, so that the data consistency is maintained

  4. Shared-Memory Systems • Unbounded Buffer: the consumer may have to wait for new items, but producer can always produce new items. • Bounded Buffer: the consumer have to wait if buffer is empty, the producer have to wait if buffer is full

  5. Bounded Buffer #define BUFFER_SIZE 6 Typedefstruct { . . . } item; item buffer[BUFFER_SIZE]; intin = 0; intout = 0;

  6. Bounded Buffer (producer view) while (true) { /* Produce an item */ while (((in + 1) % BUFFER SIZE count) == out) ; /* do nothing --no free buffers */ buffer[in] = item; in = (in + 1) % BUFFER SIZE; }

  7. Bounded Buffer (Consumer view) while (true) { while (in == out) ; // do nothing --nothing to consume // until remove an item from the buffer item = buffer[out]; out = (out + 1) % BUFFER SIZE; return item; }

  8. Background • In chapter 3, we illustrated the model with producer-consumer problem. • Suppose that we wanted to provide a solution to the consumer-producer problem that fills all the buffers. We can do so by having an integer count that keeps track of the number of full buffers. Initially, count is set to 0. It is incremented by the producer after it produces a new buffer and is decremented by the consumer after it consumes a buffer.

  9. Bounded Buffer (producer view) while (true) { /* produce an item and put in next Produced*/ while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++; }

  10. Bounded Buffer (Consumer view) while (true) { while (count == 0) ; // do nothing nextConsumed= buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; /* consume the item in next Consumed }

  11. Background • Although both the producer and consumer routines are correct separately, they may not function correctly when executed concurrently.

  12. Race Condition • We could have this incorrect state because we allowed both processes to manipulate the variable counter concurrently • Race Condition: several processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular order in which the access takes place. • Major portion of this chapter is concerned with process synchronization and coordination

  13. 6.2 The Critical-Section Problem • Consider a system with n processes, each of them has a segment code, called critical section, in which the process may be changing common variables, updating a table, writing a file…etc. • The important feature is that allow one process execute in its critical section at a time.

  14. 6.2 The Critical-Section Problem • Each process must request permission to enter its critical section. • The section of code implementing this request is the entry section • The critical section maybe followed by an exit section • The remaining code is the remainder section

  15. 6.2 The Critical-Section Problem A solution to the critical-section problem must satisfy the following three requirements: • 1. Mutual Exclusion - If process Pi is executing in its critical section, then no other processes can be executing in their critical sections • 2.Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely • 3.Bounded Waiting - A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted

  16. 6.3 Peterson’s Solution • It is restricted to 2 processes that alternate execution between their critical section and remainder section The two processes share two variables: • Int turn; • Boolean flag[2]

  17. 6.3 Peterson’s Solution The two processes share two variables: • Int turn; • Boolean flag[2] The variable turn indicates whose turn it is to enter the critical section. The flag array is used to indicate if a process is ready to enter the critical section. flag[i] = true implies that process Pi is ready!

  18. Algorithm for Process Pi while (true) { flag[i] = TRUE; turn = j; while ( flag[j] && turn == j) ; CRITICAL SECTION flag[i] = FALSE; REMAINDER SECTION }

  19. Algorithm for Process Pi do { acquire lock critical section release lock remainder section }

More Related