Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I...

104
Process Synchronization (Part II) Amir H. Payberah [email protected] Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 1 / 55

Transcript of Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I...

Page 1: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Process Synchronization(Part II)

Amir H. [email protected]

Amirkabir University of Technology(Tehran Polytechnic)

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 1 / 55

Page 2: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Motivation

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 2 / 55

Page 3: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Process Synchronization

I Maintaining consistency of shared data

I Critical-Section (CS) problem

I CS solutions:• Peterson’s solution• Synchronization Hardware• Mutex lock

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 3 / 55

Page 4: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Mutex Lock

acquire() {

while (!available); /* busy wait */

available = false;

}

release() {

available = true;

}

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 4 / 55

Page 5: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Semaphores

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 5 / 55

Page 6: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Semaphore

I Synchronization tool that provides more sophisticated ways (thanMutex locks) for process to synchronize their activities.

I Semaphore S: integer variable.

I Accessed via two atomic operations: wait() and signal()

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 6 / 55

Page 7: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Semaphore

I Synchronization tool that provides more sophisticated ways (thanMutex locks) for process to synchronize their activities.

I Semaphore S: integer variable.

I Accessed via two atomic operations: wait() and signal()

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 6 / 55

Page 8: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

wait() and signal()

wait(S) {

while (S <= 0); // busy wait

S--;

}

signal(S) {

S++;

}

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 7 / 55

Page 9: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Counting and Binary Semaphore

I Counting semaphore: integer value can range over an unrestricteddomain.

I Binary semaphore: integer value can range only between 0 and 1.• Same as a mutex lock.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 8 / 55

Page 10: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Counting and Binary Semaphore

I Counting semaphore: integer value can range over an unrestricteddomain.

I Binary semaphore: integer value can range only between 0 and 1.• Same as a mutex lock.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 8 / 55

Page 11: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Semaphore Usage (1/2)

I Access control to a given resource consisting of a finite number ofinstances.

• Initialize the semaphore to the number of available resources.

• Call wait() before using a resource.

• Call signal() after releasing a resource.

• If S = 0: all resources are used, and processes that wish to use aresource will block until the count becomes greater than 0.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 9 / 55

Page 12: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Semaphore Usage (1/2)

I Access control to a given resource consisting of a finite number ofinstances.

• Initialize the semaphore to the number of available resources.

• Call wait() before using a resource.

• Call signal() after releasing a resource.

• If S = 0: all resources are used, and processes that wish to use aresource will block until the count becomes greater than 0.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 9 / 55

Page 13: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Semaphore Usage (1/2)

I Access control to a given resource consisting of a finite number ofinstances.

• Initialize the semaphore to the number of available resources.

• Call wait() before using a resource.

• Call signal() after releasing a resource.

• If S = 0: all resources are used, and processes that wish to use aresource will block until the count becomes greater than 0.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 9 / 55

Page 14: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Semaphore Usage (1/2)

I Access control to a given resource consisting of a finite number ofinstances.

• Initialize the semaphore to the number of available resources.

• Call wait() before using a resource.

• Call signal() after releasing a resource.

• If S = 0: all resources are used, and processes that wish to use aresource will block until the count becomes greater than 0.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 9 / 55

Page 15: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Semaphore Usage (1/2)

I Access control to a given resource consisting of a finite number ofinstances.

• Initialize the semaphore to the number of available resources.

• Call wait() before using a resource.

• Call signal() after releasing a resource.

• If S = 0: all resources are used, and processes that wish to use aresource will block until the count becomes greater than 0.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 9 / 55

Page 16: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Semaphore Usage (2/2)

I Consider P1 and P2 that require C1 to happen before C2.

I Create a semaphore S initialized to 0.

// Process P1

C1;

signal(S);

// Process P2

wait(S);

C2;

I The implementation still suffers from busy waiting.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 10 / 55

Page 17: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Semaphore Usage (2/2)

I Consider P1 and P2 that require C1 to happen before C2.

I Create a semaphore S initialized to 0.

// Process P1

C1;

signal(S);

// Process P2

wait(S);

C2;

I The implementation still suffers from busy waiting.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 10 / 55

Page 18: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Semaphore Usage (2/2)

I Consider P1 and P2 that require C1 to happen before C2.

I Create a semaphore S initialized to 0.

// Process P1

C1;

signal(S);

// Process P2

wait(S);

