what we will cover… process synchronization basic concepts the critical-section problem ...

48
What we will cover… Process Synchronization Basic Concepts The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors 1-1 Lecture 5

Post on 21-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

What we will cover…

Process Synchronization Basic Concepts The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors

1-1Lecture 5

Page 2: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Objectives

To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data

To present both software and hardware solutions of the critical-section problem

1-2Lecture 5

Page 3: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Why Process Synchronization

Concurrent access to shared data may result in data inconsistency

Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes

1-3Lecture 5

Page 4: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Recap Producer-Consumer Problem

Let’s try a solution to the producer-consumer problem that fills all the buffers

Introduce an integer count that keeps track of the number of full buffers Initially, count is set to 0 incremented by the producer after it produces a new

item decremented by the consumer after it consumes an item

1-4Lecture 5

Page 5: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

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

}

1-5Lecture 5

Page 6: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Consumer while (true) {

while (count == 0) ; // do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE;

count--;

/* consume the item in nextConsumed */}

1-6Lecture 5

Page 7: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Inconsistency

Both the routines are correct separately BUT they may not function correctly when

executed concurrently

Let’s look at an illustration!

1-7Lecture 5

Page 8: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Race Condition count++ could be implemented as

register1 = count register1 = register1 + 1 count = register1

count-- could be implemented as

register2 = count register2 = register2 - 1 count = register2

Consider this execution interleaving with “count = 5” initially:S0: producer execute register1 = count {register1 = 5}S1: producer execute register1 = register1 + 1 {register1 = 6} S2: consumer execute register2 = count {register2 = 5} S3: consumer execute register2 = register2 - 1 {register2 = 4} S4: producer execute count = register1 {count = 6 } S5: consumer execute count = register2 {count = 4}

Other execution order could have produced other

result!!!

1-8Lecture 5

Page 9: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Three properties: Critical-Section

1. 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 grantedAssume that each process executes at a nonzero speed

1-9Lecture 5

Page 10: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Peterson’s Solution Two process solution Assume that the LOAD and STORE

instructions are atomic; that is, cannot be interrupted.

The two processes share two variables: int turn; Boolean flag[2]

The variable turn indicates whose turn it is to enter the critical section.

The flag array is used to indicate if a process is ready to enter the critical section. flag[i] = true implies that process Pi is ready!

1-10Lecture 5

Page 11: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Algorithm for Process Pi

do {

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

critical section flag[i] = FALSE;

remainder section } while (TRUE);

Prove 3 properties of Critical Section Problem!!!

1-11Lecture 5

Page 12: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Synchronization Hardware Many systems provide hardware support for

critical section code Uniprocessors – could disable interrupts

Currently running code would execute without preemption

Generally too inefficient on multiprocessor systems

• Operating systems using this not broadly scalable

Modern machines provide special atomic hardware instructions

• Atomic = non-interruptable Either test memory word and set value Or swap contents of two memory words

1-12Lecture 5

Page 13: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

do { acquire lock

critical section release lock

remainder section } while (TRUE);

Solution to Critical-section Problem Using Locks

1-13Lecture 5

Page 14: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

TestAndSet Instruction

Definition:

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

1-14Lecture 5

Page 15: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Solution using TestAndSet Shared boolean variable lock., initialized to false. Solution:

do { while ( TestAndSet (&lock )) ; // do nothing // critical section lock = FALSE; // remainder section

} while (TRUE);

1-15Lecture 5

Page 16: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Swap Instruction

Definition:

void Swap (boolean *a, boolean *b) { boolean temp = *a; *a = *b; *b = temp: }

1-16Lecture 5

Page 17: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Solution using Swap Shared Boolean variable lock initialized to FALSE; Each

process has a local Boolean variable key Solution: do { key = TRUE; while ( key == TRUE) Swap (&lock, &key ); // critical section

lock = FALSE;

// remainder section

} while (TRUE);

1-17Lecture 5

Page 18: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Semaphore Semaphore S : integer variable Accessed only through two standard operations

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

Less complicated Can only be accessed via two indivisible (atomic)

