Top Banner
1 Chapter 7: Process Synchronization
69

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

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Chapter 7: Process Synchronization 2 Contents Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization.

1

Chapter 7: Process Synchronization

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

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

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

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

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

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;

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

5

Producer process

item nextProduced;

while (1) {while (counter == BUFFER_SIZE)

; /* do nothing */buffer[in] = nextProduced;in = (in + 1) % BUFFER_SIZE;counter++;

}

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

6

Consumer process

item nextConsumed;

while (1) {while (counter == 0)

; /* do nothing */nextConsumed = buffer[out];out = (out + 1) % BUFFER_SIZE;counter--;

}

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

7

Bounded Buffer

• The statements

counter++;counter--;

must be performed atomically.

• Atomic operation means an operation that completes in its entirety without interruption.

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

8

In machine language

• count++ register1 = count;register1= register1 + 1;count = register1

• count--register2 = count;register2= register2 – 1;count = register2

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

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

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.

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

10

Race Condition

T0: producer execute rigister1 := count {register1=5}

T1: producer execute rigister1 := rigister1 +1 {register1=6}

T2: consumer execute rigister2 := count {register2=5}

T3: consumer execute rigister2 := rigister2 –1 {register2=4}

T4: producer execute count := rigister1 {count =6}

T5: consumer execute count := rigister2 {count =4}

Should be 5.

Error!

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

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.

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

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

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

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

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

14

Critical-Section Problem1. 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.

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

15

Critical-Section Problem1. 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.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.

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

16

Critical-Section Problem1. 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.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

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

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.

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

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: ×

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

19

Two-Process Solutions

• Only 2 processes, P0 and P1

• General structure of process Pi

do {entry section critical sectionexit section reminder section

} while (1);

• Use Pj to denote the other process. (j == 1 – i)• Processes may share some common variables to synchronize

their actions.

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

20

Algorithm 1

• Shared variables: int turn;initially turn = 0

turn == i Pi can enter its critical section• Process Pi

do {while (turn != i) ;

critical sectionturn = j;

reminder section} while (1);

• Satisfies mutual exclusion, but not progress

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

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.

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

22

Algorithm 2• Shared variables

– boolean flag[2];initially flag [0] = flag [1] = false.

– flag [i] = true Pi ready to enter its critical section

• Process Pi

do {flag[i] := true;while (flag[j]);

critical section

flag [i] = false;

remainder section} while (1);

• Satisfies mutual exclusion, but not progress requirement.

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

23

Algorithm 2• Shared variables

– boolean flag[2];initially flag [0] = flag [1] = false.

– flag [i] = true Pi ready to enter its critical section

• Process Pi

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 ?

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

24

Algorithm 2

• The mutual-exclusion requirement is satisfied.

• Unfortunately, the progress requirement is still not met.

• Consider the following execution sequence:T0: P0 sets flag[0] = true

T1: P1 sets flag[1] = true

Now P0 and P1 are looping forever in their respective while statements.

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

25

Algorithm 3

• Combined shared variables of algorithms 1 and 2.

• Process Pi

do {flag [i]:= true;turn = j;while (flag [j] and turn = j) ; critical sectionflag [i] = false; remainder section

} while (1);• Meets all three requirements; solves the critical-section problem for

two processes.

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

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.

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

27

Bakery Algorithm

• Before entering its critical section, process receives a number. Holder of the smallest number enters the critical section.

• If processes Pi and Pj receive the same number, if i < j, then Pi is served first; else Pj 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

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

28

Bakery Algorithm

• Notation : lexicographical order (ticket #, process id #)– (a,b) < (c,d) if a < c or if a = c and b < d– max (a0,…, an-1) is a number, k, such that k ai for i - 0,

…, n – 1

• Shared databoolean choosing[n];int number[n];

Data structures are initialized to false and 0 respectively

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

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 sectionnumber[i] = 0;

remainder section} while (1);

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

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

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

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;

}

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

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

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

33

Mutual Exclusion with Test-and-Set

• Shared data: boolean lock = false;

• Process Pi

do {while (TestAndSet(lock)); critical section

lock = false; remainder section

}

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

34

Mutual Exclusion with Test-and-Set

• Shared data: boolean lock = false;

• Process Pi

do {while (TestAndSet(lock)) ; critical sectionlock = 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

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

35

Mutual Exclusion with Test-and-Set

• Shared data: boolean lock = false;

• Process Pi

do {while (TestAndSet(lock)) ; critical sectionlock = 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

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

36

Synchronization Hardware

• Atomically swap two variables.

void Swap(boolean &a, boolean &b) {

boolean temp = a;a = b;b = temp;

}

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

37

Mutual Exclusion with Swap• Shared data (initialized to false):

boolean lock;boolean waiting[n];

• Process Pi

do {key = true;while (key == true) Swap(lock,key);critical sectionlock = false;remainder section}

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

38

Mutual Exclusion with Swap• Shared data (initialized to false):

boolean lock;boolean waiting[n];

• Process Pi

do {key = true;while (key == true) Swap(lock,key);critical sectionlock = 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

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

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.

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

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.

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

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.

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

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

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

43

Solving Synchronization problems

• P1 with statement S1, P2 with statement S2 and S2 can be executed only after S1 has completed.

• Shared data: semaphore mutex; //initially mutex = 1

• insert the statements

S1;signal(synch);

in process P1, and the statements

wait(synch);S2;

in process P2

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

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

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

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.

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

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?

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

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.

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

48

Semaphore Eliminating Busy-Waiting

• Define a semaphore as a recordtypedef 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.

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

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

}}

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

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 1P0 P1

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

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

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 1P0 P1

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: ×

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

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

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

53

Implement counting semaphore S• Data structures:

binary-semaphore S1, S2;int C:

• Initialization:

S1 = 1S2 = 0C = initial value of semaphore S

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

54

Implement counting semaphore S• wait operation

wait(S1);C--;if (C < 0) {

signal(S1);wait(S2);

}signal(S1);

• signal operationwait(S1);C ++;if (C <= 0)

signal(S2);else

signal(S1);

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

55

Classical Problems of Synchronization

• Bounded-Buffer Problem

• Readers and Writers Problem

• Dining-Philosophers Problem

§ 7.5

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

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

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

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

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

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

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

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

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

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.

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

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: ×

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

62

Readers-Writers Problem: Writer• Shared data

semaphore mutex, wrt;

Initiallymutex = 1, wrt = 1, readcount = 0

• The writer process:wait(wrt);

…writing is performed …

signal(wrt);

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

63

Readers-Writers Problem: Readerwait(mutex);readcount++;if (readcount == 1)

wait(wrt);signal(mutex);

…reading is performed …

wait(mutex);readcount--;if (readcount == 0)

signal(wrt);signal(mutex):

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

64

Readers-Writers Problem: Readerwait(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: ×

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

65

Dining-Philosophers Problem

• A classic synchronization problem because it is an example of a large class of concurrency-control problems.

§ 7.5.3

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

66

Dining-Philosophers Problem

• Simple solution: represent each chopstick by a semaphore. Get the chopstick: waitRelease the chopstick: signal

• Shared data: semaphore chopstick[5];

Initially all values are 1

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

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

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

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

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

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