Docsity

Understanding the Critical-Section Problem and Solutions in Concurrency, Slides of Operating Systems

Partial preview of the text

Download Understanding the Critical-Section Problem and Solutions in Concurrency and more Slides Operating Systems in PDF only on Docsity!

Lecture 10

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization

• Background

• The Critical-Section Problem

• Peterson’s Solution

• Synchronization Hardware

• Semaphores

• Classic Problems of Synchronization

• Monitors

• Synchronization Examples

• Atomic Transactions

Background

• Concurrent access to shared data may result in data

inconsistency

 concurrency is a fundamental part of O/S design

 concurrency includes

 communication among processes/threads
 sharing of, and competition for system resources
 cooperative processing of shared data
 synchronization of process/thread activities
 organized CPU scheduling
 solving deadlock and starvation problems

• Maintaining data consistency requires mechanisms to

ensure the orderly execution of cooperating processes

 Concurrency can be viewed at different levels:

multiprogramming — interaction between multiple processes running on one CPU (pseudo-parallelism)

multithreading — interaction between multiple threads running in one process

multiprocessors — interaction between multiple CPUs running multiple processes/threads (real parallelism)

multicomputers — interaction between multiple computers running distributed processes/threads

→ the principles of concurrency are basically the same in all of these categories

Process Interaction & Concurrency

Software

view

Hardware

view

Multithreaded shopping diagram and possible outputs

./multi_shopping grabbing the salad...

grabbing the milk... grabbing the apples... grabbing the butter... grabbing the cheese...

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

grabbing the cheese... grabbing the apples...

Molay, B. (2002) Unix/Linux Programming (1st Edition). Understanding

 Insignificant race condition in the shopping scenario

 there is a “ race condition ” if the outcome depends on the order of the execution

Race Condition

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

char chin, chout;

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

B

./echo Hello world! Hello world!

Single-threaded echo Multithreaded echo (lucky)

./echo Hello world! Hello world!

1 2 3

4 5 6 lucky CPU scheduling

 Significant race conditions in I/O & variable sharing

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

 Significant race conditions in I/O & variable sharing

 in this case, replacing the global variables with local variables did not solve the problem

 we actually had two race conditions here:  one race condition in the shared variables and the order of value assignment  another race condition in the shared output stream:

  • which thread is going to write to output first
  • this race persisted even after making the variables local to each thread

→ generally, problematic race conditions may occur whenever resources and/or data are sharedby processes unaware of each other or processes indirectly aware of each other

Race Condition

Producer / Consumer Problem

• Suppose that we wanted to provide a

solution to the consumer-producer problem

that fills all the buffers.

– We can do so by having an integer count that

keeps track of the number of full buffers.

  • Initially, count is set to 0.
  • incremented by the producer after it produces a new

while (true) {

/* Produce an item / while (((in = (in + 1) % BUFFER SIZE) == out) ; / do nothing -- no free buffers */ buffer[in] = item; in = (in + 1) % BUFFER SIZE;

}

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

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

 How to avoid race conditions?

 find a way to keep the instructions together  this means actually... reverting from too much interleaving and going back to “indivisible/atomic” blocks of execution!!

thread A

thread B

chin='H'

chin='e'

putchar('e')

chout='e' putchar('e')

(a) too much interleaving may create race conditions

(b) keeping “indivisible” blocks of execution avoids race conditions

thread A

thread B

chin='H'

chin='e'

putchar('H')

chout='e' putchar('e')

Race Condition

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

Critical Section

do {

entry section

critical section

exit session

remainder section

} while (TRUE);