Chapter 6 Process Synchronization Dr Yingwu Zhu Overview

  • Slides: 59
Download presentation
Chapter 6: Process Synchronization Dr. Yingwu Zhu

Chapter 6: Process Synchronization Dr. Yingwu Zhu

Overview n Question 1: Why need process synchronization? n n n Question 2: How

Overview n Question 1: Why need process synchronization? n n n Question 2: How to do synchronization? n n n Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Monitors Case study: Classic Problems of Synchronization

Take a first look n Producer-Consumer Problem

Take a first look n Producer-Consumer Problem

Producer-Consumer Problem n n Paradigm for cooperating processes a producer process produces information that

Producer-Consumer Problem n n Paradigm for cooperating processes a producer process produces information that is consumed by a consumer process n n unbounded-buffer places no practical limit on the size of the buffer bounded-buffer assumes that there is a fixed buffer size (more realistic!)

Bounded-Buffer – Shared-Memory Solution n Shared data #define BUFFER_SIZE 10 typedef struct {. .

Bounded-Buffer – Shared-Memory Solution n Shared data #define BUFFER_SIZE 10 typedef struct {. . . } item; item buffer[BUFFER_SIZE]; //circular buffer int in = 0; int out = 0;

Bounded-Buffer – Insert() Method //Producer while (true) { /* Produce an item */ …

Bounded-Buffer – Insert() Method //Producer while (true) { /* Produce an item */ … /* do nothing no free buffers */ while (((in = (in + 1) % BUFFER SIZE) == out); buffer[in] = item; in = (in + 1) % BUFFER SIZE; }

Bounded Buffer – Remove() Method //Consumer while (true) { // do nothing -- nothing

Bounded Buffer – Remove() Method //Consumer while (true) { // do nothing -- nothing to consume while (in == out); // remove an item from the buffer item = buffer[out]; out = (out + 1) % BUFFER SIZE; return item; }

Bounded Buffer n Problems in the previous example? 1. Space efficiency: use BUFFER_SIZE-1 buffers

Bounded Buffer n Problems in the previous example? 1. Space efficiency: use BUFFER_SIZE-1 buffers 2. Only allow ONE producer and ONE consumer

Background n n n Concurrent access to shared data may result in data inconsistency

Background n n n Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes Suppose that we wanted to provide a solution to the consumer-producer problem that fills all the buffers. n We can do so by having an integer count that keeps track of the number of full buffers. Initially, count is set to 0. It is incremented by the producer after it produces a new buffer and is decremented by the consumer after it consumes a buffer.

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

Producer & Consumer //Producer while (true) { /* produce an item and put in next. Produced while (count == BUFFER_SIZE); // do nothing buffer [in] = next. Produced; in = (in + 1) % BUFFER_SIZE; count++; } //consumer while (1) { while (count == 0); // do nothing next. Consumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; /* consume the item in next. Consumed }

Question? n What problem do we have in the solution using count?

Question? n What problem do we have in the solution using count?

Race Condition n count++ could be implemented as register 1 = count register 1

Race Condition n count++ could be implemented as register 1 = count register 1 = register 1 + 1 count = register 1 n count-- could be implemented as register 2 = count register 2 = register 2 - 1 count = register 2 n Consider this execution interleaving with “count = 5” initially: S 0: producer execute register 1 = count {register 1 = 5} S 1: producer execute register 1 = register 1 + 1 {register 1 = 6} S 2: consumer execute register 2 = count {register 2 = 5} S 3: consumer execute register 2 = register 2 - 1 {register 2 = 4} S 4: producer execute count = register 1 {count = 6 } S 5: consumer execute count = register 2 {count = 4}

Race Condition & Critical-Section Problem n Race condition n Multiple processes manipulate same data

Race Condition & Critical-Section Problem n Race condition n Multiple processes manipulate same data concurrently The outcome of execution depends on the particular order in which the data access takes place Critical Section n n Multiple processes has a segment of code, called critical section which handle race condition Admission to the critical section MUST be coordinated

Solution to Critical-Section Problem MUST satisfy the following three requirements: 1. Mutual Exclusion -

Solution to Critical-Section Problem MUST satisfy the following three requirements: 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 granted Assume that each process executes at a nonzero speed No assumption concerning relative speed of the N processes

