Download - Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah [email protected] Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Transcript
Page 1: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Process Synchronization (Part I)

Amir H. [email protected]

Amirkabir University of Technology(Tehran Polytechnic)

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 1 / 44

Page 2: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Motivation

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 2 / 44

Page 3: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Background

I Processes can execute concurrently.

I Concurrent access to shared data may result in data inconsistency.

I Maintaining data consistency requires mechanisms to ensure theorderly execution of cooperating processes.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 3 / 44

Page 4: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Background

I Processes can execute concurrently.

I Concurrent access to shared data may result in data inconsistency.

I Maintaining data consistency requires mechanisms to ensure theorderly execution of cooperating processes.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 3 / 44

Page 5: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Background

I Processes can execute concurrently.

I Concurrent access to shared data may result in data inconsistency.

I Maintaining data consistency requires mechanisms to ensure theorderly execution of cooperating processes.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 3 / 44

Page 6: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Producer-Consumer Problem

I Providing a solution to the producer-consumer problem that fills allthe buffers.

I Having an integer counter that keeps track of the number of fullbuffers.

• Initially, counter is set to 0.• The producer produces a new buffer: increment the counter• The consumer consumes a buffer: decrement the counter

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 4 / 44

Page 7: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Producer-Consumer Problem

I Providing a solution to the producer-consumer problem that fills allthe buffers.

I Having an integer counter that keeps track of the number of fullbuffers.

• Initially, counter is set to 0.• The producer produces a new buffer: increment the counter• The consumer consumes a buffer: decrement the counter

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 4 / 44

Page 8: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Producer-Consumer Problem

I Providing a solution to the producer-consumer problem that fills allthe buffers.

I Having an integer counter that keeps track of the number of fullbuffers.

• Initially, counter is set to 0.

• The producer produces a new buffer: increment the counter• The consumer consumes a buffer: decrement the counter

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 4 / 44

Page 9: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Producer-Consumer Problem

I Providing a solution to the producer-consumer problem that fills allthe buffers.

I Having an integer counter that keeps track of the number of fullbuffers.

• Initially, counter is set to 0.• The producer produces a new buffer: increment the counter

• The consumer consumes a buffer: decrement the counter

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 4 / 44

Page 10: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Producer-Consumer Problem

I Providing a solution to the producer-consumer problem that fills allthe buffers.

I Having an integer counter that keeps track of the number of fullbuffers.

• Initially, counter is set to 0.• The producer produces a new buffer: increment the counter• The consumer consumes a buffer: decrement the counter

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 4 / 44

Page 11: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Producer

I Producer

while (true) {

/* produce an item in next produced */

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

buffer[in] = next_produced;

in = (in + 1) % BUFFER_SIZE;

counter++;

}

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 5 / 44

Page 12: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Consumer

I Consumer

while (true) {

while (counter == 0); /* do nothing */

next_consumed = buffer[out];

out = (out + 1) % BUFFER_SIZE;

counter--;

/* consume the item in next consumed */

}

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 6 / 44

Page 13: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Race Condition

I counter++ could be implemented asregister1 = counter

register1 = register1 + 1

counter = register1

I counter-- could be implemented asregister2 = counter

register2 = register2 - 1

counter = register2

I Consider this execution interleaving with count = 5 initially:S0: producer: register1 = counter: register1 = 5

S1: producer: register1 = register1 + 1: register1 = 6

S2: consumer: register2 = counter: register2 = 5

S3: consumer: register2 = register2 - 1: register2 = 4

S4: producer: counter = register1: counter = 6

S5: consumer: counter = register2: counter = 4

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

Page 14: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Race Condition

I counter++ could be implemented asregister1 = counter

register1 = register1 + 1

counter = register1

I counter-- could be implemented asregister2 = counter

register2 = register2 - 1

counter = register2

I Consider this execution interleaving with count = 5 initially:S0: producer: register1 = counter: register1 = 5

S1: producer: register1 = register1 + 1: register1 = 6

S2: consumer: register2 = counter: register2 = 5

S3: consumer: register2 = register2 - 1: register2 = 4

S4: producer: counter = register1: counter = 6

S5: consumer: counter = register2: counter = 4

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

Page 15: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Race Condition

I counter++ could be implemented asregister1 = counter

register1 = register1 + 1

counter = register1

I counter-- could be implemented asregister2 = counter

register2 = register2 - 1

counter = register2

I Consider this execution interleaving with count = 5 initially:S0: producer: register1 = counter: register1 = 5

S1: producer: register1 = register1 + 1: register1 = 6

S2: consumer: register2 = counter: register2 = 5

S3: consumer: register2 = register2 - 1: register2 = 4

S4: producer: counter = register1: counter = 6

S5: consumer: counter = register2: counter = 4

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

Page 16: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

The Critical-Section (CS)Problem

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 8 / 44

