Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 7: Process Synchronization 2 Contents Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization.

Similar presentations


Presentation on theme: "1 Chapter 7: Process Synchronization 2 Contents Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization."— Presentation transcript:

1

2 1 Chapter 7: Process Synchronization

3 2 Contents Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Monitors Java Synchronization Synchronization in Solaris 2 Synchronization in Windows NT

4 3 Background Concurrent access to shared data may result in data inconsistency. Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes. Shared-memory solution to bounded-buffer problem (Chapter 4) has a race condition on the class data count § 7.1

5 4 Bounded Buffer Shared data #define BUFFER_SIZE 10 typedef struct {... } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0; int counter = 0;

6 5 Producer process item nextProduced; while (1) { while (counter == BUFFER_SIZE) ; /* do nothing */ buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; counter++; }

7 6 Consumer process item nextConsumed; while (1) { while (counter == 0) ; /* do nothing */ nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; }

8 7 Bounded Buffer The statements counter++; counter--; must be performed atomically. Atomic operation means an operation that completes in its entirety without interruption.

9 8 In machine language count++ register 1 = count ; register 1 = register 1 + 1; count = register 1 count-- register 2 = count ; register 2 = register 2 – 1; count = register 2 The concurrent execution of the statements count++ and count-- is equivalent to a sequential execution where the lower-level statements are interleaved in some arbitrary order.

10 9 Interleaving If both the producer and consumer attempt to update the buffer concurrently, the assembly language statements may get interleaved. Interleaving depends upon how the producer and consumer processes are scheduled.

11 10 Race Condition T 0 : producer execute rigister 1 := count {register 1 =5} T 1 : producer execute rigister 1 := rigister 1 +1 {register 1 =6} T 2 : consumer execute rigister 2 := count {register 2 =5} T 3 : consumer execute rigister 2 := rigister 2 –1 {register 2 =4} T 4 : producer execute count := rigister 1 { count =6} T 5 : consumer execute count := rigister 2 { count =4} Should be 5. Error!

12 11 Race Condition Race condition: The situation where several processes access – and manipulate shared data concurrently. The final value of the shared data depends upon which process finishes last. To guard against the race condition, we need to ensure that only one thread at a time can be manipulating the variable count. To make such a guarantee, we require some form of synchronization of the processes.

13 12 Critical-Section Problem n processes all competing to use some shared data Each process has a code segment, called critical section, in which the in which the thread may be changing common variables, updating a table, writing a file, and so on. Problem – ensure that when one process is executing in its critical section, no other process is allowed to execute in its critical section... mutually exclusive. The critical-section problem is how to choose a protocol that the threads can use to cooperate. § 7.2

14 13 Critical-Section Problem Each thread has a segment of code, called a critical section, in which the thread may be changing common variables, updating a table, writing a file, and so on. When one thread is executing in its critical section, no other thread is to be allowed to execute in its critical section … mutually exclusive. The critical-section problem is how to choose a protocol that the threads can use to cooperate. § 7.2 ( ) When one process is executing in its critical section, no other process is to be allowed to execute in its critical section, this is called _________. (A) global data (B) common method (C) mutually exclusive (D) constant accessing (E) value assertion Answer: C

15 14 Critical-Section Problem 1. Mutual Exclusion. If process P i 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 15 Critical-Section Problem 1. Mutual Exclusion. If process P i 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. Assume that each process executes at a nonzero speed No assumption concerning relative speed of the n processes. Short Answer Question: A solution to the critical-section problem must satisfy three requirements: mutual exclusive, progress, and bounded waiting. Please explain the meaning of these requirements.

17 16 Critical-Section Problem 1. Mutual Exclusion. If process P i 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. Assume that each process executes at a nonzero speed No assumption concerning relative speed of the n processes. Multiple Choice Question: ( ) Which of the following is not one of the requirements for a solution of critical-section problem? (A) progress (B) loopless (C) mutual exclusive (D) bounded waiting

18 17 Critical-Section Problem We can make no assumption concerning the relative speed of the n threads. The solutions should not rely on any assumptions concerning the hardware instructions. However, the basic machine-language instructions (the primitive instructions such as load, store, and test ) are executed atomically.

19 18 Critical-Section Problem We can make no assumption concerning the relative speed of the n threads. The solutions should not rely on any assumptions concerning the hardware instructions. However, the basic machine-language instructions (the primitive instructions such as load, store, and test ) are executed atomically. True-False Question: ( ) When solving critical section problems, we should make proper assumption concerning the relative speed of the threads. Answer: ×

20 19 Two-Process Solutions Only 2 processes, P 0 and P 1 General structure of process P i do { entry section critical section exit section reminder section } while (1); Use P j to denote the other process. ( j == 1 – i ) Processes may share some common variables to synchronize their actions.

