operating systems lecture - process synchronization

52
7/23/2019 Operating Systems Lecture - Process Synchronization http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 1/52  Synchronization Daniel Hagimont (INPT) Thanks to Noël De Palma, Fabienne Boyer and Arnaud Legrand (UJF)

Upload: nhu-tung-doan

Post on 18-Feb-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 1/52

 

Synchronization

Daniel Hagimont (INPT)

Thanks to Noël De Palma, Fabienne Boyer and

Arnaud Legrand (UJF)

Page 2: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 2/52

 

Problem statement

(1) y := read_account(1);

(2) x := read_account(2);

(3) x := x + y;

(4) write_account(2, x);

(5) write_account(1, 0);

Account 2 is shared between both executions

Variables x, y, v are local

Executions are performed in parallel and instructions can be

intertwined

(1) (2) (3) (4) (5) (a) (b) (c) is consistent (200/200)(0,300)

(1) (a) (b) (c) (2) (3) (4) (5) is consistent (200/200)(0,300)

(1) (2) (a) (3) (b) (4) (c) (5) is not consistent (200/200)(0,100)

(a) v := read_account(2);

(b) v = v - 100;

(c) write_account(2, v);

Page 3: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 3/52

 

Problem statement

N processes all competing to use some shared data

A critical section is a code fragment, in which the shared

data is accessed

Problem: Ensure shared data consistency

Ensure mutual exclusion

When one process is executing in one critical section, noother process is allowed to execute in this critical section

Page 4: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 4/52

 

Desired properties

Mutual Exclusion

Only one thread can be in a given critical section at a time

Progress

If no process currently in a given critical section, one

of the processes trying to enter will eventually get in

Fairness

No starvation

Page 5: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 5/52

 

Critical section

n processes: P0, P1, .., Pn

P0, P1, .., Pn use a set of shared variables a, b, c, …

Structure of a process Pi :

<enter section> // enter mutex

<access a,b,c,..> // critical section<exit section> // leave mutex

Page 6: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 6/52

 

Software implementation (1)

No mutual exclusion if context switch at (1)

Test and set are not atomic

Shared data :

  boolean busy = false;

Pi :  hile !busy" ; !#"   // busy waiting

  busy = true;

  <critical section>

  busy = false;

Page 7: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 7/52

 

Software implementation (2)

Mutual exclusion

Can be generalized to N processes

Progress issue

Shared data :

  int turn = $; // turn = i : Pi%s turn to enter 

Pi !$ or #":

  hile !turn &= i";   // busy waiting

  <critical section>

  turn =#'i;

Page 8: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 8/52

 

Software implementation (3)

Mutual exclusion

Difficult to generalize to N processes

Can block (deadlock)

Shared data :

  boolean demand()* = +false, false; // Pi as- to enter 