operations wait (S) { while S <= 0

; // no-op S--; }

signal (S) { S++; }

1-18Lecture 5

Page 19: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Semaphore as General Synchronization Tool

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

Also known as mutex locks

Provides mutual exclusionSemaphore mutex; // initialized to 1do {

wait (mutex); // Critical Section signal (mutex);

// remainder section} while (TRUE);

1-19Lecture 5

Page 20: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Semaphore Implementation

Must guarantee that no two processes can execute wait () and signal () on the same semaphore at the same time

Thus, implementation becomes the critical section problem where the wait and signal code are placed in the critical section

1-20Lecture 5

Page 21: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Semaphore as General Synchronization Tool

Semaphore used to solve various synchronization problems…

E.g., consider two concurrently running processes: P1 with statement S1 and P2 with S2

Requirement: S2 be executed only after S1 is completed

Implement the code using proper placement of wait and signal!

1-21Lecture 5

Page 22: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Semaphore disadvantages

Have busy waiting (spinlock) in critical section implementation

• Little busy waiting if critical section rarely occupied

Note that applications may spend lots of time in critical sections and therefore this is not a good solution.

1-22Lecture 5

Page 23: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Semaphore Implementation with no Busy waiting

With each semaphore there is an associated waiting queue. Each entry in a waiting queue has two data items: value (of type integer) pointer to next record in the list

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.

1-23Lecture 5

Page 24: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Semaphore Implementation with no Busy waiting (Cont.)

Implementation of wait:

wait(semaphore *S) { S->value--; if (S->value < 0) {

add this process to S->list; block();

} }

Implementation of signal:

signal(semaphore *S) { S->value++; if (S->value <= 0) {

remove a process P from S->list; wakeup(P);

}}

1-24Lecture 5

Page 25: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

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

Priority Inversion - Scheduling problem when lower-priority process holds a lock needed by higher-priority process

Will be discussed in detail later!

1-25Lecture 5

Page 26: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Classic Problems of Synchronization

Semaphore Solutions… Bounded-Buffer Problem Readers and Writers Problem Dining-Philosophers Problem

1-26Lecture 5

Page 27: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

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.

1-27Lecture 5

Page 28: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Bounded Buffer Problem (Cont.)

The structure of the producer process

do {

// produce an item in nextp

wait (empty); wait (mutex);

// add the item to the buffer

signal (mutex); signal (full); } while (TRUE);

1-28Lecture 5

Page 29: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Bounded Buffer Problem (Cont.)

The structure of the consumer process

do { wait (full); wait (mutex);

// remove an item from buffer to nextc

signal (mutex); signal (empty); // consume the item in nextc

} while (TRUE);

1-29Lecture 5

Page 30: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

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

Problem – allow multiple readers to read at the same time. Only one single writer can access the shared data at the same time

1-30Lecture 5

Page 31: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Readers-Writers Problem (Cont.)

Implement the structure of readers and writer process!

Hint: Shared Semaphores Semaphore mutex initialized to 1 Semaphore wrt initialized to 1 Integer readcount initialized to 0

1-31Lecture 5

Page 32: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Dining-Philosophers Problem

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

1-32Lecture 5

Page 33: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Dining-Philosophers Problem

Design the process only from one philosopher’s point of view!

1-33Lecture 5

Page 34: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Problems with Semaphores Correct use of semaphore

operations:

signal (mutex) …. wait (mutex)

wait (mutex) … wait (mutex)

Omitting of wait (mutex) or signal (mutex) (or both)

1-34Lecture 5

Page 35: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Monitors

A high-level abstraction that provides a convenient and effective mechanism for process synchronization

Characterized by a set of programmer-defined operators

1-35Lecture 5

Page 36: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Why monitors?

Concurrency has always been an OS issue Resource allocation is necessary among competing

processes Timer interrupts

Existing synchronization mechanisms (semaphores, locks) are subject to hard-to-find, subtle bugs.

Page 37: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

What is a monitor?

A collection of data and procedures Mutual exclusion

allows controlled acquisition and release of critical resources

single anonymous lock automatically acquired and released at entry and exit

Data encapsulation monitor procedures are “entry points” for

accessing data

Page 38: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Monitors: a language construct Monitors are a programming language

construct anonymous lock issues handled by

compiler and OS detection of invalid accesses to critical

sections happens at compile time process of detection can be automated

by compiler by scanning the text of the program

Page 39: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

An Abstract Monitor

name: monitor …local declarations …initialize local data proc1 (…parameters)

…statement list proc2 (…parameters)

…statement list proc3 (…parameters) …statement list

Page 40: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Rules to Follow with Monitors

Any process can call a monitor procedure at any time

But only one process can be inside a monitor at any time (mutual exclusion)

No process can directly access a monitor’s local variables (data encapsulation)

A monitor may only access its local variables

Page 41: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Enforcing the Rules

“wait” operation current process is put to sleep

“signal” operation wakes up a sleeping process

condition variables May have different reasons for “waiting” or

“signaling” Processes waiting on a particular condition

enter its queue

Page 42: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Condition Variables condition x;

Two operations on a condition variable: x.wait () – a process that invokes the

operation is suspended. x.signal () – resumes one of processes

(if any) that invoked x.wait ()

1-42Lecture 5

Page 43: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

A simple example: Car

car: monitor occupied: Boolean; occupied := false; nonOccupied: condition;

procedure enterCar()if occupied then nonOccupied.wait;

occupied = true;

procedure exitCar() occupied = false; nonOccupied.signal;

monitor declaration

local variables / initializations

procedure

procedure

Page 44: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Multiple Conditions

Sometimes it is necessary to be able to wait on multiple things

Can be implemented with multiple conditions

Example: 2 reasons to enter car drive (empties tank) fill up car

Two reasons to wait: Going to gas station but tank is already full Going to drive but tank is (almost) empty

Page 45: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Condition Queue

Might want to check if any process is waiting on a condition The “condition queue” returns true if a

process is waiting on a condition

Example: filling the tank only if someone is waiting to drive the car.

Page 46: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Monitor with Condition Variables

1-46Lecture 5

Page 47: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Solution to Dining Philosophers

monitor dp {

enum { THINKING; HUNGRY, EATING) state [5] ;condition self [5];

void pickup (int i) { state[i] = HUNGRY; test(i); if (state[i] != EATING) self [i].wait;}

void putdown (int i) { state[i] = THINKING;

// test left and right neighbors test((i + 4) % 5); test((i + 1) % 5);

}

void test (int i) { if ( (state[(i + 4) % 5] != EATING) && (state[i] == HUNGRY) && (state[(i + 1) % 5] != EATING) ) { state[i] = EATING ;

self[i].signal () ; } }

initialization_code() { for (int i = 0; i < 5; i++) state[i] = THINKING;}

}1-47Lecture 5

Page 48: What we will cover…  Process Synchronization  Basic Concepts  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Semaphores

Solution to Dining Philosophers (cont)

Each philosopher I invokes the operations pickup()

and putdown() in the following sequence:

dp.pickup (i);

EAT

dp.putdown (i);

1-48Lecture 5