Page 17: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

The Critical-Section Problem (1/2)

I Consider system of n processes {p0, p1, · · · , pn−1}.

I Each process has CS segment of code.

• Process may be changing common variables, updating table, writingfile, etc.

• When one process in CS, no other may be in its CS.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 9 / 44

Page 18: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

The Critical-Section Problem (1/2)

I Consider system of n processes {p0, p1, · · · , pn−1}.

I Each process has CS segment of code.

• Process may be changing common variables, updating table, writingfile, etc.

• When one process in CS, no other may be in its CS.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 9 / 44

Page 19: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

The Critical-Section Problem (1/2)

I Consider system of n processes {p0, p1, · · · , pn−1}.

I Each process has CS segment of code.• Process may be changing common variables, updating table, writing

file, etc.

• When one process in CS, no other may be in its CS.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 9 / 44

Page 20: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

The Critical-Section Problem (1/2)

I Consider system of n processes {p0, p1, · · · , pn−1}.

I Each process has CS segment of code.• Process may be changing common variables, updating table, writing

file, etc.• When one process in CS, no other may be in its CS.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 9 / 44

Page 21: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

The Critical-Section Problem (2/2)

I CS problem is to design protocol to solve this.

I Each process must ask permission to enter CS in entry section, mayfollow CS with exit section, then remainder section.

General structure of process Pi .

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 10 / 44

Page 22: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

CS Problem Solution Requirements (1/3)

I Mutual Exclusion: if process Pi is executing in its CS, then no otherprocesses can be executing in their CSs.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 11 / 44

Page 23: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

CS Problem Solution Requirements (2/3)

I Progress: if no process is executing in its CS and there exist someprocesses that wish to enter their CS, then the selection of the pro-cesses that will enter the CS next cannot be postponed indefinitely.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 12 / 44

Page 24: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

CS Problem Solution Requirements (3/3)

I Bounded Waiting: a bound must exist on the number of times thatother processes are allowed to enter their CSs after a process hasmade a request to enter its CS and before that request is granted.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 13 / 44

Page 25: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

CS Handling in OS

I Two approaches depending on if kernel is preemptive or non-preemptive.

I Preemptive: allows preemption of process when running in kernelmode.

I Non-preemptive: runs until exits kernel mode, blocks, or voluntarilyyields CPU.

• Essentially free of race conditions in kernel mode.

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

Page 26: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

CS Handling in OS

I Two approaches depending on if kernel is preemptive or non-preemptive.

I Preemptive: allows preemption of process when running in kernelmode.

I Non-preemptive: runs until exits kernel mode, blocks, or voluntarilyyields CPU.

• Essentially free of race conditions in kernel mode.

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

Page 27: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

CS Handling in OS

I Two approaches depending on if kernel is preemptive or non-preemptive.

I Preemptive: allows preemption of process when running in kernelmode.

I Non-preemptive: runs until exits kernel mode, blocks, or voluntarilyyields CPU.

• Essentially free of race conditions in kernel mode.

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

Page 28: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Peterson’s Solution

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 15 / 44

Page 29: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Peterson’s Solution

I Two-process solution.

I The two processes share two variables:• int turn• boolean flag[2]

I turn: indicates whose turn it is to enter the CS.

I flag: indicates if a process is ready to enter the CS, i.e.,flag[i] = true implies that process Pi is ready.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 16 / 44

Page 30: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Peterson’s Solution

I Two-process solution.

I The two processes share two variables:• int turn• boolean flag[2]

I turn: indicates whose turn it is to enter the CS.

I flag: indicates if a process is ready to enter the CS, i.e.,flag[i] = true implies that process Pi is ready.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 16 / 44

Page 31: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Peterson’s Solution

I Two-process solution.

I The two processes share two variables:• int turn• boolean flag[2]

I turn: indicates whose turn it is to enter the CS.

I flag: indicates if a process is ready to enter the CS, i.e.,flag[i] = true implies that process Pi is ready.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 16 / 44

Page 32: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Peterson’s Solution

I Two-process solution.

I The two processes share two variables:• int turn• boolean flag[2]

I turn: indicates whose turn it is to enter the CS.

I flag: indicates if a process is ready to enter the CS, i.e.,flag[i] = true implies that process Pi is ready.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 16 / 44

Page 33: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Algorithm for Process Pi

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 17 / 44

Page 34: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

CS Requirements

I Provable that the three CS requirement are met:

1 Mutual exclusion is preserved:Pi enters CS only if: either flag[j] = false or turn = i

2 Progress requirement is satisfied.3 Bounded-waiting requirement is met.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 18 / 44

Page 35: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

CS Requirements

I Provable that the three CS requirement are met:1 Mutual exclusion is preserved:

Pi enters CS only if: either flag[j] = false or turn = i