C2;

I The implementation still suffers from busy waiting.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 10 / 55

Page 19: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Semaphore Implementation with no Busy Waiting (1/2)

I With each semaphore there is an associated waiting queue.

I Each entry in a waiting queue has two data items:• Value (of type integer).• Pointer to next record in the list.

typedef struct {

int value;

struct process *list;

} semaphore;

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 11 / 55

Page 20: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Semaphore Implementation with no Busy Waiting (1/2)

I With each semaphore there is an associated waiting queue.

I Each entry in a waiting queue has two data items:• Value (of type integer).• Pointer to next record in the list.

typedef struct {

int value;

struct process *list;

} semaphore;

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 11 / 55

Page 21: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Semaphore Implementation with no Busy Waiting (1/2)

I With each semaphore there is an associated waiting queue.

I Each entry in a waiting queue has two data items:• Value (of type integer).• Pointer to next record in the list.

typedef struct {

int value;

struct process *list;

} semaphore;

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 11 / 55

Page 22: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Semaphore Implementation with no Busy Waiting (2/2)

I block: place the process invoking the operation on the appropriatewaiting queue.

I wakeup: remove one of processes in the waiting queue and place itin the ready queue.

wait(semaphore *S) {

S->value--;

if (S->value < 0) {

// add this process to S->list;

block();

}

}

signal(semaphore *S) {

S->value++;

if (S->value <= 0) {

// remove a process P from S->list;

wakeup(P);

}

}

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 12 / 55

Page 23: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Semaphore Implementation with no Busy Waiting (2/2)

I block: place the process invoking the operation on the appropriatewaiting queue.

I wakeup: remove one of processes in the waiting queue and place itin the ready queue.

wait(semaphore *S) {

S->value--;

if (S->value < 0) {

// add this process to S->list;

block();

}

}

signal(semaphore *S) {

S->value++;

if (S->value <= 0) {

// remove a process P from S->list;

wakeup(P);

}

}

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 12 / 55

Page 24: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Semaphore Implementation with no Busy Waiting (2/2)

I block: place the process invoking the operation on the appropriatewaiting queue.

I wakeup: remove one of processes in the waiting queue and place itin the ready queue.

wait(semaphore *S) {

S->value--;

if (S->value < 0) {

// add this process to S->list;

block();

}

}

signal(semaphore *S) {

S->value++;

if (S->value <= 0) {

// remove a process P from S->list;

wakeup(P);

}

}

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 12 / 55

Page 25: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Deadlock

I Deadlock: two or more processes are waiting indefinitely for an eventthat can be caused by only one of the waiting processes.

I Let S and Q be two semaphores initialized to 1.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 13 / 55

Page 26: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Deadlock

I Deadlock: two or more processes are waiting indefinitely for an eventthat can be caused by only one of the waiting processes.

I Let S and Q be two semaphores initialized to 1.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 13 / 55

Page 27: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Starvation

I Starvation: indefinite blocking.

I A process may never be removed from the semaphore queue in whichit is suspended.

I If we remove processes from the list associated with a semaphore inLIFO (last-in, first-out) order.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 14 / 55

Page 28: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Starvation

I Starvation: indefinite blocking.

I A process may never be removed from the semaphore queue in whichit is suspended.

I If we remove processes from the list associated with a semaphore inLIFO (last-in, first-out) order.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 14 / 55

Page 29: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Starvation

I Starvation: indefinite blocking.

I A process may never be removed from the semaphore queue in whichit is suspended.

I If we remove processes from the list associated with a semaphore inLIFO (last-in, first-out) order.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 14 / 55

Page 30: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Priority Inversion (1/2)

I Priority inversion: scheduling problem when lower-priority processholds a lock needed by higher-priority process.

I Example:• L < M < H, assume process H requires R, which is accessed by

process L.

• Now suppose that process M becomes runnable, therebypreempting process L.

• So, M has affected how long process H must wait.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 15 / 55

Page 31: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Priority Inversion (1/2)

I Priority inversion: scheduling problem when lower-priority processholds a lock needed by higher-priority process.

I Example:• L < M < H, assume process H requires R, which is accessed by

process L.

• Now suppose that process M becomes runnable, therebypreempting process L.

• So, M has affected how long process H must wait.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 15 / 55

Page 32: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Priority Inversion (1/2)

I Priority inversion: scheduling problem when lower-priority processholds a lock needed by higher-priority process.