Solution to Critical-Section Problem do { Entry Section CRITICAL SECTION Exit Section REMAINDER SECTION

Solution to Critical-Section Problem do { Entry Section CRITICAL SECTION Exit Section REMAINDER SECTION } while (TRUE);

Peterson’s Solution n n n A classic software-based solution to the critical-section problem Two

Peterson’s Solution n n n A classic software-based solution to the critical-section problem Two process solution Assume that the LOAD and STORE instructions are atomic; that is, cannot be interrupted. The two processes share two variables: n int turn; n Boolean flag[2] The variable turn indicates whose turn it is to enter the critical section. If turn == i, then process Pi is allowed to execute in its 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!

Algorithm for Process Pi do { flag[i] = TRUE; turn = j; while (

Algorithm for Process Pi do { flag[i] = TRUE; turn = j; while ( flag[j] && turn == j); CRITICAL SECTION flag[i] = FALSE; REMAINDER SECTION } while (TRUE);

Peterson’s Solution n Prove this solution satisfying the three requirements n n n Exclusion

Peterson’s Solution n Prove this solution satisfying the three requirements n n n Exclusion Progress Bound waiting

Synchronization Hardware n n Many systems provide hardware support for critical section code Uniprocessors

Synchronization Hardware n n Many systems provide hardware support for critical section code Uniprocessors – could disable interrupts n n n Currently running code on a shared variable would execute without preemption (taken by nonpreemptive kernel) Generally too inefficient on multiprocessor systems n Operating systems using this not broadly scalable (interrupt disabling message passing to all processors, delay entrance to critical section) Modern machines provide special atomic hardware instructions n n n Atomic = non-interruptable test memory word and set value swap contents of two memory words

Test. And. Set Instruction n Definition: boolean Test. And. Set (boolean *target) { boolean

Test. And. Set Instruction n Definition: boolean Test. And. Set (boolean *target) { boolean rv = *target; *target = TRUE; return rv: } This function is to be executed atomically

Solution using Test. And. Set n n Shared boolean variable lock, initialized to false.

Solution using Test. And. Set n n Shared boolean variable lock, initialized to false. Solution: do { while ( Test. And. Set (&lock )); // do nothing // critical section lock = FALSE; // remainder section } while ( TRUE);

Swap Instruction n Definition: void Swap (boolean *a, boolean *b) { boolean temp =

Swap Instruction n Definition: void Swap (boolean *a, boolean *b) { boolean temp = *a; *a = *b; *b = temp: } This function is to be executed atomically

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

Solution using Swap n n 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);

Semaphore n n n Synchronization tool that does not require busy waiting Semaphore S

Semaphore n n n Synchronization tool that does not require busy waiting Semaphore S – integer variable Two standard operations modify S: wait() and signal() n Originally called P() and V() Less complicated Can only be accessed via two indivisible (atomic) operations n n wait (S) { while S <= 0; // no-op S--; } signal (S) { S++; } atomic

Semaphore as General Synchronization Tool n n Counting semaphore – integer value can range

Semaphore as General Synchronization Tool n n 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 n Also known as mutex locks Can implement a counting semaphore S as a binary semaphore Provides mutual exclusion n Semaphore S; // initialized to 1 n wait (S); Critical Section signal (S);

Semaphore as General Synchronization Tool n Counting semaphores n n Used to control access

Semaphore as General Synchronization Tool n Counting semaphores n n Used to control access to a given resource consisting of a finite number of instances The semaphore is initialized to the number of resources available

Semaphore as General Synchronization Tool n Exercise n n consider two concurrently running processes

Semaphore as General Synchronization Tool n Exercise n n consider two concurrently running processes P 1 with a statement S 1 and P 2 with a statement S 2. Suppose we require that S 2 be executed only after S 1 has completed. Solution ?

Semaphore as General Synchronization Tool n n Example n consider two concurrently running processes

Semaphore as General Synchronization Tool n n Example n consider two concurrently running processes P 1 with a statement S 1 and P 2 with a statement S 2. Suppose we require that S 2 be executed only after S 1 has completed. Solution n n Declare a semaphore synch with initial value of 0 P 1 { S 1; signal(synch); } P 2 { wait(synch); S 2; }

Semaphore Implementation n Previous semaphore implementation problem? n Spinlock, busy waiting! n How to

Semaphore Implementation n Previous semaphore implementation problem? n Spinlock, busy waiting! n How to handle it?

Semaphore Implementation with no Busy waiting n With each semaphore there is an associated

Semaphore Implementation with no Busy waiting n With each semaphore there is an associated waiting queue. Each entry in a waiting queue has two data items: n n typedef struct { int value; struct PCB *list; } semaphore; Two operations: n n 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. ) n Implementation of wait: wait (S){

Semaphore Implementation with no Busy waiting (Cont. ) n Implementation of wait: wait (S){ value--; if (value < 0) { add this process P to waiting queue } n block(P); } Implementation of signal: Signal (S){ value++; if (value <= 0) { remove a process P from the waiting queue } } wakeup(P);

Deadlock and Starvation n n Deadlock – two or more processes are waiting indefinitely

Deadlock and Starvation n n 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 n P 1 wait (S); wait (Q); wait (S); . . . signal (S); signal (Q); signal (S); Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended.

Classical Problems of Synchronization Bounded-Buffer Problem n Readers and Writers Problem n Dining-Philosophers Problem

Classical Problems of Synchronization Bounded-Buffer Problem n Readers and Writers Problem n Dining-Philosophers Problem n

Bounded-Buffer Problem n N buffers, each can hold one item Producers write items (in)

Bounded-Buffer Problem n N buffers, each can hold one item Producers write items (in) while consumers take items (out) n Using semaphores n n How many are needed?

Bounded-Buffer Problem n n N buffers, each can hold one item Semaphore mutex initialized

Bounded-Buffer Problem n n 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 (Cont. ) n The structure of the producer process do {

Bounded Buffer Problem (Cont. ) n The structure of the producer process do { // produce an item wait (empty); wait (mutex); // add the item to the buffer signal (mutex); signal (full); Question: why do we need mutex? } while (true);

Bounded Buffer Problem (Cont. ) n The structure of the consumer process do {

Bounded Buffer Problem (Cont. ) n The structure of the consumer process do { wait (full); wait (mutex); // remove an item from buffer signal (mutex); signal (empty); // consume the removed item {can this be moved inside critical-section? } } while (true); Q 1: Can we move wait(mutex) before wait(full)? Q 2: How about using two mutexes?

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

Readers-Writers Problem n A data set is shared among a number of concurrent processes n n Problem – allow multiple readers to read at the same time. Only one single writer can access the shared data at the same time (if on writer access it, no readers or other writers are allowed to access the data). Shared Data n n Readers – only read the data set; they do not perform any updates Writers – can both read and write. Data set Using semaphores n What semaphores do we need?

Readers-Writers Problem n n n Semaphore mutex initialized to 1. Semaphore wrt initialized to

Readers-Writers Problem n n n Semaphore mutex initialized to 1. Semaphore wrt initialized to 1. Integer readcount initialized to 0.

Readers-Writers Problem (Cont. ) n Exercise: Implement readers and writers

Readers-Writers Problem (Cont. ) n Exercise: Implement readers and writers

Readers-Writers Problem (Cont. ) n The structure of a writer process do { wait

Readers-Writers Problem (Cont. ) n The structure of a writer process do { wait (wrt) ; // writing is performed signal (wrt) ; } while (true)

Readers-Writers Problem (Cont. ) n The structure of a reader process do { wait

Readers-Writers Problem (Cont. ) n The structure of a reader process do { wait (mutex) ; readcount ++ ; if (readercount == 1) wait (wrt) ; signal (mutex) // reading is performed wait (mutex) ; readcount - - ; if (redacount == 0) signal (wrt) ; signal (mutex) ; } while (true)

Dining-Philosophers Problem n Shared data n n 1. 5 philosophers spend their lives thinking

Dining-Philosophers Problem n Shared data n n 1. 5 philosophers spend their lives thinking and eating. 2. Pick 2 chopsticks closest to him/her to eat 3. Pick a chopstick at a time A bowl of rice (data set) at the center of a table Semaphore chopstick [5] initialized to 1: associate each chopstick with a semaphore

Dining-Philosophers Problem (Cont. ) n The structure of Philosopher i: Do { wait (

Dining-Philosophers Problem (Cont. ) n The structure of Philosopher i: Do { wait ( chopstick[i] ); wait ( chop. Stick[ (i + 1) % 5] ); // eat signal ( chopstick[i] ); signal (chopstick[ (i + 1) % 5] ); Deadlock Problem!!! // think } while (true) ;

Problems with Semaphores n Correct use of semaphore operations: n n n signal (mutex)

Problems with Semaphores n Correct use of semaphore operations: n n n signal (mutex) …. wait (mutex) concurrent access wait (mutex) … wait (mutex) deadlock Omitting of wait (mutex) or signal (mutex) (or both) either concurrent access or dealock

Monitors n n A high-level abstraction that provides a convenient and effective mechanism for

Monitors n n A high-level abstraction that provides a convenient and effective mechanism for process synchronization Address the previous problems caused by semaphores, i. e. , timing issue, accidentally or intentionally by another process Only one process may be active within the monitor at a time Programmers do not need to code the synchronization mechanisms explicitly monitor-name { // shared variable declarations procedure P 1 (…) { …. } … procedure Pn (…) {……} } Initialization code ( …. ) { … } …

Schematic view of a Monitor

Schematic view of a Monitor

Condition Variables n n condition x, y; Two operations on a condition variable: x.

Condition Variables n n condition x, y; Two operations on a condition variable: x. wait () – a process that invokes the operation is suspended. n x. signal () – resumes one of processes (if any) that invoked x. wait (), unlike signal() in semaphore!!!! n

Monitor with Condition Variables

Monitor with Condition Variables

Solution to Dining Philosophers monitor DP { enum { THINKING; HUNGRY, EATING) state [5]

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

Solution to Dining Philosophers (cont) void test (int i) { if ( (state[(i +

Solution to Dining Philosophers (cont) 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;

Solution to Dining Philosophers (cont) Philosopher i: DP dp; dp. pickup(i); eating dp. putdown(i);

Solution to Dining Philosophers (cont) Philosopher i: DP dp; dp. pickup(i); eating dp. putdown(i); //Users do not need to do synchronization explicitly //Avoid malicious user behaviors or accidental mistakes.

Exercise #1 n A file is to be shared among different processes, each of

Exercise #1 n A file is to be shared among different processes, each of which has a unique number. The file can be accessed simultaneously by several processes, subject to the following constraint: The sum of all unique numbers associated with all the processes currently accessing the file must be less than n. Write a monitor to coordinate access to the file.

Solution n n n The pseudocode is as follows: monitor file access { int

Solution n n n The pseudocode is as follows: monitor file access { int curr_sum = 0; int n; condition c; void access_file(int my_num) { while (curr_sum + my_num >= n) c. wait(); curr_sum += my_num; } void finish_access(int my num) { curr sum -= my num; c. broadcast(); } }

Synchronization Examples n n Solaris Windows XP Linux Pthreads

Synchronization Examples n n Solaris Windows XP Linux Pthreads

Solaris Synchronization n n Implements a variety of locks to support multitasking, multithreading (including

Solaris Synchronization n n Implements a variety of locks to support multitasking, multithreading (including real-time threads), and multiprocessing Uses adaptive mutexes for efficiency when protecting data from short code segments Uses condition variables and readers-writers locks when longer sections of code need access to data Uses turnstiles to order the list of threads waiting to acquire either an adaptive mutex or reader-writer lock

Windows XP Synchronization n n Uses interrupt masks to protect access to global resources

Windows XP Synchronization n n Uses interrupt masks to protect access to global resources on uniprocessor systems Uses spinlocks on multiprocessor systems Also provides dispatcher objects which may act as either mutexes and semaphores Dispatcher objects may also provide events n An event acts much like a condition variable

Linux Synchronization n Linux: n n disables interrupts to implement short critical sections Linux

Linux Synchronization n Linux: n n disables interrupts to implement short critical sections Linux provides: n n semaphores spin locks

Pthreads Synchronization n n Pthreads API is OS-independent It provides: n n n mutex

Pthreads Synchronization n n Pthreads API is OS-independent It provides: n n n mutex locks semphores condition variables