Docsity

Operating Systems Synchronization Lecture, Study notes of Computer Science

Partial preview of the text

Download Operating Systems Synchronization Lecture and more Study notes Computer Science in PDF only on Docsity!

Operating SystemsOperating Systems

CMPSC 473 CMPSC 473

Synchronization Synchronization

February 26, 2008 - Lecture February 26, 2008 - Lecture 1212

Instructor: Trent Jaeger Instructor: Trent Jaeger

  • Last class:
    • Synchronization Problems and Primitives
  • Today:
    • Synchonization Solutions

More than just Exclusion

• But you also need synchronization constructs for

other than exclusion.

  • E.g. If printer queue is full, I need to wait until there is at

least 1 empty slot

  • Note that mutex_lock()/mutex_unlock() are not very

suitable to implement such synchronization

  • We need constructs to enforce orderings (e.g. A should

be done after B).

Semaphores

  • You are given a data-type Semaphore_t.
  • On a variable of this type, you are allowed
    • P(Semaphore_t) -- wait
    • V(Semaphore_t) – signal
  • Intuitive Functionality:
    • Logically one could visualize the semaphore as having a counter initially set to 0.
    • When you do a P(), you decrement the count, and need to block if the count becomes negative.
    • When you do a V(), you increment the count and you wake up 1 process from its blocked queue if not null.

Binary vs. Counting Semaphores

  • What we just discussed is a counting semaphore.
  • A binary semaphore restricts the “value” field to just 0 or 1.
  • We will mainly restrict ourselves to counting semaphores.
  • Exercise: Implement counting semaphores using binary semaphores.

Semaphores can implement Mutex

Semaphore_t m;

Mutex_lock() {

P(m);

Mutex_unlock() {

V(m);

Bounded Buffer problem

• A queue of finite size implemented as an array.

• You need mutual exclusion when adding/removing

from the buffer to avoid race conditions

• Also, you need to wait when appending to buffer

when it is full or when removing from buffer when

it is empty.

Bounded Buffer using Semaphores int BB[N]; int count, head, tail = 0; Semaphore_t m; // value initialized to 1 Semaphore_t empty; // value initialized to N Semaphore_t full; // value initialized to 0 Append(int elem) { P(empty); P(m); BB[tail] = elem; tail = (tail + 1)%N; count = count + 1; V(m); V(full); } int Remove () { P(full); P(m); int temp = BB[head]; head = (head + 1)%N; count = count - 1; V(m); V(empty); return(temp); }

Readers-writers using Semaphores Database db; int nreaders = 0; Semaphore_t m; // value initialized to 1 Semaphore_t wrt; // value initialized to 1 Reader() { P(m); nreaders++; if (nreaders == 1) P(wrt); V(m); …. Read db here … P(m); nreaders--; if (nreaders == 0) V(wrt); V(m); } Writer() { P(wrt); … Write db here … V(wrt); }

Dining Philosophers Problem

Philosophers alternate between

thinking and eating.

When eating, they need both

(left and right) chopsticks.

A philosopher can pick up only 1

chopstick at a time.

After eating, the philosopher

puts down both chopsticks.

  • Note that putting

P(chopstick[i]);

P(chopstick[(i+1)%5];

within a critical section (using say P(mutex)/V(mutex)) can avoid the deadlock.

  • But then, only 1 philosopher can eat at any time!

int state[N]; Semaphore_t s[N]; // init. to 0 Semaphore_t mutex; // init. to 1 #define LEFT (i-1)%N #define RIGHT (i+1)%N philosopher(i) { while () { take_forks(i); eat(); put_forks(i); think(); } } take_forks(i) { P(mutex); state[i] = HUNGRY; test(i); V(mutex); P(s[i]); } put_forks(i) { P(mutex); state[i] = THINKING; test(LEFT); test(RIGHT); V(mutex); } test(i) { /* can phil i eat? if so, signal that philosopher */ if (state[i] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING) { state[i] = EATING; V(s[i]); } }

Monitors

• An abstract data type consisting of

  • Shared data
  • Operations/procedures on this shared data

• External world only sees these operations (not the

shared data or how the operations and sync. are

implemented).

• Only 1 process can be “active” within the monitor

at any time

i.e. of all the processes that are executing monitor code,

there can be at most 1 process in ready queue (rest are

either blocked or not in monitor!)

  • In addition, you have a condition variable construct

available within a monitor.

  • Condition_t x, y;
  • You can perform the following operations on a condition

variable:

  • Wait(x): Process invoking this is blocked until someone does a

signal.

  • Signal(x); Resumes exactly one blocked process.
  • NOTE: If the signal comes before the wait, the signal gets

lost!!! – You need to be careful since signals are not stored

unlike semaphores.