I Example:• L < M < H, assume process H requires R, which is accessed by

process L.• Now suppose that process M becomes runnable, thereby

preempting process L.

• So, M has affected how long process H must wait.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 15 / 55

Page 33: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Priority Inversion (1/2)

I Priority inversion: scheduling problem when lower-priority processholds a lock needed by higher-priority process.

I Example:• L < M < H, assume process H requires R, which is accessed by

process L.• Now suppose that process M becomes runnable, thereby

preempting process L.• So, M has affected how long process H must wait.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 15 / 55

Page 34: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Priority Inversion (2/2)

I Solved via priority-inheritance protocol.

• All processes that are accessing resources needed by ahigher-priority process inherit the higher priority until they arefinished with the resources in question.

• When they are finished, their priorities revert to their original values.

I Process L temporarily inherits the priority of process H, therebypreventing process M from preempting its execution.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 16 / 55

Page 35: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Priority Inversion (2/2)

I Solved via priority-inheritance protocol.

• All processes that are accessing resources needed by ahigher-priority process inherit the higher priority until they arefinished with the resources in question.

• When they are finished, their priorities revert to their original values.

I Process L temporarily inherits the priority of process H, therebypreventing process M from preempting its execution.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 16 / 55

Page 36: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Priority Inversion (2/2)

I Solved via priority-inheritance protocol.

• All processes that are accessing resources needed by ahigher-priority process inherit the higher priority until they arefinished with the resources in question.

• When they are finished, their priorities revert to their original values.

I Process L temporarily inherits the priority of process H, therebypreventing process M from preempting its execution.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 16 / 55

Page 37: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Priority Inversion (2/2)

I Solved via priority-inheritance protocol.

• All processes that are accessing resources needed by ahigher-priority process inherit the higher priority until they arefinished with the resources in question.

• When they are finished, their priorities revert to their original values.

I Process L temporarily inherits the priority of process H, therebypreventing process M from preempting its execution.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 16 / 55

Page 38: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 17 / 55

Page 39: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Opening an Unnamed Semaphore

I sem init() initializes a semaphore to the value specified by value.

#include <semaphore.h>

int sem_init(sem_t *sem, int pshared, unsigned int value);

I pshared: whether the semaphore is shared between threads or pro-cesses.

• == 0: shared between the threads, and sem is the address of eithera variable.

• > 0: shared between processes, and sem is the address of a sharedmemory.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 18 / 55

Page 40: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Opening an Unnamed Semaphore

I sem init() initializes a semaphore to the value specified by value.

#include <semaphore.h>

int sem_init(sem_t *sem, int pshared, unsigned int value);

I pshared: whether the semaphore is shared between threads or pro-cesses.

• == 0: shared between the threads, and sem is the address of eithera variable.

• > 0: shared between processes, and sem is the address of a sharedmemory.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 18 / 55

Page 41: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Destroying an Unnamed Semaphore

I sem destroy() destroys the semaphore.

#include <semaphore.h>

int sem_destroy(sem_t *sem);

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 19 / 55

Page 42: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Waiting on a Semaphore

I sem wait() decrements the value of the semaphore.

#include <semaphore.h>

int sem_wait(sem_t *sem);

I value > 0: returns immediately.

I value == 0: blocks until the semaphore value rises above 0, then itdecrements and sem wait() returns.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 20 / 55

Page 43: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Posting a Semaphore

I sem post() increments the value of the semaphore.

#include <semaphore.h>

int sem_post(sem_t *sem);

I If the semaphore value was 0 before the sem post(), and someother process is blocked waiting to decrement the semaphore, thenthat process is awoken.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 21 / 55

Page 44: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Retrieving the Current Value of a Semaphore

I sem getvalue() returns the current value of the semaphore.

#include <semaphore.h>

int sem_getvalue(sem_t *sem, int *sval);

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 22 / 55

Page 45: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Producer-Consumer Example

I Init the buffer and semaphore.

typedef struct {

char buf[BSIZE];

int nextin;

int nextout;

sem_t occupied;

sem_t empty;

sem_t mutex;

} buffer_t;

buffer_t buffer;

sem_init(&buffer.mutex, 0, 1);

sem_init(&buffer.occupied, 0, 0);

sem_init(&buffer.empty,0, BSIZE);

buffer.nextin = buffer.nextout = 0;

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 23 / 55

