Presentation is loading. Please wait.

Presentation is loading. Please wait.

Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.

Similar presentations


Presentation on theme: "Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware."— Presentation transcript:

1 Process Synchronization CS 360

2 Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization

3 Slide 3 CS 360, WSU Vancouver Background Concurrent access to shared data may result in data inconsistency or non- deterministic behavior Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes. Race condition: 4 The situation where several processes access – and manipulate shared data concurrently. The final value of the shared data depends upon which process finishes last. 4 To prevent race conditions, concurrent processes must be synchronized.

4 Slide 4 CS 360, WSU Vancouver The Critical-Section Problem n A resource that allows only one user at a time to use is called a critical resource. n Processes compete for the use of critical resources n Each process has a code segment, called a critical section, in which the concerned critical resource is used. n Problem – ensure that when one process is executing in its critical section, no other process is allowed to execute in the critical section with the critical resource.

5 Slide 5 CS 360, WSU Vancouver Definitions 1. Mutual Exclusion: At most one process can be inside its critical section at a time. 2. Progress: If no process is executing in its critical section, no process outside its critical section should block other processes indefinitely from entering their critical section. 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. 4. No process can remain inside the critical section indefinitely. 5. No assumption concerning relative speed of the n processes or the number of CPUs can be made. 6. Deadlock is when two or more processes are waiting for an event to occur and that event can never occur. 7. Processes not deadlocked could wait for events that may never occur because of biases in the resource scheduling policies of a system. This is called starvation or indefinite postponement.

6 Slide 6 CS 360, WSU Vancouver Hardware Support Any implementation must ultimately rely on mutual exclusion provided by hardware. All computers offer a basic form of mutual exclusion called storage interlock. It prohibits the concurrent execution of two instructions that access the same main storage location. In the Intel processors: 4 BTS – Bit test and set (80386+) 4 BTR – Bit test and reset (80386+) 4 CMPSWAP – compare swap (80486+)

7 Slide 7 CS 360, WSU Vancouver Synchronization Hardware Test and set Instruction This technique requires hardware locks, which can be as simple as a single bit Boolean flag. Test and modify the content of a word atomically. boolean TestAndSet(boolean *target) { boolean rv = target; target = true; return rv; }

8 Slide 8 CS 360, WSU Vancouver Mutual Exclusion with Test-and-Set Shared data: boolean lock = false; Process P i do { while (TestAndSet (&lock) ) ; /*  “busy” loop */ critical section lock = false; remainder section }

9 Slide 9 CS 360, WSU Vancouver Mutual Exclusion with Swap Swap Instruction Atomically swap the contents of two words. void Swap(boolean *a, boolean *b) { boolean temp = *a; *a = *b; *b = temp; }

10 Slide 10 CS 360, WSU Vancouver Mutual Exclusion with Swap Shared data (initialized to false): boolean lock; Process P i do { key = true; while (key == true) Swap(&lock,&key) ; /*  “busy” loop */ critical section lock = false; remainder section }

11 Slide 11 CS 360, WSU Vancouver Only 2 processes, P 0 and P 1 General structure of process P 0 (other process P j ) do { entry section critical section exit section remainder section } while (1); Processes must share some variables in common to synchronize their actions. Initial Attempts to Solve Problem

12 Slide 12 CS 360, WSU Vancouver Semaphores Semaphores are primitives used in dealing with n-process critical-section problems or solving various synchronization problems. A semaphore can be defined as a record: typedef struct { int value; struct process *L; } semaphore; 4 The integer variable is a counter which indicates how many instances of the resource are currently available. 4 The list of processes indicates the processes waiting on the semaphore.

13 Slide 13 CS 360, WSU Vancouver Semaphores can only be accessed via two indivisible (atomic) operations: P, or wait, and V, or signal (proceed). The P and V notation is due to Dijkstra used his native Dutch to get P from verlagen (to try to decrease) and V from verhagen (to increase). In English, P(S) is usually wait() and V(S) is usually signal(S): wait (S): while S  0 do no-op; S--; signal (S): S++;

14 Slide 14 CS 360, WSU Vancouver Two Types of Semaphores Counting semaphore – integer value can range over an unrestricted domain. Binary semaphore – integer value can range only between 0 and 1; can be simpler to implement. Can implement a counting semaphore S as a binary semaphore.

