tailieunhanh - Lecture Operating system concepts - Chapter 7: Process synchronization

Chapter 7 is concerned with the topic of process synchronization among concurrently executing processes. Concurrency is generally very hard for students to deal with correctly, and so we have tried to introduce it and its problems with the classic process coordination problems: mutual exclusion, bounded-buffer, readers/writers, and so on. An understanding of these problems and their solutions is part of current operating-system theory and development. | Chapter 7: Process Synchronization I Background I The Critical-Section Problem I Synchronization Hardware I Semaphores I Classical Problems of Synchronization I Critical Regions I Monitors I Synchronization in Solaris 2 & Windows 2000 Operating System Concepts Silberschatz, Galvin and Gagne 2002 Background I Concurrent access to shared data may result in data inconsistency. I Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes. I Shared-memory solution to bounded-butter problem (Chapter 4) allows at most n – 1 items in buffer at the same time. A solution, where all N buffers are used is not simple. ✦ Suppose that we modify the producer-consumer code by adding a variable counter, initialized to 0 and incremented each time a new item is added to the buffer Operating System Concepts Silberschatz, Galvin and Gagne 2002 Bounded-Buffer I Shared data #define BUFFER_SIZE 10 typedef struct { . } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0; int counter = 0; Operating System Concepts Silberschatz, Galvin and Gagne 2002 Bounded-Buffer I Producer process item nextProduced; while (1) { while (counter == BUFFER_SIZE) ; /* do nothing */ buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; counter++; } Operating System Concepts Silberschatz, Galvin and Gagne 2002 Bounded-Buffer I Consumer process item nextConsumed; while (1) { while (counter == 0) ; /* do nothing */ nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; } Operating System Concepts Silberschatz, Galvin and Gagne 2002 Bounded Buffer I The statements counter++; counter--; must be performed atomically. I Atomic operation means an operation that completes in its entirety without interruption. Operating System Concepts Silberschatz, Galvin and Gagne 2002 Bounded Buffer I The statement “count++” may be implemented in machine language as: register1 = counter register1 = .