Page 46: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Producer-Consumer Example

I Producer

void producer(buffer_t *b, char item) {

sem_wait(&b->empty);

sem_wait(&b->mutex);

b->buf[b->nextin] = item;

b->nextin++;

b->nextin %= BSIZE;

sem_post(&b->mutex);

sem_post(&b->occupied);

}

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 24 / 55

Page 47: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Producer-Consumer Example

I Consumer

char consumer(buffer_t *b) {

char item;

sem_wait(&b->occupied);

sem_wait(&b->mutex);

item = b->buf[b->nextout];

b->nextout++;

b->nextout %= BSIZE;

sem_post(&b->mutex);

sem_post(&b->empty);

return(item);

}

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 25 / 55

Page 48: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Monitors

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 26 / 55

Page 49: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Problems with Semaphores

I Incorrect use of semaphore operations:

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

I Deadlock and starvation are possible.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 27 / 55

Page 50: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Problems with Semaphores

I Incorrect use of semaphore operations:• signal(mutex) ... wait(mutex)

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

I Deadlock and starvation are possible.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 27 / 55

Page 51: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Problems with Semaphores

I Incorrect use of semaphore operations:• signal(mutex) ... wait(mutex)• wait(mutex) ... wait(mutex)

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

I Deadlock and starvation are possible.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 27 / 55

Page 52: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Problems with Semaphores

I Incorrect use of semaphore operations:• signal(mutex) ... wait(mutex)• wait(mutex) ... wait(mutex)• Omitting of wait(mutex) or signal(mutex) (or both)

I Deadlock and starvation are possible.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 27 / 55

Page 53: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Problems with Semaphores

I Incorrect use of semaphore operations:• signal(mutex) ... wait(mutex)• wait(mutex) ... wait(mutex)• Omitting of wait(mutex) or signal(mutex) (or both)

I Deadlock and starvation are possible.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 27 / 55

Page 54: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Monitors

I A high-level abstraction for process synchronization.

I Abstract data type, internal variables only accessible by code withinthe procedure.

I Only one process may be active within the monitor at a time.

monitor monitor_name {

/* shared variable declarations */

function P1(... ) { ... }

function P2(...) { ... }

function Pn(...) { ... }

initialization code(...) { ... }

}

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 28 / 55

Page 55: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Monitors

I A high-level abstraction for process synchronization.

I Abstract data type, internal variables only accessible by code withinthe procedure.

I Only one process may be active within the monitor at a time.

monitor monitor_name {

/* shared variable declarations */

function P1(... ) { ... }

function P2(...) { ... }

function Pn(...) { ... }

initialization code(...) { ... }

}

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 28 / 55

Page 56: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Monitors

I A high-level abstraction for process synchronization.

I Abstract data type, internal variables only accessible by code withinthe procedure.

I Only one process may be active within the monitor at a time.

monitor monitor_name {

/* shared variable declarations */

function P1(... ) { ... }

function P2(...) { ... }

function Pn(...) { ... }

initialization code(...) { ... }

}

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 28 / 55

Page 57: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

A Monitor

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 29 / 55

Page 58: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Condition Variables

I condition x, y;

I Two operations are allowed on a condition variable:

• x.wait(): a process that invokes the operation is suspended untilx.signal().

• x.signal(): resumes one of processes (if any) that invokedx.wait().

• If no x.wait() on the variable, then it has no effect on the variable.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 30 / 55

Page 59: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Condition Variables

I condition x, y;

I Two operations are allowed on a condition variable:

• x.wait(): a process that invokes the operation is suspended untilx.signal().

• x.signal(): resumes one of processes (if any) that invokedx.wait().

• If no x.wait() on the variable, then it has no effect on the variable.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 30 / 55

Page 60: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Condition Variables

I condition x, y;

I Two operations are allowed on a condition variable:• x.wait(): a process that invokes the operation is suspended untilx.signal().

• x.signal(): resumes one of processes (if any) that invokedx.wait().

• If no x.wait() on the variable, then it has no effect on the variable.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 30 / 55

Page 61: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Condition Variables

I condition x, y;

I Two operations are allowed on a condition variable:• x.wait(): a process that invokes the operation is suspended untilx.signal().

• x.signal(): resumes one of processes (if any) that invokedx.wait().

• If no x.wait() on the variable, then it has no effect on the variable.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 30 / 55

