Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s...

Post on 20-Dec-2015

229 views 3 download

transcript

Chapter 6: Process Synchronization

Outline

• Background• Critical-Section Problem• Peterson’s Solution• Synchronization Hardware• Semaphores• Classic Problems of Synchronization• Monitors• Synchronization Examples • Atomic Transactions

Background• Concurrent access to shared data

may result in data inconsistency• Maintaining data consistency

requires mechanisms to ensure the orderly execution of cooperating processes

• Example: Producer Consumer Problem in the following slides

Producer while (true) { /* produce an item and put in nextProduced

*/ while (count == BUFFER_SIZE); // do nothing

buffer [in] = nextProduced;in = (in + 1) % BUFFER_SIZE;count++;

}

Consumer

while (true)

{ while (count == 0); // do nothing

nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE;

count--; // consume the item in nextConsumed

}

Race Conditions in Producer Consumer Functions

• count++ could be implemented as

register1 = count register1 = register1 + 1 count = register1

• count-- could be implemented as

register2 = count register2 = register2 - 1 count = register2

Race Condition in Producer Consumer Functions

• Consider this execution interleaving with “count = 5” initially:S0: Producer register1 = count {register1 = 5}S1: Producer register1 = register1 + 1 {register1 =

6} S2: Consumer register2 = count {register2 = 5}S3: Consumer register2 = register2 - 1 {register2 =

4} S4: Producer count = register1 {count = 6 } S5: Consumer count = register2 {count = 4}

• Wrong results could be produced due to race conditions

Critical Section Problem

• n processes {P0, P1, ... , Pn-1} have a segment of code, called a critical section, in which a process may change a common variable, update a shared table or a file, ...

• When one process is in a CS, ensure no other process is allowed to enter the CS

• Entry section: code segment to request the permission to enter a CS

• Exit section: code segment to exit the CS• Remainder section: remaining code

Solution to Critical Section Problem: 3 Conditions to meet

C1. Mutual Exclusion: If a process is executing in its critical section, then no other processes can be executing in their critical sections

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

Solution to Critical Section Problem: 3 Conditions to meet

C3. 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 requested to enter its critical section and before that request is granted

Peterson’s Solution

• Two process solution• Assume LOAD and STORE instructions

are atomic; that is, they cannot be interrupted

• Two processes share two variables:– int turn: whose turn it is to enter the CS – Boolean flag[2]: flag[i] = true implies

that process Pi is ready to enter the CS

Algorithm for Process Pi

while (true)

{ flag[i] = TRUE; turn = j; while (flag[j] && turn == j);

CRITICAL SECTION

flag[i] = FALSE;

REMAINDER SECTION }

C1, C2, C3 all satisfied!

atomic

N process solution

• Bakery algorithm– On entering the store, each customer

receives a number– The customer with the lowest number is

served next– If two customers have the same

number, serve the one with the smaller ID number

Process Pi in bakery algorithm

while(1){

choosing[i] = true;number[i] = max(number[0], number[1], ..., number[n-1]) + 1;choosing[i] = false;for k=0 to n-1

{while (choosing[k]) nop;while (number[k] ≠ 0 and

(number[k], k) < (number[i], i)) nop;}

Critical section

number[i] = 0;

remainder section}

Synchronization Hardware

• For efficiency and convenience of programming, modern machines provide special atomic hardware instructions– Atomic = non-interruptible– test-and-set– swap

TestAndndSet Instruction

boolean TestAndSet (boolean *target) { boolean rv = *target; *target = TRUE; return rv: }

Remember this instruction is atomic!

Solution using TestAndSet• Initially shared variable lock = false

while (1) {

while (TestAndSet (&lock)) nop;

critical section

lock = FALSE;

remainder section}

C1 & C2 supportedC3 not supported

Swap Instruction

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

boolean temp = *a; *a = *b; *b = temp:}

This instruction is also atomic!

Solution using Swap• Initially shared boolean variable lock = false • Each process has a local boolean variable key

while (true) { key = TRUE; while ( key == TRUE) swap (&lock, &key ) nop; // critical section

lock = FALSE;

// remainder section

}