21 20 Algorithm 1 Shared variables: int turn; initially turn = 0 turn == i  P i can enter its critical section Process P i do { while (turn != i) ; critical section turn = j; reminder section } while (1); Satisfies mutual exclusion, but not progress

22 21 Algorithm 1 Ensures that only one process at a time can be in its critical section. However, does not satisfy the progress requirement, since it requires strict alternation of processes in the execution of their critical section.

23 22 Algorithm 2 Shared variables –boolean flag[2]; initially flag [0] = flag [1] = false. –flag [i] = true  P i ready to enter its critical section Process P i do { flag[i] := true; while (flag[j]); critical section flag [i] = false; remainder section } while (1); Satisfies mutual exclusion, but not progress requirement.

24 23 Algorithm 2 Shared variables –boolean flag[2]; initially flag [0] = flag [1] = false. –flag [i] = true  P i ready to enter its critical section Process P i do { flag[i] := true; while (flag[j]); critical section flag [i] = false; remainder section } while (1); Satisfies mutual exclusion, but not progress requirement. Short Answer Question: Examine the algorithm on the right for solving two-tasks critical-section problem. What this algorithm has achieved and what kind of problem this algorithm is encountered, if any ?

25 24 Algorithm 2 The mutual-exclusion requirement is satisfied. Unfortunately, the progress requirement is still not met. Consider the following execution sequence: T 0 : P 0 sets flag[0] = true T 1 : P 1 sets flag[1] = true Now P 0 and P 1 are looping forever in their respective while statements.

26 25 Algorithm 3 Combined shared variables of algorithms 1 and 2. Process P i do { flag [i]:= true; turn = j; while (flag [j] and turn = j) ; critical section flag [i] = false; remainder section } while (1); Meets all three requirements; solves the critical-section problem for two processes.

27 26 Algorithm 3 Correct solution to the critical-section problem. (how to prove it? look at the textbook.) If both processes try to enter at the same time, turn is set to both i and j at roughly the same time. Only one of these assignments lasts; the other will occur, but will be overwritten immediately. The eventual value of turn decides which of the two processes is allowed to enter its critical section first.

28 27 Bakery Algorithm Before entering its critical section, process receives a number. Holder of the smallest number enters the critical section. If processes P i and P j receive the same number, if i < j, then P i is served first; else P j is served first. The numbering scheme always generates numbers in increasing order of enumeration; i.e., 1,2,3,3,3,3,4,5... Critical section for n processes