Page 62: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Condition Variables

I condition x, y;

I Two operations are allowed on a condition variable:• x.wait(): a process that invokes the operation is suspended untilx.signal().

• x.signal(): resumes one of processes (if any) that invokedx.wait().

• If no x.wait() on the variable, then it has no effect on the variable.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 30 / 55

Page 63: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

A Monitor with Condition Variables

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 31 / 55

Page 64: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Condition Variables Choices

I If process P invokes x.signal(), and process Q is suspended inx.wait(), what should happen next?

• Both Q and P cannot execute in parallel. If Q is resumed, then P

must wait.

I Options include:

• Signal and wait: P waits until Q either leaves the monitor or it waitsfor another condition.

• Signal and continue: Q waits until P either leaves the monitor or itwaits for another condition.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 32 / 55

Page 65: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Condition Variables Choices

I If process P invokes x.signal(), and process Q is suspended inx.wait(), what should happen next?

• Both Q and P cannot execute in parallel. If Q is resumed, then P

must wait.

I Options include:

• Signal and wait: P waits until Q either leaves the monitor or it waitsfor another condition.

• Signal and continue: Q waits until P either leaves the monitor or itwaits for another condition.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 32 / 55

Page 66: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Condition Variables Choices

I If process P invokes x.signal(), and process Q is suspended inx.wait(), what should happen next?

• Both Q and P cannot execute in parallel. If Q is resumed, then P

must wait.

I Options include:

• Signal and wait: P waits until Q either leaves the monitor or it waitsfor another condition.

• Signal and continue: Q waits until P either leaves the monitor or itwaits for another condition.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 32 / 55

Page 67: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Condition Variables Choices

I If process P invokes x.signal(), and process Q is suspended inx.wait(), what should happen next?

• Both Q and P cannot execute in parallel. If Q is resumed, then P

must wait.

I Options include:• Signal and wait: P waits until Q either leaves the monitor or it waits

for another condition.

• Signal and continue: Q waits until P either leaves the monitor or itwaits for another condition.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 32 / 55

Page 68: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Condition Variables Choices

I If process P invokes x.signal(), and process Q is suspended inx.wait(), what should happen next?

• Both Q and P cannot execute in parallel. If Q is resumed, then P

must wait.

I Options include:• Signal and wait: P waits until Q either leaves the monitor or it waits

for another condition.• Signal and continue: Q waits until P either leaves the monitor or it

waits for another condition.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 32 / 55

Page 69: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Resuming Processes within a Monitor

I If several processes queued on condition x, and x.signal() exe-cuted, which should be resumed?

I FCFS (First-Come, First-Served) frequently not adequate.

I Conditional-wait construct of the form x.wait(c):• Where c is priority number.• Process with lowest number (highest priority) is scheduled next.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 33 / 55

Page 70: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Resuming Processes within a Monitor

I If several processes queued on condition x, and x.signal() exe-cuted, which should be resumed?

I FCFS (First-Come, First-Served) frequently not adequate.

I Conditional-wait construct of the form x.wait(c):• Where c is priority number.• Process with lowest number (highest priority) is scheduled next.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 33 / 55

Page 71: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Resuming Processes within a Monitor

I If several processes queued on condition x, and x.signal() exe-cuted, which should be resumed?

I FCFS (First-Come, First-Served) frequently not adequate.

I Conditional-wait construct of the form x.wait(c):• Where c is priority number.• Process with lowest number (highest priority) is scheduled next.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 33 / 55

Page 72: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Single Resource Allocation

I Allocate a single resource among competing processes using prioritynumbers that specify the maximum time a process plans to use theresource.

R.acquire(t);

...

access the resource;

...

R.release();

I Where R is an instance of type ResourceAllocator monitor.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 34 / 55

Page 73: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

A Monitor to Allocate Single Resource

monitor ResourceAllocator {

boolean busy;

condition x;

void acquire(int time) {

if (busy)

x.wait(time);

busy = true;

}

void release() {

busy = false;

x.signal();

}

initialization code() {

busy = false;

}

}

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 35 / 55

Page 74: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Classical Problems ofSynchronization

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 36 / 55

Page 75: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Classical Problems of Synchronization

I Bounded-Buffer Problem

I Readers and Writers Problem

I Dining-Philosophers Problem

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 37 / 55

Page 76: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Bounded-Buffer Problem

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 38 / 55