C1 & C2 supportedC3 not supported

Semaphore• Much simpler than the other solutions • Semaphore S: integer variable• Only two atomic operations can modify S

– wait() and signal()– Originally called P() and V()

wait (S) {

while S ≤ 0 nop; //busy waitingS--

}signal (S) { S++;}

Semaphore as General Synchronization Tool

• Binary semaphore – Special case where the value of

semaphore S is either 0 or 1– Simpler to implement

• Counting semaphore– Semaphore can have integer values– When a process does wait(S) and finds

that S->value < 0, the process is added to the waiting list for S

Binary Semaphore Usages

• Provides mutual exclusion – called mutex semaphoreSemaphore S; // initialized to 1wait (S); Critical Sectionsignal (S);

Binary Semaphore Usages

• Solve synchronization problems– Example: Ensure process P2 executes

only after process P1 completes– Initialize binary semaphore synch = 0P1 P2S1; wait(synch);signal(synch); S2;

Semaphore Implementation with no Busy waiting

• Each semaphore has a waiting queue • Two operations:

– block: Place the process invoking the operation on the appropriate waiting queue

– wakeup: Remove one of processes in the waiting queue and place it in the ready queue

Semaphore Implementation with no Busy waiting (Cont.)

wait (S){ S.value--; if (value < 0) add this process to waiting queue S.waiting_q block(); }

signal (S){

S.value++;if (value ≤ 0)

{ remove a process P from S.waiting_q wakeup(P);

}}

How to make wait and signal atomic?

• Uniprocessor– Disable interrupt

• Multiprocessor– Instructions of different processes

running on different processors may interleave in an arbitrary manner

– Spinlock (busy waiting) is necessary

Deadlock

• Deadlock: Two or more processes are waiting indefinitely for an event that can be caused by only one of the other waiting processes

Deadlock

• Let S and Q be two semaphores initialized to 1

P0 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

– Can happen if processes are removed from the waiting queue in LIFO order

Classical Problems of Synchronization

• Bounded-Buffer Problem• Readers and Writers Problem• Dining-Philosophers Problem

Bounded-Buffer Problem

• N buffers, each can hold one item

• Semaphore mutex initialized to the value 1

• Semaphore full initialized to the value 0

• Semaphore empty initialized to the value N.

Bounded Buffer Problem

• Producer while (true)

{

// produce an item

wait (empty); wait (mutex);

// add the item to the // buffer

signal (mutex); signal (full); }

• Consumerwhile (true) { wait (full); wait (mutex);

// remove an item from buffer

signal (mutex); signal (empty); // consume the removed

item}

Readers-Writers Problem• A data set is shared among a number of concurrent

processes– Readers only read the data set; they do not

perform any updates– Writers can both read and write.

• Multiple readers to read at the same time • Only a single writer can access the shared data at

the same time.

• Shared Data– Semaphore mutex initialized to 1– Semaphore wrt initialized to 1– Integer readcount initialized to 0

Readers-Writers Problem

• Writer while (true)

{ wait (wrt) ; // writing is performed

signal (wrt) ; }

• Reader

while (true) { wait (mutex) ; readcount ++ ; if (readcount == 1) wait

(wrt) ; signal (mutex) // reading is performed

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

(wrt) ; signal (mutex) ;}

Dining-Philosophers Problem

• Shared data – Bowl of rice (data set)– Semaphore chopstick [5] initialized to 1

Dining-Philosophers Problem

• Philosopher iwhile (true) {

wait ( chopstick[i] ); wait ( chopStick[ (i + 1) % 5] );

// eat

signal ( chopstick[i] ); signal (chopstick[ (i + 1) % 5] );

// think}

Dining-Philosophers Problem

• What happens if every philosopher becomes hungry at the same time?

• Each philosopher picks his/her right chopstic: chopsitc[1..5] = 0

• They are delayed forever• Solutions

– Allow at most four philosophers– Pick up only if two chopsticks are available at

the same time– An odd philosopher picks up the left chopstick

first, while an even philosopher picks up the right one first