29 28 Bakery Algorithm Notation : lexicographical order (ticket #, process id #) –(a,b) < (c,d) if a < c or if a = c and b < d –max (a 0, …, a n-1 ) is a number, k, such that k  a i for i - 0, …, n – 1 Shared data boolean choosing[n]; int number[n]; Data structures are initialized to false and 0 respectively

30 29 Bakery Algorithm do { choosing[i] = true; number[i] = max(number[0], number[1], …, number [n–1])+1; choosing[i] = false; for (j = 0; j < n; j++) { while (choosing[j]) ; while ((number[j] != 0) && ((number[j],j) < (number[i],i))) ; } critical section number[i] = 0; remainder section } while (1);

31 30 Synchronization Hardware Hardware instructions can be used effectively in solving critical-section problem. It allows as either to test and modify the content of a word, or to swap the contents of two words, atomically --- as one uninterruptible unit. § 7.3

32 31 Test-and-Set Instruction Test and modify the content of a word atomically. boolean TestAndSet(boolean &target) { boolean rv = target; target = true; return rv; }

33 32 Test-and-Set Instruction Test and modify the content of a word atomically. boolean TestAndSet(boolean &target) { boolean rv = target; tqrget = true; return rv; } True-False Question: ( ) The value of the lock returned by the TestAndSet instruction is the value after the instruction is applied onto the target. Answer: X

34 33 Mutual Exclusion with Test-and-Set Shared data: boolean lock = false; Process P i do { while (TestAndSet(lock)); critical section lock = false; remainder section }

35 34 Mutual Exclusion with Test-and-Set Shared data: boolean lock = false; Process P i do { while (TestAndSet(lock)) ; critical section lock = false; remainder section } Multiple Choices Question: ( ) When calling a test-and-set instruction, a ______ must be provided as its parameter. (a) semaphore (b) integer (c) constant (d) lock Answer: d

36 35 Mutual Exclusion with Test-and-Set Shared data: boolean lock = false; Process P i do { while (TestAndSet(lock)) ; critical section lock = false; remainder section } True-False Question: ( ) When the value returned by the test-and-set instruction is false, it means that the locking is not successful. Answer: x

37 36 Synchronization Hardware Atomically swap two variables. void Swap(boolean &a, boolean &b) { boolean temp = a; a = b; b = temp; }

38 37 Mutual Exclusion with Swap Shared data (initialized to false): boolean lock; boolean waiting[n]; Process P i do { key = true; while (key == true) Swap(lock,key); critical section lock = false; remainder section }

39 38 Mutual Exclusion with Swap Shared data (initialized to false): boolean lock; boolean waiting[n]; Process P i do { key = true; while (key == true) Swap(lock,key); critical section lock = false; remainder section } Multiple Choices Question: ( ) The value of the key used to switch with the lock must be set to ______ before calling the swap method. (a) constant (b) false (c) true (d) integer Answer: c

40 39 Semaphore Previous solutions ( § 7.3) are not easy to generalize to more complex problems. Semaphores can be implemented as synchronization tool that does not require busy waiting. A semaphore S is an integer variable that can only be accessed via two indivisible (atomic) operations: P and V. P(S) { while S  0 ; // no-op; S--; } § 7.4 V(S) { S++; } These are classical definition of P and V, they require busy waiting.

41 40 Semaphore Previous solutions ( § 7.3) are not easy to generalize to more complex problems. Semaphores can be implemented as synchronization tool that does not require busy waiting. A semaphore S is an integer variable that can only be accessed via two indivisible (atomic) operations: P and V. P(S) { while S  0 ; // no-op; S--; } § 7.4 V(S) { S++; } New definition has changed to wait and signal.

42 41 Semaphore Previous solutions ( § 7.3) are not easy to generalize to more complex problems. Semaphores can be implemented as synchronization tool that does not require busy waiting. A semaphore S is an integer variable that can only be accessed via two indivisible (atomic) operations: P and V. wait(S) { while S  0 ; // no-op; S--; } § 7.4 signal(S) { S++; } New definition has changed to wait and signal.

43 42 Solving critical-section problem Shared data: semaphore mutex; //initially mutex = 1 Process Pi: do { wait(mutex); critical section signal(mutex); remainder section } while (1); § 7.4.1

44 43 Solving Synchronization problems P 1 with statement S 1, P 2 with statement S 2 and S 2 can be executed only after S 1 has completed. Shared data: semaphore mutex; //initially mutex = 1 insert the statements S 1 ; signal(synch); in process P 1, and the statements wait(synch); S 2 ; in process P 2

45 44 Semaphore Eliminating Busy- Waiting The main disadvantage of previous solutions: they all require busy waiting…a problem in single CPU, multiprogramming system. Busy waiting wastes CPU cycles that some other process might be able to use productively. This type of semaphore is also called a spinlock. Spinlocks are useful in multiprocessor systems. The advantage of a spinlock is that no context switch is required when a process must wait on a lock (context switch may take considerable time). Spinlocks are useful when locks are expected to be held for short times. § 7.4.2

46 45 Semaphore Eliminating Busy- Waiting The main disadvantage of previous solutions: they all require busy waiting…a problem in single CPU, multiprogramming system. Busy waiting wastes CPU cycles that some other process might be able to use productively. This type of semaphore is also called a spinlock. To overcome the need for busy waiting, the definition of wait and signal are modified.

47 46 Semaphore Eliminating Busy- Waiting The main disadvantage of previous solutions: they all require busy waiting…a problem in single CPU, multiprogramming system. Busy waiting wastes CPU cycles that some other process might be able to use productively. This type of semaphore is also called a spinlock. To overcome the need for busy waiting, the definition of wait and signal are modified... Short Answer Question: Although spinlocks waist CPU time with busy waiting, they are still useful in some systems. What are the advantage of spinlocks and in which situation they are considered useful?

48 47 Semaphore Eliminating Busy- Waiting Rather than busy waiting, the process can block itself. The block operation places a process into a waiting queue associated with the semaphore, and the state of the process is switched to the waiting state. A process that is blocked, waiting on S, should be restarted when some other process execute a signal operation. When the process is restarted by the wakeup operation, the process is changed from the waiting state to the ready state. The process is then placed in the ready gueue.

49 48 Semaphore Eliminating Busy- Waiting Define a semaphore as a record typedef struct { int value; struct process *L; } semaphore; Assume two simple operations: –block suspends the process that invokes it. –wakeup(P) resumes the execution of a blocked process P. These two operations are provided by the OS as basic system calls.

50 49 Semaphore Eliminating Busy- Waiting Semaphore operations now defined as void wait (semaphore S) { S.value--; if (S.value < 0) { add this process to S.L; block;} void signal(semaphore S) { S.value++; if (S.value <= 0) { remove a process P from S.L; wakeup(P); }

51 50 Deadlock and Starvation Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes. 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 – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended. the execution of a V operation § 7.4.3

52 51 Deadlock and Starvation Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes. 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 – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended. the execution of a V operation § 7.4.3 True-False Question: ( ) A deadlock can only happen in between two processes. Answer: ×

53 52 Binary 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. § 7.4.4

54 53 Implement counting semaphore S Data structures: binary-semaphore S1, S2; int C: Initialization: S1 = 1 S2 = 0 C = initial value of semaphore S

55 54 Implement counting semaphore S wait operation wait(S1); C--; if (C < 0) { signal(S1); wait(S2); } signal(S1); signal operation wait(S1); C ++; if (C <= 0) signal(S2); else signal(S1);

56 55 Classical Problems of Synchronization Bounded-Buffer Problem Readers and Writers Problem Dining-Philosophers Problem § 7.5

57 56 Bounded-Buffer Problem Shared data semaphore full, empty, mutex; Initially: full = 0, empty = n, mutex = 1 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); § 7.5.1

58 57 Bounded-Buffer Problem 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);

59 58 Bounded-Buffer Problem 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); Multiple Choices Question: ( ) Consider following program for the consumer process of bounded-buffer problem. Which instruction is missing from the blank? (a) wait(empty) (b) wait(mutex) (c) signal(full) (d) signal(empty) do { wait(full) wait(mutex); … remove an item from buffer to nextc … signal(mutex); _______________ … consume the item in nextc … } while (1); Answer: d

60 59 Readers-Writers Problem The readers-writers problem has several variations, all involving priorities. first: no reader will be kept waiting unless a writer has already obtained permission to use the shared database. § 7.5.2 No reader should wait for other readers to finish simply because a writer is waiting. Starvation: writers

61 60 Readers-Writers Problem second: once a writer is ready, that writer performs its write as soon as possible. If a writer is waiting to access the object, no new readers may start reading. Starvation: readers Here we present the first solution.

62 61 Readers-Writers Problem second: once a writer is ready, that writer performs its write as soon as possible. If a writer is waiting to access the object, no new readers may start reading. Starvation: readers Here we present the first solution. True-False Question: ( ) If a reader will be kept waiting only if a writer is currently working on the database, the starvation may happen on the reader. Answer: ×

63 62 Readers-Writers Problem: Writer Shared data semaphore mutex, wrt; Initially mutex = 1, wrt = 1, readcount = 0 The writer process: wait(wrt); … writing is performed … signal(wrt);

64 63 Readers-Writers Problem: Reader wait(mutex); readcount++; if (readcount == 1) wait(wrt); signal(mutex); … reading is performed … wait(mutex); readcount--; if (readcount == 0) signal(wrt); signal(mutex):

65 64 Readers-Writers Problem: Reader wait(mutex); readcount++; if (readcount == 1) wait(wrt); signal(mutex); … reading is performed … wait(mutex); readcount--; if (readcount == 0) signal(wrt); signal(mutex): True-False Question: ( ) Although there are many readers working on the database, the semaphore mutex will be acquired ( wait operation) and released ( signal operation) by the same reader. Answer: ×

66 65 Dining-Philosophers Problem A classic synchronization problem because it is an example of a large class of concurrency-control problems. § 7.5.3

67 66 Dining-Philosophers Problem Simple solution: represent each chopstick by a semaphore. Get the chopstick: wait Release the chopstick: signal Shared data: semaphore chopstick[5]; Initially all values are 1

68 67 Dining-Philosophers Problem Philosopher i: do { wait(chopstick[i]) wait(chopstick[(i+1) % 5]) … eat … signal(chopstick[i]); signal(chopstick[(i+1) % 5]); … think … } while (1); Deadlock when all five philosophers get her left chopstick at the same time

69 68 Dining-Philosophers Problem Philosopher i: do { wait(chopstick[i]) wait(chopstick[(i+1) % 5]) … eat … signal(chopstick[i]); signal(chopstick[(i+1) % 5]); … think … } while (1); Deadlock when all five philosophers get her left chopstick at the same time Multiple Choices Question: ( ) In Dining-Philosophers Problem, if all philosophers get their right chopstick at the same time, what will happen? (a) starvation (b) deadlock (c) blocking (d) synchronization (e) mutual exclusioncc Answer: b

70 69 Dining-Philosophers Problem Preventing deadlock by placing restrictions: –Allow at most four philosophers to be sitting simultaneously at the table. –Allow a philosopher to pick up only if both chopsticks are available. –Use an asymmetric solution (for example, an odd philosopher picks up left then right, whereas an even philosopher picks up her right chopstick and then her left.)


Download ppt "1 Chapter 7: Process Synchronization 2 Contents Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization."

Similar presentations


Ads by Google