2 Progress requirement is satisfied.3 Bounded-waiting requirement is met.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 18 / 44

Page 36: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

CS Requirements

I Provable that the three CS requirement are met:1 Mutual exclusion is preserved:

Pi enters CS only if: either flag[j] = false or turn = i2 Progress requirement is satisfied.

3 Bounded-waiting requirement is met.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 18 / 44

Page 37: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

CS Requirements

I Provable that the three CS requirement are met:1 Mutual exclusion is preserved:

Pi enters CS only if: either flag[j] = false or turn = i2 Progress requirement is satisfied.3 Bounded-waiting requirement is met.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 18 / 44

Page 38: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Synchronization Hardware

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

Page 39: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Synchronization Hardware

I Many systems provide hardware support for implementing the CScode.

I Uniprocessors: could disable interrupts: running code without pre-emption.

I Modern machines provide atomic hardware instructions:atomic = non-interruptible

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 20 / 44

Page 40: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Synchronization Hardware

I Many systems provide hardware support for implementing the CScode.

I Uniprocessors: could disable interrupts: running code without pre-emption.

I Modern machines provide atomic hardware instructions:atomic = non-interruptible

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 20 / 44

Page 41: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Synchronization Hardware

I Many systems provide hardware support for implementing the CScode.

I Uniprocessors: could disable interrupts: running code without pre-emption.

I Modern machines provide atomic hardware instructions:atomic = non-interruptible

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 20 / 44

Page 42: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Solution to CS Problem Using Locks

I Protecting CS via locks.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 21 / 44

Page 43: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Atomic Instructions

I test and set

I compare and swap

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 22 / 44

Page 44: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

test and set Instruction

I Executed atomically.

I Returns the original value of passed parameter.

I Set the new value of passed parameter to true.

boolean test_and_set(boolean *target) {

boolean rv = *target;

*target = true;

return rv;

}

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 23 / 44

Page 45: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Solution Using test and set

I Shared boolean variable lock, initialized to false.

do {

while (test_and_set(&lock)); /* do nothing */

/* critical section */

lock = false;

/* remainder section */

} while (true);

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 24 / 44

Page 46: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

compare and swap Instruction

I Executed atomically.

I Returns the original value of passed parameter value.

I Set the value the value of the passed parameter new value butonly if value == expected.

int compare_and_swap(int *value, int expected, int new_value) {

int temp = *value;

if (*value == expected)

*value = new_value;

return temp;

}

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 25 / 44

Page 47: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Solution Using compare and swap

I Shared integer lock initialized to 0.

do {

while (compare_and_swap(&lock, 0, 1) != 0); /* do nothing */

/* critical section */

lock = 0;

/* remainder section */

} while (true);

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 26 / 44

Page 48: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

compare and swap and compare and swap Problem

I Although these algorithms satisfy the mutual-exclusion requirement,they do not satisfy the bounded-waiting requirement.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 27 / 44

Page 49: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Mutex Locks

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 28 / 44

Page 50: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Mutex Locks (1/2)

I Previous solutions are complicated and generally inaccessible to ap-plication programmers.

I OS designers build software tools to solve CS problem.

I Simplest is mutex lock.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 29 / 44

Page 51: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Mutex Locks (2/2)

I Protect a CS by first acquire() a lock then release() the lock.• Boolean variable indicating if lock is available or not.

I Calls to acquire() and release() must be atomic.• Usually implemented via hardware atomic instructions.

I But this solution requires busy waiting.• This lock therefore called a spinlock.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 30 / 44

Page 52: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Mutex Locks (2/2)

I Protect a CS by first acquire() a lock then release() the lock.• Boolean variable indicating if lock is available or not.

I Calls to acquire() and release() must be atomic.• Usually implemented via hardware atomic instructions.

I But this solution requires busy waiting.• This lock therefore called a spinlock.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 30 / 44

Page 53: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Mutex Locks (2/2)

I Protect a CS by first acquire() a lock then release() the lock.• Boolean variable indicating if lock is available or not.

I Calls to acquire() and release() must be atomic.• Usually implemented via hardware atomic instructions.

I But this solution requires busy waiting.• This lock therefore called a spinlock.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 30 / 44

Page 54: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

acquire() and release()

acquire() {

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

available = false;

}

release() {

available = true;

}

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 31 / 44

Page 55: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 32 / 44

Page 56: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Initializing Mutexes

I Mutexes are represented by the pthread mutex t object.

/* define and initialize a mutex named ‘mutex’ */

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 33 / 44

Page 57: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Locking Mutexes

I pthread mutex lock() locks (acquires) a pthreads mutex.

#include <pthread.h>

int pthread_mutex_lock(pthread_mutex_t *mutex);

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 34 / 44

Page 58: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Unlocking Mutexes

I pthread mutex unlock() unlocks (releases) a pthreads mutex.

