Process Synchronization: Understanding Race Conditions and Critical Regions, Slides of Operating Systems

Process synchronization, focusing on race conditions and critical regions. It covers different levels of concurrency, including multiprogramming, multithreading, multiprocessors, and multicomputers. The importance of process interaction and concurrency in software and hardware views. It also delves into race conditions in i/o and variable sharing, providing examples of single-threaded echo and producer/consumer scenarios. The concept of indivisible execution blocks, or critical regions, and proposes solutions using atomic locks, testandset instruction, swap instruction, and semaphores.

Typology: Slides

2012/2013

Uploaded on 04/25/2013

baidehi
baidehi šŸ‡®šŸ‡³

4.4

(14)

101 documents

1 / 49

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture 11
Chapter 6: Process Synchronization (cont)
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31

Partial preview of the text

Download Process Synchronization: Understanding Race Conditions and Critical Regions and more Slides Operating Systems in PDF only on Docsity!

Lecture 11

Chapter 6: Process Synchronization (cont)

Chapter 6: Process Synchronization

• Background

• The Critical-Section Problem

• Peterson’s Solution

• Synchronization Hardware

• Semaphores

• Classic Problems of Synchronization

• Monitors

• Synchronization Examples

• Atomic Transactions

o processes unaware of each other

 they must use shared resources independently, without interfering, and leave them intact for the others

P
P

resource

o processes indirectly aware of each other

 they work on common data and build some result together via the data

P
P

data

o processes directly aware of each other

 they cooperate by communicating, e.g., exchanging messages

P
P

messages

 Whether processes or threads: three basic interactions

Process Interaction & Concurrency

CPU

CPU

 Insignificant race condition in the shopping scenario

 the outcome depends on the CPU scheduling or ā€œinterleavingā€ of the threads (separately, each thread always does the same thing)

./multi_shopping grabbing the salad... grabbing the milk... grabbing the apples... grabbing the butter... grabbing the cheese...

A
B

./multi_shopping grabbing the milk... grabbing the butter... grabbing the salad... grabbing the cheese... grabbing the apples...

A
B

Race Condition

char chin, chout;

void echo() { do { chin = getchar(); chout = chin; putchar(chout); } while (...); }

A

./echo Hello world! Hello world!

Single-threaded echo

char chin, chout;

void echo() { do { chin = getchar(); chout = chin; putchar(chout); } while (...); }

B

 Significant race conditions in I/O & variable sharing

1 5 6

2 3 4 unlucky CPU scheduling



Multithreaded echo (unlucky)

./echo Hello world! ee....

Race Condition

void echo() { char chin, chout;

do { chin = getchar(); chout = chin; putchar(chout); } while (...); }

B

void echo() { char chin, chout;

do { chin = getchar(); chout = chin; putchar(chout); } while (...); }

A

./echo Hello world! Hello world!

Single-threaded echo

 Significant race conditions in I/O & variable sharing

1 5 6

2 3 4 unlucky CPU scheduling



Multithreaded echo (unlucky)

./echo Hello world! eH....

changed to local variables

Race Condition

Producer / Consumer

while (true) { /* produce an item and put in nextProduced */ while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++; }

while (true) { while (count == 0) ; // do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; /* consume the item in nextConsumed }

Race Condition

  • count++ could be implemented as

register1 = count register1 = register1 + 1 count = register

  • count-- could be implemented as

register2 = count register2 = register2 - 1 count = register

  • Consider this execution interleaving with ā€œcount = 5ā€ initially:

S0: producer execute register1 = count

{register1 = 5}

S1: producer execute register1 = register1 + 1

{register1 = 6}

S2: consumer execute register2 = count

{register2 = 5} Docsity.com

enter critical region?

exit critical region

enter critical region?

exit critical region

 critical regions can be protected from concurrent access by padding them with entrance and exit gates o a thread must try to check in, then it must check out

 We need mutual exclusion from critical regions

void echo() { char chin, chout; do {

chin = getchar(); chout = chin; putchar(chout);

} while (...); }

A B

void echo() { char chin, chout; do {

chin = getchar(); chout = chin; putchar(chout);

} while (...); }

Critical Regions

Solution to Critical-Section

Problem

1. Mutual Exclusion

o If process Pi is executing in its critical section,

o then no other processes can be executing in their

critical sections

2. Progress

o If no process is executing in its critical section

and there exist some processes that wish to

enter their critical section,

o then the selection of the processes that will

enter the critical section next cannot be

postponed indefinitely

Algorithm for Process P

i

do {

flag[i] = TRUE;

turn = j;

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

critical section

flag[i] = FALSE;

remainder section

} while (TRUE); Docsity.com

Synchronization Hardware

• Uniprocessors – could disable interrupts

– Currently running code would execute without

preemption

– what guarantees that the user process is going to

ever exit the critical region?

– meanwhile, the CPU cannot interleave any other

task

  • even unrelated to this race condition

– Generally too inefficient on multiprocessor

systems

– Operating systems using this not broadly scalable

1. thread A reaches CR and

finds the lock at 0 and sets

it in one shot, then enters

1.1’ even if B comes right

behind A, it will find that

the lock is already at 1

2. thread A exits CR, then

resets lock to 0

3. thread B finds the lock at 0

and sets it to 1 in one shot,

just before entering CR

B^ critical region

A
B
A
B
A
B
A

Atomic lock

TestAndSet Instruction

• Definition:

boolean TestAndSet (boolean *target)

boolean rv = *target;

*target = TRUE;

return rv: