Docsity

The Critical-Section Problems - Lecture Slides | CS 543, Study notes of Operating Systems

Toggle sidebar
bg1
1
CS370 - Operating Systems
• Background
• The Critical-Section Problem
• Synchronization Hardware
• Semaphores
• Classical Problems of Synchronization
• Critical Regions
• Monitors
Slides derived from material in “Operating System Concepts,”
by Silberschatz, Galvin, and Gagne
Process Syncronization
2
Background
• Concurrent access to shared data may result in data
inconsistency.
• Maintaining data consistency requires mechanisms to
ensure the orderly execution of cooperating processes.
• Shared-memory solution to bounded-butter problem allows
at most n – 1 items in buffer at the same time. A solution,
where all N buffers are used is not simple.
– Suppose that we modify the producer-consumer code by adding a
variable counter, initialized to 0 and incremented each time a new
item is added to the buffer
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download The Critical-Section Problems - Lecture Slides | CS 543 and more Study notes Operating Systems in PDF only on Docsity!

1

CS370 - Operating Systems

  • Background
  • The Critical-Section Problem
  • Synchronization Hardware
  • Semaphores
  • Classical Problems of Synchronization
  • Critical Regions
  • Monitors

Slides derived from material in “Operating System Concepts,” by Silberschatz, Galvin, and Gagne

Process Syncronization

Background

  • Concurrent access to shared data may result in data inconsistency.
  • Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes.
  • Shared-memory solution to bounded-butter problem allows at most n – 1 items in buffer at the same time. A solution, where all N buffers are used is not simple. - Suppose that we modify the producer-consumer code by adding a variable counter, initialized to 0 and incremented each time a new item is added to the buffer

3

  • Producer Process item nextProduced; while (1) { while (counter == BUFFER_SIZE) ; /* do nothing */ buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; counter++; }
  • Consumer Process item nextConsumed; while (1) { while (counter == 0) ; /* do nothing */ nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; }
  • Notice that manipulating counter has to be done atomically to avoid interference from the other process.

Bounded Buffer

#define BUFFER_SIZE 10 typedef struct {

... } item; item buffer[BUFFER_SIZE]; int counter = 0;

Race Condition

  • Race condition: The situation where several processes access – and manipulate shared data concurrently. The final value of the shared data depends upon which process finishes last.
  • To prevent race conditions, concurrent processes must be synchronized.

7

Bakery Algorithm

Critical section for n processes

  • Before entering its critical section, process receives a number. Holder of the smallest number enters the critical section.
  • If processes Pi and Pj receive the same number, if i < j, then Pi is served first; else Pj is served first.
  • The numbering scheme always generates numbers in increasing order of enumeration; i.e., 1,2,3,3,3,3,4,5...