Page 77: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Bounded-Buffer Problem (1/3)

I n buffers, each can hold one item.

I Semaphore mutex initialized to the value 1.

I Semaphore full initialized to the value 0.

I Semaphore empty initialized to the value n.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 39 / 55

Page 78: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Bounded-Buffer Problem (2/3)

I The structure of the producer process

do {

...

/* produce an item in next produced */

...

wait(empty);

wait(mutex);

...

/* add next produced to the buffer */

...

signal(mutex);

signal(full);

} while (true);

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 40 / 55

Page 79: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Bounded-Buffer Problem (3/3)

I The structure of the consumer process

do {

wait(full);

wait(mutex);

...

/* remove an item from buffer to next consumed */

...

signal(mutex);

signal(empty);

...

/* consume the item in next consumed */

...

} while (true);

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 41 / 55

Page 80: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Readers and Writers Problem

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 42 / 55

Page 81: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Readers and Writers Problem (1/3)

I A shared data set among a number of concurrent processes:• Readers: only read the data set; they do not perform any updates.• Writers: can both read and write.

I Problem: allow multiple readers to read at the same time, only onesingle writer can access the shared data at the same time.

I Shared Data• Semaphore rw mutex initialized to 1.• Semaphore mutex initialized to 1.• Integer read count initialized to 0.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 43 / 55

Page 82: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Readers and Writers Problem (1/3)

I A shared data set among a number of concurrent processes:• Readers: only read the data set; they do not perform any updates.• Writers: can both read and write.

I Problem: allow multiple readers to read at the same time, only onesingle writer can access the shared data at the same time.

I Shared Data• Semaphore rw mutex initialized to 1.• Semaphore mutex initialized to 1.• Integer read count initialized to 0.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 43 / 55

Page 83: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Readers and Writers Problem (1/3)

I A shared data set among a number of concurrent processes:• Readers: only read the data set; they do not perform any updates.• Writers: can both read and write.

I Problem: allow multiple readers to read at the same time, only onesingle writer can access the shared data at the same time.

I Shared Data• Semaphore rw mutex initialized to 1.• Semaphore mutex initialized to 1.• Integer read count initialized to 0.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 43 / 55

Page 84: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Readers and Writers Problem (2/3)

I The writer process.

do {

wait(rw_mutex);

...

/* writing is performed */

...

signal(rw_mutex);

} while (true);

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 44 / 55

Page 85: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Readers and Writers Problem (3/3)

I The reader process.

do {

wait(mutex);

read_count++;

if (read_count == 1)

wait(rw_mutex);

signal(mutex);

...

/* reading is performed */

...

wait(mutex);

read_count--;

if (read_count == 0)

signal(rw_mutex);

signal(mutex);

} while (true);

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 45 / 55

Page 86: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Dining-Philosophers Problem

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 46 / 55

Page 87: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Dining-Philosophers Problem (1/3)

I Philosophers spend their lives alternating thinking and eating.

I Don’t interact with their neighbors, occasionally try to pick up 2chopsticks (one at a time) to eat from bowl.

I Need both to eat, then release both when done.

I In the case of 5 philosophers:• Shared data: bowl of rice (data set)• Shared data: semaphore chopstick[5] initialized to 1

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 47 / 55

Page 88: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Dining-Philosophers Problem (1/3)

I Philosophers spend their lives alternating thinking and eating.

I Don’t interact with their neighbors, occasionally try to pick up 2chopsticks (one at a time) to eat from bowl.

I Need both to eat, then release both when done.

I In the case of 5 philosophers:• Shared data: bowl of rice (data set)• Shared data: semaphore chopstick[5] initialized to 1

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 47 / 55

Page 89: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Dining-Philosophers Problem (2/3)

I The structure of philosopher i:

semaphore chopstick[5];

do {

wait(chopstick[i]);

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

...

/* eat for awhile */

...

signal(chopstick[i]);

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

...

/* think for awhile */

...

} while (true);

I What is the problem with this algorithm?

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 48 / 55

Page 90: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Dining-Philosophers Problem (3/3)

I Deadlock handling

• At most 4 philosophers to sit simultaneously.

• Allow a philosopher to pick up the forks only if both are available.

• Use an asymmetric solution: an odd-numbered philosopher picks upfirst the left chopstick and then the right chopstick. Even-numberedphilosopher picks up first the right chopstick and then the leftchopstick.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 49 / 55

