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

An introduction to the critical-section problem, its objectives, background, and various solutions. It covers both software and hardware approaches, including peterson's solution, synchronization hardware, semaphores, monitors, atomic transactions, and more. The document also discusses race conditions and their significance in concurrency.

Typology: Slides

2012/2013

Uploaded on 04/25/2013

baidehi
baidehi šŸ‡®šŸ‡³

4.4

(14)

101 documents

1 / 43

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture 10
Chapter 6: Process Synchronization
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

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 shared ļ‚§ by 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);