Bakery Algorithm

  • Notation <≡ lexicographical order (ticket #, process id #)
    • (a,b) < (c,d) if a < c or if a = c and b < d
    • max (a 0 ,…, an-1) is a number, k, such that k ≥ ai for i in 0, …, n – 1
  • Shared data

boolean choosing[n]; int number[n];

  • Data structures are initialized to false and 0 respectively

9

Bakery Algorithm

do { choosing[i] = true; number[i] = max(number[0], number[1], …, number [n – 1])+1; choosing[i] = false; for (j = 0; j < n; j++) { while (choosing[j]) ; while ((number[j] != 0) && (number[j,j] < number[i,i])) ; } critical section number[i] = 0; remainder section } while (1);

Synchronization Hardware

  • Test and modify the content of a word atomically.

boolean TestAndSet(boolean &target) { boolean rv = target; tqrget = true;

return rv; }

13

Mutual Exclusion with Swap

  • Shared data (initialized to false): boolean lock; boolean waiting[n];
  • Process Pi do { key = true; while (key == true) Swap(lock,key); critical section lock = false; remainder section }

Semaphores

  • Synchronization tool that does not require busy waiting.
  • Semaphore S – integer variable
  • can only be accessed via two indivisible (atomic) operations wait (S): while S ≤≤≤≤ 0 do no-op; S--; signal (S): S++;

15

Critical Section of n Processes

  • Shared data:

semaphore mutex; //initially mutex = 1

  • Process Pi: do { wait(mutex); critical section signal(mutex); remainder section } while (1);

Semaphore Implementation

  • Define a semaphore as a record typedef struct { int value; struct process *L; } semaphore;
  • Assume two simple operations:
    • block suspends the process that invokes it.
    • wakeup(P) resumes the execution of a blocked process P.

19

Deadlock and Starvation

  • Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes.
  • Let S and Q be two semaphores initialized to 1 P 0 P 1 wait(S); wait(Q); wait(Q); wait(S); M M signal(S); signal(Q); signal(Q) signal(S);
  • Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended.

Two Types of Semaphores

  • Counting semaphore – integer value can range over an unrestricted domain.
  • Binary semaphore – integer value can range only between 0 and 1; can be simpler to implement.
  • Can implement a counting semaphore S as a binary semaphore.

21

Implementing S as a Binary

Semaphore

  • Data structures: binary-semaphore S1, S2; int C:
  • Initialization: S1 = 1 S2 = 0 C = initial value of semaphore S

Implementing S

  • wait operation wait(S1); C--; if (C < 0) { signal(S1); wait(S2); } signal(S1);
  • signal operation wait(S1); C ++; if (C <= 0) signal(S2); else signal(S1);

25

Bounded-Buffer Problem Producer

Process

do { … produce an item in nextp … wait(empty); wait(mutex); … add nextp to buffer … signal(mutex); signal(full); } while (1);

Bounded-Buffer Problem Consumer

Process

do { wait(full) wait(mutex); … remove an item from buffer to nextc … signal(mutex); signal(empty); … consume the item in nextc … } while (1);

27

Readers-Writers Problem

  • Shared data

semaphore mutex, wrt;

Initially

mutex = 1, wrt = 1, readcount = 0

Readers-Writers Problem Writer

Process

wait(wrt); … writing is performed … signal(wrt);

31

Dining-Philosophers Problem

  • Philosopher i: do { wait(chopstick[i]) wait(chopstick[(i+1) % 5]) … eat … signal(chopstick[i]); signal(chopstick[(i+1) % 5]); … think … } while (1);

Critical Regions

  • High-level synchronization construct
  • A shared variable v of type T, is declared as:

v: shared T

  • Variable v accessed only inside statement

region v when B do S

where B is a boolean expression.

  • While statement S is being executed, no other process can access variable v.

33

Critical Regions

  • Regions referring to the same shared variable exclude each other in time.
  • When a process tries to execute the region statement, the Boolean expression B is evaluated. If B is true, statement S is executed. If it is false, the process is delayed until B becomes true and no other process is in the region associated with v.

Example – Bounded Buffer

  • Shared data:

struct buffer { int pool[n]; int count, in, out; }

37

Implementation region x

when B do S

  • Associate with the shared variable x, the following variables: semaphore mutex, first-delay, second-delay; int first-count, second-count;
  • Mutually exclusive access to the critical section is provided by mutex.
  • If a process cannot enter the critical section because the Boolean expression B is false, it initially waits on the first- delay semaphore; moved to the second-delay semaphore before it is allowed to reevaluate B.

Implementation

  • Keep track of the number of processes waiting on first- delay and second-delay, with first-count and second- count respectively.
  • The algorithm assumes a FIFO ordering in the queuing of processes for a semaphore.
  • For an arbitrary queuing discipline, a more complicated implementation is required.

39

Monitors

  • High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent processes. monitor monitor-name { shared variable declarations procedure body P1 (…) { }... procedure body P2 (…) { ... }procedure body Pn (…) { ... } { (^) initialization code } }

Monitors

  • To allow a process to wait within the monitor, a condition variable must be declared, as condition x, y;
  • Condition variable can only be used with the operations wait and signal. - The operation x.wait(); means that the process invoking this operation is suspended until another process invokes x.signal(); - The x.signal operation resumes exactly one suspended process. If no process is suspended, then the signal operation has no effect.