15 Slide 15 CS 360, WSU Vancouver Mutual Exclusion of n Processes Shared data: semaphore mutex; //initially mutex = 1 Process P i : do { wait(mutex); critical section signal(mutex); remainder section } while (1);

16 Slide 16 CS 360, WSU Vancouver Semaphore as a General Synchronization Tool Execute B in P j only after A executed in P i Use semaphore flag initialized to 0 Code: P i P j  Await(flag) signal(flag)B

17 Slide 17 CS 360, WSU Vancouver Classical Problems of Synchronization Bounded Buffer Problem Readers and Writers Problem Dining Philosophers Problem Sleepy Barber Problem

18 Slide 18 CS 360, WSU Vancouver Bounded Buffer Problem This is a producer/consumer problem The producer creates a product for use by the consumer. They act concurrently, synchronization is required since nothing can be consumed unless it has first been produced. There is a finite amount of resource (memory) in which to store the products produced but not yet consumed.

19 Slide 19 CS 360, WSU Vancouver Readers and Writers Problem There are two kinds of processes (readers / writers) attempting to use a common resource. No process may use the resource concurrently with a writer. The problem is to coordinate access for the many readers and writers without allowing deadlock or starvation.

20 Slide 20 CS 360, WSU Vancouver Dining Philosophers Problem Five philosophers (processes) are seated at a round table as in the following figure. Five chopsticks (resources) are distributed as shown Each philosopher alternates between eating and thinking. In order to eat, a philosopher must have possession of the two chopsticks on his immediate left and right. The problem is to synchronize the philosophers' acquisition of the chopsticks to prevent deadlock and indefinite starvation.

21 Slide 21 CS 360, WSU Vancouver Dining Philosophers Problem Shared data semaphore chopstick[5];// Initially all values are 1

22 Slide 22 CS 360, WSU Vancouver Sleepy Barber Problem The barber's shop consists of a fixed number of chairs in a waiting room and a separate room holding the barber chair. The barber sleeps when no customers are present. An entering customer acts in the following manner: 4 If all chairs are full, the customer leaves. 4 Otherwise, if the barber is busy, the customer takes a chair. 4 Otherwise, the customer wakes up the barber and sits in the barber chair for a haircut. The goal is to synchronize the actions of the barber and the customers.

23 Slide 23 CS 360, WSU Vancouver Semaphore Implementation Semaphore operations now defined as wait(S): S.value- -; if (S.value < 0) { add this process to S.L; block this process; } signal(S): S.value++; if (S.value > 0) { S.value--; remove a process P from S.L; wakeup(P); }

24 Slide 24 CS 360, WSU Vancouver Deadlock and Starvation Deadlock 4 Example: Let S and Q be two semaphores initialized to 1 P 0 P 1 wait(S);wait(Q); wait(Q);wait(S);  signal(S);signal(Q); signal(Q)signal(S); Starvation 4 Example: A process may never be removed from the semaphore queue in which it is suspended.

25 Slide 25 CS 360, WSU Vancouver Bounded-Buffer Problem Shared data semaphore full, empty, mutex; /* define three semaphores */ Initially: full = 0, empty = n, mutex = 1

26 Slide 26 CS 360, WSU Vancouver Bounded-Buffer Problem Producer Process do { … produce an item in nextp … wait(empty); wait(mutex); … add nextp to buffer … signal(mutex); signal(full); } while (1);

27 Slide 27 CS 360, WSU Vancouver Bounded-Buffer Problem Consumer Process do { wait(full) wait(mutex); … remove an item from buffer to nextc … signal(mutex); signal(empty); … consume the item in nextc … } while (1);

28 Slide 28 CS 360, WSU Vancouver Readers-Writers Problem Shared data semaphore mutex, wrt; integer readcount; Initially mutex = 1, wrt = 1, readcount = 0

29 Slide 29 CS 360, WSU Vancouver Readers-Writers Problem: Writer Process wait(wrt); … writing is performed … signal(wrt);

30 Slide 30 CS 360, WSU Vancouver Readers-Writers Problem: Reader Process wait(mutex); readcount++; if (readcount == 1) wait(wrt); signal(mutex); … reading is performed … wait(mutex); readcount--; if (readcount == 0) signal(wrt); signal(mutex);


Download ppt "Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware."

Similar presentations


Ads by Google