#include <pthread.h>

int pthread_mutex_unlock(pthread_mutex_t *mutex);

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 35 / 44

Page 59: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Mutex Example

static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

int withdraw(struct account *account, int amount) {

pthread_mutex_lock(&mutex);

const int balance = account->balance;

if (balance < amount) {

pthread_mutex_unlock(&mutex);

return -1;

}

account->balance = balance - amount;

pthread_mutex_unlock(&mutex);

disburse_money(amount);

return 0;

}

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 36 / 44

Page 60: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Condition Variable

I Mutex is very resource consuming since the thread would becontinuously busy in this activity.

I A condition variable is a way to achieve the same goal withoutpolling.

I A condition variable allows one thread to inform other threadsabout changes in the state of a shared variable.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 37 / 44

Page 61: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Condition Variable

I Mutex is very resource consuming since the thread would becontinuously busy in this activity.

I A condition variable is a way to achieve the same goal withoutpolling.

I A condition variable allows one thread to inform other threadsabout changes in the state of a shared variable.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 37 / 44

Page 62: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Condition Variable

I Mutex is very resource consuming since the thread would becontinuously busy in this activity.

I A condition variable is a way to achieve the same goal withoutpolling.

I A condition variable allows one thread to inform other threadsabout changes in the state of a shared variable.

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 37 / 44

Page 63: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Waiting on Condition Variables

I pthread cond wait() must be called afterpthread mutex lock() and before pthread mutex unlock().

I pthread cond wait() release the mutex lock while it is waiting,so that pthread cond signal(), which is also called in the mutexshould get access and can give signal to waiting thread to awake.

#include <pthread.h>

int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 38 / 44

Page 64: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Signaling on Condition Variables

I This routine is used to wakeup the thread which is waiting for anycondition to occur.

I If in any case pthread cond signal() calls first before theexecution of pthread cond wait() then it cannot awake thethread which is waiting.

#include <pthread.h>

int pthread_cond_signal(pthread_cond_t *cond);

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 39 / 44

Page 65: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Condition Variable Calling Format

static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;

static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

s = pthread_mutex_lock(&mtx);

while (/* Check that shared variable is not in state we want */ )

pthread_cond_wait(&cond, &mtx);

/* Now shared variable is in desired state; do some work */

s = pthread_mutex_unlock(&mtx);

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 40 / 44

Page 66: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Condition Variable Example

pthread_mutex_t count_lock;

pthread_cond_t count_nonzero;

unsigned count;

decrement_count() {

pthread_mutex_lock(&count_lock);

while (count == 0)

pthread_cond_wait(&count_nonzero, &count_lock);

count = count - 1;

pthread_mutex_unlock(&count_lock);

}

increment_count() {

pthread_mutex_lock(&count_lock);

if (count == 0)

pthread_cond_signal(&count_nonzero);

count = count + 1;

pthread_mutex_unlock(&count_lock);

}

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 41 / 44

Page 67: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Summary

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 42 / 44

Page 68: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Summary

I Access to shared data

I Race condition

I The critical-section problem

I Requirements: mutual-exclusion, progress, bounding waiting

I Peterson solution

I Synchronization hardware

I Mutex lock and condition variable

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 43 / 44

Page 69: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Summary

I Access to shared data

I Race condition

I The critical-section problem

I Requirements: mutual-exclusion, progress, bounding waiting

I Peterson solution

I Synchronization hardware

I Mutex lock and condition variable

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 43 / 44

Page 70: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Summary

I Access to shared data

I Race condition

I The critical-section problem

I Requirements: mutual-exclusion, progress, bounding waiting

I Peterson solution

I Synchronization hardware

I Mutex lock and condition variable

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 43 / 44

Page 71: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Summary

I Access to shared data

I Race condition

I The critical-section problem

I Requirements: mutual-exclusion, progress, bounding waiting

I Peterson solution

I Synchronization hardware

I Mutex lock and condition variable

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 43 / 44

Page 72: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Summary

I Access to shared data

I Race condition

I The critical-section problem

I Requirements: mutual-exclusion, progress, bounding waiting

I Peterson solution

I Synchronization hardware

I Mutex lock and condition variable

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 43 / 44

Page 73: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Summary

I Access to shared data

I Race condition

I The critical-section problem

I Requirements: mutual-exclusion, progress, bounding waiting

I Peterson solution

I Synchronization hardware

I Mutex lock and condition variable

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 43 / 44

Page 74: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Summary

I Access to shared data

I Race condition

I The critical-section problem

I Requirements: mutual-exclusion, progress, bounding waiting

I Peterson solution

I Synchronization hardware

I Mutex lock and condition variable

Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 43 / 44

Page 75: Process Synchronization (Part I) · Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran

Questions?

Acknowledgements

Some slides were derived from Avi Silberschatz slides.

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