Pi !$ or #":

  demand(i* = true;  hile !demand(#'i*" ;  // busy waiting

  <critical section>

  demand(i* = false;

Page 9: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 9/52

 

Software implementation (4)Peterson algorithm

Shared data :

  int turn = 0; // Pi's turn

  boolean demand[2] = {false, false}; // Pi ask to enter

Pi (0 or 1):  demand[i] = true;

  turn = 1-i;

  while (demand[1-i] && (turn = 1-i)) ;

<critical section>

  demand[i] = false;

Difficult to generalize to N processes

Page 10: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 10/52

 

Software implementation ()!amport algorithm

Shared data :

  boolean choice[N] = {false, … false};

  int num[N] = {0, … 0};

Pi:

  choice[i] = true;

  int turn = 0;  for (int k=0;k<N;k++) turn = max(turn, num[k]);

  num[i] = turn + 1;

  choice[i] = false;

  for (int k=0;k<N;k++) {  while (choice[k]);

  while ((num[k] != 0) && ((num[k],k) < (num[i],i)));

  }

  <critical section>

  num[i] = 0;

Page 11: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 11/52

 

Synthesis

Software solutions

Complex

Not very efficient

Hardware solutions

Masking interrupts

Test&Set

Page 12: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 12/52

 

"as#ing interr$pts

Entry section : mask the IT

Exit section : unmask the IT

Cannot control the time spent in critical section

Acceptable if the critical section exec time is

short Cannot be used with multiprocessors

Page 13: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 13/52

 

%est&Set instr$ction

Most CPUs support atomic read-[modify-]write

Example: int test_and_set (int *lockp);

Atomically sets *lockp = 1 and returns old value

int estSet !int 0b" +

// set b to #, and return initial value of b

  int res = 0b;

  0b = #;

  return res;

Page 14: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 14/52

 

%est&Set critical section

Busy waiting

Starvation issue (not FIFO)

Shared data :

  int busy = 0; // false

Pi:

  while (Test&Set (&busy));  <critical section>

  busy = 0;

Page 15: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 15/52

 

Sleep and wa#e $p sol$tions

Previous solution disadvantage :

CPU wasting (polling)

Sleep and wake up solutions :

Block a process when it cannot enter a critical section

Wake up when the critical section is free

Different abstractions

Lock  Semaphore

Monitor

Page 16: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 16/52

 

!oc#s

Simple synchronization primitives

Lock/unlock function

Only one process can go through a lock at the same time

Based on sleep/wakeup

Different interfaces, implementations, properties (fifo ...)

e.g. Thread packages :

void mutex_init (mutex_t *m, ...);

void mutex_lock (mutex_t *m); int mutex_trylock (mutex_t *m);

void mutex_unlock (mutex_t *m);

Page 17: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 17/52

 

!oc# implementation (1'2)

ty1edef struct +int busy; // the loc- ta-en or free1roc2ctxt2list aitin3s; // aitin3 1rocesses

loc-;

void 4nit5oc-!loc- 0l" +6as-!";l'>busy = false;

init5ist!!l'>aitin3s"";7nmas-!";

 

true

P3   P2

waitings

busy

lock

Page 18: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 18/52

 

!oc# implementation (2'2)

void loc-!loc- 0l" +6as-!";

 hile !l'>busy"Sus1end!!l'>aitin3s"";

l'>busy = true;7nmas-!";

void unloc-!loc- 0l" +

  1roc2ctxt a-ed;

6as-!";l'>busy = false;if !a-ed = 8et9irst!!!l'>aitin3s""""

  a-eu1!a-ed";

  7nmas-!";

Why this loop ?

Starvation risk 

Page 19: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 19/52

 

S$spend and a#e$pimplementation

Shared data:  1roc2ctxt actif;

void Sus1end! 1roc2ctxt2list 0ueue" +

  1roc2ctxt 0old = actif;  1ut5ast!ueue, actif";  actif = 3et!eadyueue";  ctxtSa1!actif, old"; 

 void a-e71!1roc2ctxt 01" +  1ut!eadyueue, 1";

Page 20: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 20/52

 

!oc# implementation (*+)

void loc-!loc- 0l" +6as-!";if !l'>busy"

Sus1end!!l'>aitin3s"";l'>busy = true;7nmas-!";

void unloc-!loc- 0l" +1roc2ctxt a-ed;6as-!";if !a-ed = 8et9irst!!!l>aitin3s""""

a-eu1!a-ed";else l'>busy = false;7nmas-!";

Page 21: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 21/52

 

Prod$cer Cons$mer e,ample

Producer 

Producer 

onsumer 

onsumer 

onsumer 9ixed si?e buffer 

Fixed size buffer

Variable number of producers and consumers

Page 22: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 22/52

 

Prod$cer Cons$mer e,ample

Shared data:  int bufferS? = @;

  int in = $, out = $, nb = $;  6s3 buffer(* = ne 6s3(bufferS?*;

1roduce !6s3 ms3" +buffer(in* = ms3;in = in A # B bufferS?;nbAA;

6s3 onsume +6s3 ms3 = buffer(out*;out = out A # B bufferS?;nb'';

  return ms3;

Page 23: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 23/52

 

Prod$cer Cons$mer with loc#sloc#s are not s$fficient

Shared data:

  int bufferS? = @;  int in = $, out = $, nb = $;  6s3 buffer(* = ne 6s3(bufferS?*;

  5oc- mutex = ne 5oc-!";

1roduce !6s3 ms3" +mutex.loc-!";hile !nb == bufferS?" +

mutex.unloc-!";yield!"; // slee1mutex.loc-!";

buffer(in* = ms3;in = in A # B bufferS?;nbAA;mutex.unloc-!";

6s3 onsume +

mutex.loc-!";hile !nb == $" +

mutex.unloc-!";yield!"; // slee1mutex.loc-!";

6s3 ms3 = buffer(out*;out = out A # B bufferS?;nb'';mutex.unloc-!";

  return ms3;

+busyaitin3

Page 24: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 24/52

 

-igher synchronizationabstractions

Principles

Use application's semantic to suspend/wake up a

process that wait for a condition to happen

Examples Semaphore

Monitor

Page 25: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 25/52

 

Semaphores (Di.#stra/ 10)

Semaphore S :

counter S.c; //Model a ressource number or a

condition

waiting queue S.f; //Waiting processes

Think of a semaphore as a purse with a certain number of

tokens

Suspend when no more token

Wake up when token released

A Semaphore is initialized with an integer N

Accessed through P() and V() operations

Page 26: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 26/52

 

Semaphores (Di.#stra/ 10)

ait!" ou P !":S.c''if S.c < $ do +

// no more free resources  1ut!myself, S.f";

  sus1end!"; // sus1ension

si3nal!" ou C !":S.cAA;if !S.c <= $" do +

  // at least # aitin3 1rocessP = 3et!S.f";

  a-eu1!1";

 ritical sectionritical section

emember real im1lementations reuire loer level loc-in3 !e.3. interru1tion mana3ement"

Semaphores

Page 27: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 27/52

 

Semaphores Counter S.c == S.c initial + NV - NP

NV is the number of V operations executed on the semaphore

NP is the number of P operations executed on the semaphore

Counter S.c < 0

Correspond to the number of blocked processes

Counter S.c > 0 Correspond to the number of available resources

Correspond also to the number of processes that can call a P operation

without beeing blocked

Counter S.c == 0 : No more resources available and no blocked process

The next process that call P() will be blocked

Can use semaphores to implement mutual exclusion (init =1)

Could use semaphores to implement conditions

Page 28: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 28/52

 

Prod$cer Cons$mer

Producer 

Producer 

onsumer 

onsumer 

onsumer 9ixed si?e buffer 

ondition to 1roduce/consume

Produce: the buffer is not full onsume: the buffer is not em1ty

Page 29: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 29/52

 

Prod$cer Cons$mer

Can write producer/consumer with threesemaphores

Semaphore mutex initialized to 1

Used as mutex, protects buffer, in, out. . . Semaphore notempty initialized to 0 ( number of≈

items)

To block consumer when buffer is empty Semaphore notfull initialized to N ( number of≈

free locations)

To block producer when queue is full

Page 30: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 30/52

 

Prod$cer Cons$mer

Shared data:  int bufferS? = @;

  int in = $, out = $;  6s3 buffer(* = ne 6s3(bufferS?*;  Sema1hore not9ull = ne Sema1hore!bufferS?";

Sema1hore notDm1ty = ne Sema1hore!$";

Sema1hore mutex = ne Sema1hore!#";

1roduce !6s3 ms3" +not9ull.P!";mutex.P!";

buffer(in* = ms3;  in = in A # B bufferS?;

mutex.C!";notDm1ty.C!";

6s3 onsume +notDm1ty.P!";mutex.P!";

6s3 ms3 = buffer(out*;  out = out A # B bufferS?;

mutex.C!";not9ull.C!";

  return ms3;

Prod$cer Cons$mer

Page 31: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 31/52

 

Prod$cer Cons$mer(improe parallelism)

Shared data:  int bufferS? = @;

  int in = $, out = $;  6s3 buffer(* = ne 6s3(bufferS?*;  Sema1hore not9ull = ne Sema1hore!bufferS?";

Sema1hore notDm1ty = ne Sema1hore!$";

Sema1hore mutex4n = ne Sema1hore!#";  Sema1hore mutexEut = ne Sema1hore!#";

1roduce !6s3 ms3" +not9ull.P!";mutex4n.P!";

buffer(in* = ms3;  in = in A # B bufferS?;

mutex4n.C!";notDm1ty.C!";

6s3 onsume +notDm1ty.P!";mutexEut.P!";

6s3 ms3 = buffer(out*;  out = out A # B bufferS?;

mutexEut.C!";not9ull.C!";

  return ms3;

Page 32: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 32/52

 

%hread and Semaphore

Thread packages typically provide semaphores.

int sem_init(sem_t *sem, int pshared, unsigned int value);

int sem_post(sem_t *sem);

int sem_wait(sem_t *sem); int sem_trywait(sem_t *sem);

int sem_timedwait(sem_t *sem, const struct timespec

*abs_timeout);

int sem_getvalue(sem_t *sem, int *sval);

Page 33: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 33/52

 

Semaphore concl$sion

They are quite error prone

If you call P instead of V, you’ll have a deadlock 

If you forget to protect parts of your code, you end

up with a mutual exclusion violation If you have “tokens” of different types, it may be

hard to reason about

If by mistake you interchange the order of the P

and V, you may violate mutual exclusion or end up

with a deadlock.

That is why people have proposed higher-level language

constructs

Page 34: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 34/52

 

Deadloc#

A correct solution is not always ensured by thesemaphore :

P!mutex";

if …P!S";

  …  else

…C!S";

C!mutex";

Possible deadloc-

75D: never bloc- in a

critical section ithoutreleasin3 the section

"onitor

Page 35: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 35/52

 

"onitor Programming language construct

A Monitor contains Data

Function (f1,..,fn)

Init function

Conditions

Functions are executed in mutual exclusion

A “condition variable” is a synchronization structure (a queue

associated to a “logical condition”

wait() suspends the caller

signal() wakes up a waiting process if any, else the signal is LOS

In general, condition queues are FIFO

Page 36: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 36/52

 

"onitor

monitor <monitor'name> +

<shared variables A conditions declarations>

1rocedure init + initiali?ation code

1rocedure f# !…" +. . .

1rocedure f) !…" +

. . .1rocedure Pn !…" +

. . .

Page 37: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 37/52

 

"onitor

Only one process is running inside the monitor at a time

On a signal

Either the signal sender keep the monitor (signal sender

priority) = Signal and continue Or the signal receiver acquires the monitor (signal receiver

priority) = Signal and wait

Monitor release

When the current procedure completes

When calling a wait operation

Prod$cer Cons$mer with monitors

Page 38: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 38/52

 

Prod$cer Cons$mer with monitors

6onitor Prodons6onitor +

  int bufferS?, nb, in, out;  6s3 buffer(*;  ondition notDm1ty, not9ull;

1rocedure init!" +

  bufferS? = @;  nb = in = out = $;  buffer = ne 6s3(buffers?*;

1rocedure 1roduce!6s3 ms3" +

  if !nb==bufferS?"  not9ull.ait!";  buffer(in* = ms3;  in = in A # B bufferS?;

  nbAA;notDm1ty.si3nal!";

1rocedure consume!" : 6s3 +

  if !nb==$"  notDm1ty.ait!";  6s3 ms3 = buffer(out*;  out = out A # B bufferS?;  nb'';  not9ull.si3nal!";

Si3nal receiver 1riority

Prod$cer Cons$mer with monitors

Page 39: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 39/52

 

Prod$cer Cons$mer with monitors

Si3nal sender 1riority

6onitor Prodons6onitor +

  int bufferS?, nb, in, out;  6s3 buffer(*;  ondition notDm1ty, not9ull;

1rocedure init!" +

  bufferS? = @;  nb = in = out = $;  buffer = ne 6s3(buffers?*;

1rocedure 1roduce!6s3 ms3" +

  hile !nb==bufferS?"  not9ull.ait!";  buffer(in* = ms3;  in = in A # B bufferS?;

  nbAA;notDm1ty.si3nal!";

1rocedure consume!" : 6s3 +  hile !nb==$"  notDm1ty.ait!";  6s3 ms3 = buffer(out*;  out = out A # B bufferS?;  nb'';  not9ull.si3nal!";

Page 40: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 40/52

 

pthread synchronization

pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;

int pthread_mutex_init (pthread_mutex_t *mutex, const

pthread_mutex_attr *attr);

int pthread_mutex_destroy (pthread_mutex_t *m); int pthread_mutex_lock (pthread_mutex_t *m);

int pthread_mutex_trylock (pthread_mutex_t *m);

int pthread_mutex_unlock (pthread_mutex_t *m);

h d h i i

Page 41: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 41/52

 

pthread synchronization

pthread_cond_t vc = PTHREAD_COND_INITIALIZER;

int pthread_cond_init (pthread_cond_t *vc, const

pthread_cond_attr *attr);

int pthread_cond_destroy (pthread_cond_t *vc); int pthread_cond_wait (pthread_cond_t*,

pthread_mutex_t*);

int pthread_cond_timedwait (pthread_cond_t*,

pthread_mutex_t*, const struct timespec *abstime);

int pthread_cond_signal (pthread_cond_t *vc);

int pthread_cond_broadcast (pthread_cond_t *vc);

5 h i i

Page 42: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 42/52

 

 5aa synchronization

For each object

a lock 

a condition

Monitor principles

Synchronized methods

= executed in mutual

exclusion

wait and

notify/notifyAll to

manage conditions

class Dxam1le +

 int c1t; // shared data

 1ublic void synchroni?ed 3et!" +  if !c1t <= $" ait!";  c1t'';  1ublic void synchroni?ed 1ut!" +  c1tAA;

  notify!"; 

5 h i ti

Page 43: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 43/52

 

 5aa synchronization

Synchronize a chunk of code

Also one lock per class

synchroni?ed !oneEbF" +  < critical section >

class G +  static synchroni?ed foo!" + ...

6,ercise

Page 44: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 44/52

 

6,ercisereader'writer with semaphores

A shared document

Users can read/write the document

Multiple readers / single writer

be3inead!"  < readin3 >endead!"

be3inrite!"  < ritin3 >endrite!"

6,ercise

Page 45: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 45/52

 

6,ercisereader'writer with semaphores

Shared data:  int nbeaders = $;  Sema1hore reader = ne Sema1hore!#";

  Sema1hore riter = ne Sema1hore!#";

be3inead !" +

reader.P!";nbeaders AA;  4f !nbeaders == #"

riter.P!";reader.C!";

endrite !" +riter.C!";

endead !" +

reader.P!";nbeaders '';  4f !nbeaders == $"

riter.C!";reader.C!";

be3inrite !" +riter.P!";

Potential starvation of writers

6,ercisereader'writer with semaphores

Page 46: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 46/52

 

reader'writer with semaphores(and fairness)

Shared data:

  int nbeaders = $;  Sema1hore reader = ne Sema1hore!#";

  Sema1hore riter = ne Sema1hore!#";Sema1hore fifo = ne Sema1hore!#";

be3inead !" +

fifo.P!";reader.P!";nbeaders AA;

  if !nbeaders == #"riter.P!";

reader.C!";fifo.C!";

endrite !" +riter.C!";

endead !" +reader.P!";nbeaders '';

  if !nbeaders == $"riter.C!";

reader.C!";

be3inrite !" +  fifo.P!";

  riter.P!";fifo.C!";

6,ercise

Page 47: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 47/52

 

reader'writer with monitors

monitor eaderriter !" +  int nbeaders;  boolean riter;  ondition canead, canrite;

1rocedure init!" +

  nbeaders = $;  riter = false;

1rocedure be3inead!" +  if !riter" canead.ait!";  nbeaderAA;  canead.si3nal!";

Priority to signal receiver

1rocedure endead!" +  nbeader'';  if !nbeaders == $" canrite.si3nal!";

1rocedure be3inrite!" +

  if !!nbeaders > $" HH !riter""  canrite.ait!";  riter = true;

1rocedure endrite!" +  riter = false;  canead.si3nal!";  if !nbeaders == $" canrite.si3nal!";

Priority to readers

6,ercise

Page 48: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 48/52

 

reader'writer with monitors

monitor eaderriter !" +  int nbeaders;  boolean riter;  ondition canead, canrite;

1rocedure init!" +  nbeaders = $;  riter = false;

1rocedure be3inead!" +  if !riter" canead.ait!";  nbeaderAA;

  canead.si3nal!";

Priority to writers

1rocedure endead!" +  nbeader'';  if !nbeaders == $" canrite.si3nal!";

1rocedure be3inrite!" +  if !!nbeaders > $" HH !riter""

  canrite.ait!";  riter = true;

1rocedure endrite!" +  riter = false;  canrite.si3nal!";  4f !&rite" canead.si3nal!";

6,ercise

Page 49: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 49/52

 

reader'writer with monitors

monitor eaderriter !" +  int nbeaders;  boolean riter;  ondition ueue, nextriter;

1rocedure init!" +  nbeaders = $;  riter = false;

1rocedure be3inead!" +  if !!riter" HH !&ueue.em1ty!""

HH !&nextriter.em1ty!"""

ueue.ait!";  nbeaderAA;  ueue.si3nal!";

FIFO

1rocedure endead!" +  nbeader'';  if !nbeaders == $" +  if !nextriter.em1ty!"" ueue.si3nal!";  else nextriter.si3nal!";

1rocedure be3inrite!" +  if !!nbeaders > $" HH !riter""  ueue.ait!";  if !nbeaders > $" nextriter.ait!";  riter = true;

1rocedure endrite!" +  riter = false;  ueue.si3nal!";

6,ercise

Page 50: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 50/52

 

semaphore with monitor

monitor Sema1hore !" +  int count;  ondition 1ositive;

1rocedure init!int v$" +  count = v$;

1rocedure P!" +  if !count == $" 1ositive.ait!";

count'';

1rocedure C!" +  countAA;  1ositive.si3nal!";

6,ercise

Page 51: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 51/52

 

monitor with semaphoreShared data:

  Sema1hore mutex = ne Sema1hore!#"; // mutex of the monito  Sema1hore cond = ne Sema1hore!$"; // one 1er condition in the monitor 

monitor21roceduremutex.P!";

< 1rocedure body >mutex.C!";

cond.si3nal in a 1rocedure  if !&cond.em1ty!"" +

cond.C!";

cond.ait in a 1roceduremutex.C!";cond.P!";mutex.P!";

Priority to the sender

6,ercise

Page 52: Operating Systems Lecture - Process Synchronization

7/23/2019 Operating Systems Lecture - Process Synchronization

http://slidepdf.com/reader/full/operating-systems-lecture-process-synchronization 52/52

 

monitor with semaphoreShared data:

  Sema1hore mutex = ne Sema1hore!#"; // mutex of the monitor   Sema1hore aitin3s = ne Sema1hore!$"; // to bloc- si3nal senders  Sema1hore cond = ne Sema1hore!$"; // one 1er condition in the monitor 

monitor21roceduremutex.P!";

< 1rocedure body >  if !&aitin3s.em1ty!""

aitin3s.C!";else mutex.C!";

cond.si3nal in a 1rocedure  if !&cond.em1ty!"" +

cond.C!";aitin3s.P!";

cond.ait in a 1rocedure  if !&aitin3s.em1ty!""

aitin3s.C!";else mutex.C!";cond.P!";

Priority to the receiver