Page 91: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Dining-Philosophers Problem (3/3)

I Deadlock handling

• At most 4 philosophers to sit simultaneously.

• Allow a philosopher to pick up the forks only if both are available.

• Use an asymmetric solution: an odd-numbered philosopher picks upfirst the left chopstick and then the right chopstick. Even-numberedphilosopher picks up first the right chopstick and then the leftchopstick.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 49 / 55

Page 92: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Dining-Philosophers Problem (3/3)

I Deadlock handling

• At most 4 philosophers to sit simultaneously.

• Allow a philosopher to pick up the forks only if both are available.

• Use an asymmetric solution: an odd-numbered philosopher picks upfirst the left chopstick and then the right chopstick. Even-numberedphilosopher picks up first the right chopstick and then the leftchopstick.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 49 / 55

Page 93: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Dining-Philosophers Problem (3/3)

I Deadlock handling

• At most 4 philosophers to sit simultaneously.

• Allow a philosopher to pick up the forks only if both are available.

• Use an asymmetric solution: an odd-numbered philosopher picks upfirst the left chopstick and then the right chopstick. Even-numberedphilosopher picks up first the right chopstick and then the leftchopstick.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 49 / 55

Page 94: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Dining-Philosophers with Monitor (1/3)

monitor DiningPhilosophers {

enum {THINKING; HUNGRY, EATING} state[5];

condition self[5];

initialization_code() {

for (int i = 0; i < 5; i++)

state[i] = THINKING;

}

void test(int i) {

if ((state[i] == HUNGRY) &&

(state[(i + 4) % 5] != EATING) &&

(state[(i + 1) % 5] != EATING)) {

state[i] = EATING ;

self[i].signal() ;

}

}

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 50 / 55

Page 95: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Dining-Philosophers with Monitor (2/3)

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

}

}

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 51 / 55

Page 96: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Dining-Philosophers with Monitor (3/3)

I Each philosopher i invokes the operations pickup() andputdown() in the following sequence:

DiningPhilosophers.pickup(i);

EAT

DiningPhilosophers.putdown(i);

I No deadlock, but starvation is possible.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 52 / 55

Page 97: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Summary

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 53 / 55

Page 98: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Summary

I Semaphore: counting semaphore and binary semaphore

I Waiting queue to prevent busy waiting

I Deadlock and starvation

I Priority inversion

I Monitor: a high-level abstraction

I Classical problems: bounded-buffer, reader/writer, dining philoso-pher

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 54 / 55

Page 99: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Summary

I Semaphore: counting semaphore and binary semaphore

I Waiting queue to prevent busy waiting

I Deadlock and starvation

I Priority inversion

I Monitor: a high-level abstraction

I Classical problems: bounded-buffer, reader/writer, dining philoso-pher

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 54 / 55

Page 100: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Summary

I Semaphore: counting semaphore and binary semaphore

I Waiting queue to prevent busy waiting

I Deadlock and starvation

I Priority inversion

I Monitor: a high-level abstraction

I Classical problems: bounded-buffer, reader/writer, dining philoso-pher

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 54 / 55

Page 101: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Summary

I Semaphore: counting semaphore and binary semaphore

I Waiting queue to prevent busy waiting

I Deadlock and starvation

I Priority inversion

I Monitor: a high-level abstraction

I Classical problems: bounded-buffer, reader/writer, dining philoso-pher

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 54 / 55

Page 102: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Summary

I Semaphore: counting semaphore and binary semaphore

I Waiting queue to prevent busy waiting

I Deadlock and starvation

I Priority inversion

I Monitor: a high-level abstraction

I Classical problems: bounded-buffer, reader/writer, dining philoso-pher

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 54 / 55

Page 103: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Summary

I Semaphore: counting semaphore and binary semaphore

I Waiting queue to prevent busy waiting

I Deadlock and starvation

I Priority inversion

I Monitor: a high-level abstraction

I Classical problems: bounded-buffer, reader/writer, dining philoso-pher

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 54 / 55

Page 104: Process Synchronization (Part II) - Amir H. PayberahProcess Synchronization I Maintainingconsistencyof shared data I Critical-Section (CS) problem I CS solutions: Peterson’s solution

Questions?

Acknowledgements

Some slides were derived from Avi Silberschatz slides.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/19 55 / 55