Process Synchronization Outline Wh y d o p r o c e s s e - - PowerPoint PPT Presentation

process synchronization outline
SMART_READER_LITE
LIVE PREVIEW

Process Synchronization Outline Wh y d o p r o c e s s e - - PowerPoint PPT Presentation

Process Synchronization Outline Wh y d o p r o c e s s e s n e e d s y n c h r o n i z a t i o n ? Wh a t i s t h e c r i t i c a l - s e c t i o n p r o b l e m ? D e


slide-1
SLIDE 1

1

Process Synchronization – Outline

 Wh

y d

  • p

r

  • c

e s s e s n e e d s y n c h r

  • n

i z a t i

  • n

?

 Wh

a t i s t h e c r i t i c a l

  • s

e c t i

  • n

p r

  • b

l e m ?

 D

e s c r i b e s

  • l

u t i

  • n

s t

  • t

h e c r i t i c a l

  • s

e c t i

  • n

p r

  • b

l e m

 P

e t e r s

  • n

’ s s

  • l

u t i

  • n

 u

s i n g s y n c h r

  • n

i z a t i

  • n

h a r d w a r e

 s

e m a p h

  • r

e s

 m

  • n

i t

  • r

s

 C

l a s s i c P r

  • b

l e m s

  • f

S y n c h r

  • n

i z a t i

  • n

 Wh

a t a r e a t

  • m

i c t r a n s a c t i

  • n

s ?

slide-2
SLIDE 2

2

Why Process Synchronization ?

 P

r

  • c

e s s e s m a y c

  • p

e r a t e w i t h e a c h

  • t

h e r

 p

r

  • d

u c e r

  • c
  • n

s u m e r a n d s e r v i c e

  • r

i e n t e d s y s t e m m

  • d

e l s

 e

x p l

  • i

t c

  • n

c u r r e n t e x e c u t i

  • n
  • n

m u l t i p r

  • c

e s s

  • r

s

 C

  • p

e r a t i n g p r

  • c

e s s e s m a y s h a r e d a t a ( g l

  • b

a l s , fj l e s , e t c )

 i

m p e r a t i v e t

  • m

a i n t a i n d a t a c

  • r

r e c t n e s s

 Wh

y i s d a t a c

  • r

r e c t n e s s i n d a n g e r ?

 p

r

  • c

e s s r u n a s y n c h r

  • n
  • u

s l y , c

  • n

t e x t s w i t c h e s c a n h a p p e n a t a n y t i m e

 p

r

  • c

e s s e s m a y r u n c

  • n

c u r r e n t l y

 d

i fg e r e n t

  • r

d e r s

  • f

u p d a t i n g s h a r e d d a t a m a y p r

  • d

u c e d i fg e r e n t v a l u e s

 P

r

  • c

e s s s y n c h r

  • n

i z a t i

  • n

 t

  • c
  • r

d i n a t e u p d a t e s t

  • s

h a r e d d a t a

 o

r d e r

  • f

p r

  • c

e s s e x e c u t i

  • n

s h

  • u

l d n

  • t

a fg e c t s h a r e d d a t a

 O

n l y n e e d e d w h e n p r

  • c

e s s e s s h a r e d a t a !

slide-3
SLIDE 3

3

Producer-Consumer Data Sharing

while (true){ /* wait if bufger full */ while (counter == 10) ; /* do nothing */ /* produce data */ bufger[in] = sdata; in = (in + 1) % 10; /* update number of items in bufger */ counter++; } while (true){ /* wait if bufger empty */ while (counter == 0) ; /* do nothing */ /* consume data */ sdata = bufger[out];

  • ut = (out + 1) % 10;

/* update number of items in bufger */ counter--; }

Producer Consumer

slide-4
SLIDE 4

4

Producer-Consumer Data Sharing

while (true){ /* wait if bufger full */ while (counter == 10) ; /* do nothing */ /* produce data */ bufger[in] = sdata; in = (in + 1) % 10; /* update number of items in bufger */ R1 = load (counter); R1 = R1 + 1; counter = store (R1); } while (true){ /* wait if bufger empty */ while (counter == 0) ; /* do nothing */ /* consume data */ sdata = bufger[out];

  • ut = (out + 1) % 10;

/* update number of items in bufger */ R2 = load (counter); R2 = R2 – 1; counter = store (R2); }

Producer Consumer

slide-5
SLIDE 5

5

 S

u p p

  • s

e c

  • u

n t e r = 5

 R

a c e c

  • n

d i t i

  • n

i s a s i t u a t i

  • n

w h e r e

 s

e v e r a l p r

  • c

e s s e s c

  • n

c u r r e n t l y m a n i p u l a t e s h a r e d d a t a , a n d

 s

h a r e d d a t a v a l u e d e p e n d s

  • n

t h e

  • r

d e r

  • f

e x e c u t i

  • n

Race Condition

R1 = load (counter); R1 = R1 + 1; R2 = load (counter); R2 = R2 – 1; counter = store (R1); counter = store (R2);

Final Value in counter = 4!

R1 = load (counter); R1 = R1 + 1; R2 = load (counter); R2 = R2 – 1; counter = store (R2); counter = store (R1);

Final Value in counter = 6! Incorrect Sequence 1 Incorrect Sequence 2

slide-6
SLIDE 6

6

Critical Section Problem

 R

e g i

  • n
  • f

c

  • d

e i n a p r

  • c

e s s u p d a t i n g s h a r e d d a t a i s c a l l e d a c r i t i c a l r e g i

  • n

.

 C

  • n

c u r r e n t u p d a t i n g

  • f

s h a r e d d a t a b y m u l t i p l e p r

  • c

e s s e s i s d a n g e r

  • u

s .

 C

r i t i c a l s e c t i

  • n

p r

  • b

l e m

 h

  • w

t

  • e

n s u r e s y n c h r

  • n

i z a t i

  • n

b e t w e e n c

  • p

e r a t i n g p r

  • c

e s s e s ?

 S

  • l

u t i

  • n

t

  • t

h e c r i t i c a l s e c t i

  • n

p r

  • b

l e m

 o

n l y a l l

  • w

a s i n g l e p r

  • c

e s s t

  • e

n t e r i t s c r i t i c a l s e c t i

  • n

a t a t i m e

 P

r

  • t
  • c
  • l

f

  • r

s

  • l

v i n g t h e c r i t i c a l s e c t i

  • n

p r

  • b

l e m

 r

e q u e s t p e r m i s s i

  • n

t

  • e

n t e r c r i t i c a l s e c t i

  • n

 i

n d i c a t e a f t e r e x i t f r

  • m

c r i t i c a l s e c t i

  • n

 o

n l y p e r m i t a s i n g l e p r

  • c

e s s a t a t i m e

slide-7
SLIDE 7

7

Solution to the Critical Section Problem

 F

  • r

m a l l y s t a t e s , e a c h s

  • l

u t i

  • n

s h

  • u

l d e n s u r e

 m

u t u a l e x c l u s i

  • n

:

  • n

l y a s i n g l e p r

  • c

e s s c a n e x e c u t e i n i t s c r i t i c a l s e c t i

  • n

a t a t i m e

 p

r

  • g

r e s s : s e l e c t i

  • n
  • f

a p r

  • c

e s s t

  • e

n t e r i t s c r i t i c a l s e c t i

  • n

s h

  • u

l d b e f a i r , a n d t h e d e c i s i

  • n

c a n n

  • t

b e p

  • s

t p

  • n

e d i n d e fj n i t e l y .

 b

  • u

n d e d w a i t i n g : t h e r e s h

  • u

l d b e a fj x e d b

  • u

n d

  • n

h

  • w

l

  • n

g i t t a k e s f

  • r

t h e s y s t e m t

  • g

r a n t a p r

  • c

e s s ' s r e q u e s t t

  • e

n t e r i t s c r i t i c a l s e c t i

  • n

 O

t h e r t h a n s a t i s f y i n g t h e s e r e q u i r e m e n t s , t h e s y s t e m s h

  • u

l d a l s

  • g

u a r d a g a i n s t d e a d l

  • c

k s .

slide-8
SLIDE 8

8

Preemptive Vs. Non-preemptive Kernels

 S

e v e r a l k e r n e l p r

  • c

e s s e s s h a r e d a t a

 s

t r u c t u r e s f

  • r

m a i n t a i n i n g fj l e s y s t e m s , m e m

  • r

y a l l

  • c

a t i

  • n

, i n t e r r u p t h a n d l i n g , e t c .

 H

  • w

t

  • e

n s u r e O S e s a r e f r e e f r

  • m

r a c e c

  • n

d i t i

  • n

s ?

 N

  • n

– p r e e m p t i v e k e r n e l s

 p

r

  • c

e s s e x e c u t i n g i n k e r n e l m

  • d

e c a n n

  • t

b e p r e e m p t e d

 d

i s a b l e i n t e r r u p t s w h e n p r

  • c

e s s i s i n k e r n e l m

  • d

e

 w

h a t a b

  • u

t m u l t i p r

  • c

e s s

  • r

s y s t e m s ?

 P

r e e m p t i v e k e r n e l s

 p

r

  • c

e s s e x e c u t i n g i n k e r n e l m

  • d

e c a n b e p r e e m p t e d

 s

u i t a b l e f

  • r

r e a l

  • t

i m e p r

  • g

r a m m i n g

 m

  • r

e r e s p

  • n

s i v e

slide-9
SLIDE 9

9

Peterson’s Solution to Critical Section Problem

 S

  • f

t w a r e b a s e d s

  • l

u t i

  • n

 O

n l y s u p p

  • r

t s t w

  • p

r

  • c

e s s e s

 T

h e t w

  • p

r

  • c

e s s e s s h a r e t w

  • v

a r i a b l e s :

 i

n t t u r n ;

 i

n d i c a t e s w h

  • s

e t u r n i t i s t

  • e

n t e r t h e c r i t i c a l s e c t i

  • n

 b

  • l

e a n fm a g [ 2 ]

 i

n d i c a t e s i f a p r

  • c

e s s i s r e a d y t

  • e

n t e r i t s c r i t i c a l s e c t i

  • n
slide-10
SLIDE 10

1

 S

  • l

u t i

  • n

m e e t s a l l t h r e e r e q u i r e m e n t s

 P

a n d P 1 c a n n e v e r b e i n t h e c r i t i c a l s e c t i

  • n

a t t h e s a m e t i m e

 i

f P d

  • e

s n

  • t

w a n t t

  • e

n t e r c r i t i c a l r e g i

  • n

, P 1 d

  • e

s n

  • w

a i t i n g

 p

r

  • c

e s s w a i t s f

  • r

a t m

  • s

t

  • n

e t u r n

  • f

t h e

  • t

h e r t

  • p

r

  • g

r e s s

Peterson's Solution

do { fmag[0] = TRUE; turn = 1; while (fmag[1] && turn==1) ; // critical section fmag[0] = FALSE; // remainder section } while (TRUE) do { fmag[1] = TRUE; turn = 0; while (fmag[0] && turn==0) ; // critical section fmag[1] = FALSE; // remainder section } while (TRUE)

Process 0 Process 1

slide-11
SLIDE 11

1 1

Peterson's Solution – Notes

 O

n l y s u p p

  • r

t s t w

  • p

r

  • c

e s s e s

 g

e n e r a l i z i n g f

  • r

m

  • r

e t h a n t w

  • p

r

  • c

e s s e s h a s b e e n a c h i e v e d

 A

s s u m e s t h a t t h e L O A D a n d S T O R E i n s t r u c t i

  • n

s a r e a t

  • m

i c

 A

s s u m e s t h a t m e m

  • r

y a c c e s s e s a r e n

  • t

r e

  • r

d e r e d

 M

a y b e l e s s e ffj c i e n t t h a n a h a r d w a r e a p p r

  • a

c h

 p

a r t i c u l a r l y f

  • r

> 2 p r

  • c

e s s e s

slide-12
SLIDE 12

1 2

Lock-Based Solutions

 G

e n e r a l s

  • l

u t i

  • n

t

  • t

h e c r i t i c a l s e c t i

  • n

p r

  • b

l e m

 c

r i t i c a l s e c t i

  • n

s a r e p r

  • t

e c t e d b y l

  • c

k s

 p

r

  • c

e s s m u s t a c q u i r e l

  • c

k b e f

  • r

e e n t r y

 p

r

  • c

e s s r e l e a s e s l

  • c

k

  • n

e x i t

do { acquire lock; critical section release lock; remainder section } while(TRUE);

slide-13
SLIDE 13

1 3

Hardware Support for Lock-Based Solutions – Uniprocessors

 F

  • r

u n i p r

  • c

e s s

  • r

s y s t e m s

 c

  • n

c u r r e n t p r

  • c

e s s e s c a n n

  • t

b e

  • v

e r l a p p e d ,

  • n

l y i n t e r l e a v e d

 p

r

  • c

e s s r u n s u n t i l i t i n v

  • k

e s s y s t e m c a l l ,

  • r

i s i n t e r r u p t e d

 D

i s a b l e i n t e r r u p t s !

 a

c t i v e p r

  • c

e s s w i l l r u n w i t h

  • u

t p r e e m p t i

  • n

do { disable interrupts; critical section enable interrupts; remainder section } while(TRUE);

slide-14
SLIDE 14

1 4

Hardware Support for Lock-Based Solutions – Multiprocessors

 I

n m u l t i p r

  • c

e s s

  • r

s

 s

e v e r a l p r

  • c

e s s e s s h a r e m e m

  • r

y

 p

r

  • c

e s s

  • r

s b e h a v e i n d e p e n d e n t l y i n a p e e r m a n n e r

 D

i s a b l i n g i n t e r r u p t b a s e d s

  • l

u t i

  • n

w i l l n

  • t

w

  • r

k

 t

  • i

n e ffj c i e n t

 O

S u s i n g t h i s n

  • t

b r

  • a

d l y s c a l a b l e

 P

r

  • v

i d e h a r d w a r e s u p p

  • r

t i n t h e f

  • r

m

  • f

a t

  • m

i c i n s t r u c t i

  • n

s

 a

t

  • m

i c t e s t

  • a

n d

  • s

e t i n s t r u c t i

  • n

 a

t

  • m

i c s w a p i n s t r u c t i

  • n

 a

t

  • m

i c c

  • m

p a r e

  • a

n d

  • s

w a p i n s t r u c t i

  • n

 A

t

  • m

i c e x e c u t i

  • n
  • f

a s e t

  • f

i n s t r u c t i

  • n

s m e a n s t h a t i n s t r u c t i

  • n

s a r e t r e a t e d a s a s i n g l e s t e p t h a t c a n n

  • t

b e i n t e r r u p t e d .

slide-15
SLIDE 15

1 5

TestAndSet Instruction

 P

s e u d

  • c
  • d

e d e fj n i t i

  • n
  • f

T e s t A n d S e t b

  • l

e a n T e s t A n d S e t ( b

  • l

e a n * t a r g e t ) { b

  • l

e a n r v = * t a r g e t ; * t a r g e t = T R U E ; r e t u r n r v : }

slide-16
SLIDE 16

1 6

Mutual Exclusion using TestAndSet

int mutex; init_lock (&mutex); do { lock (&mutex); critical section unlock (&mutex); remainder section } while(TRUE); void init_lock (int *mutex) { *mutex = 0; } void lock (int *mutex) { while(TestAndSet(mutex)) ; } void unlock (int *mutex) { *mutex = 0; }

slide-17
SLIDE 17

1 7

Swap Instruction

 P

s u e d

  • c
  • d

e d e fj n i t i

  • n
  • f

s w a p i n s t r u c t i

  • n

v

  • i

d S w a p ( b

  • l

e a n * a , b

  • l

e a n * b ) { b

  • l

e a n t e m p = * a ; * a = * b ; * b = t e m p : }

slide-18
SLIDE 18

1 8

Mutual Exclusion using Swap

int mutex; init_lock (&mutex); do { lock (&mutex); critical section unlock (&mutex); remainder section } while(TRUE); void init_lock (int *mutex) { *mutex = 0; } void lock (int *mutex) { int key = TRUE; do { Swap(&key, mutex); }while(key == TRUE); } void unlock (int *mutex) { *mutex = 0; }

Fairness not guaranteed by any implementation !

slide-19
SLIDE 19

1 9

Bounded Waiting Solution

do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != i) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE); do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != I) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE);

Process i = 0 Process i = 1 Cycle = 0 Process i = 0 lock=FALSE, key=FALSE, waiting[0]=0, waiting[1]=0

slide-20
SLIDE 20

2

Bounded Waiting Solution

do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != i) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE); do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != I) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE);

Process i = 0 Process i = 1 Cycle = 1 Process i = 0 lock=FALSE, key=FALSE, waiting[0]=1, waiting[1]=1

slide-21
SLIDE 21

2 1

Bounded Waiting Solution

do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != i) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE); do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != I) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE);

Process i = 0 Process i = 1 Cycle = 2 Process i = 0 lock=FALSE, key=TRUE, waiting[0]=1, waiting[1]=1

slide-22
SLIDE 22

2 2

Bounded Waiting Solution

do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != i) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE); do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != I) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE);

Process i = 0 Process i = 1 Cycle = 3 Process i = 0 lock=FALSE, key=TRUE, waiting[0]=1, waiting[1]=1

slide-23
SLIDE 23

2 3

Bounded Waiting Solution

do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != i) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE); do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != I) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE);

Process i = 0 Process i = 1 Cycle = 4 Process i = 0 lock=TRUE, key=FALSE, waiting[0]=1, waiting[1]=1

Process 0 wins the race

slide-24
SLIDE 24

2 4

Bounded Waiting Solution

do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != i) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE); do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != I) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE);

Process i = 0 Process i = 1 Cycle = 5 Process i = 0 lock=TRUE, key=TRUE, waiting[0]=0, waiting[1]=1

slide-25
SLIDE 25

2 5

Bounded Waiting Solution

do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != i) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE); do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != I) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE);

Process i = 0 Process i = 1 Cycle = 6 Process i = 0 lock=TRUE, key=TRUE, waiting[0]=0, waiting[1]=1

slide-26
SLIDE 26

2 6

Bounded Waiting Solution

do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != i) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE); do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != I) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE);

Process i = 0 Process i = 1 Cycle = 7 Process i = 0 lock=TRUE, key=TRUE, waiting[0]=0, waiting[1]=1

j = 1

slide-27
SLIDE 27

2 7

Bounded Waiting Solution

do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != i) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE); do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != I) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE);

Process i = 0 Process i = 1 Cycle = 8 Process i = 0 lock=TRUE, key=TRUE, waiting[0]=0, waiting[1]=1

slide-28
SLIDE 28

2 8

Bounded Waiting Solution

do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != i) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE); do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != I) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE);

Process i = 0 Process i = 1 Cycle = 9 Process i = 0 lock=TRUE, key=TRUE, waiting[0]=0, waiting[1]=1

slide-29
SLIDE 29

2 9

Bounded Waiting Solution

do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != i) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE); do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != I) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE);

Process i = 0 Process i = 1 Cycle = 10 Process i = 0 lock=TRUE, key=TRUE, waiting[0]=0, waiting[1]=0

slide-30
SLIDE 30

3

Bounded Waiting Solution

do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != i) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE); do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != I) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE);

Process i = 0 Process i = 1 Cycle = 11 Process i = 0 lock=TRUE, key=TRUE, waiting[0]=0, waiting[1]=0

slide-31
SLIDE 31

3 1

Bounded Waiting Solution

do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != i) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE); do{ waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // Critical Section j = (i + 1) % n; while ((j != I) && !waiting[j]) j = (j+1) % n; if (j == i ) lock = FALSE; else waiting[j] = FALSE; // Remainder Section } while (TRUE);

Process i = 0 Process i = 1 Cycle = 12 Process i = 0 lock=TRUE, key=TRUE, waiting[0]=0, waiting[1]=0

slide-32
SLIDE 32

3 2

Semaphores

 A

n

  • t

h e r s

  • l

u t i

  • n

t

  • t

h e c r i t i c a l s e c t i

  • n

p r

  • b

l e m

 h

i g h e r

  • l

e v e l t h a n u s i n g d i r e c t I S A i n s t r u c t i

  • n

s

 s

i m i l a r t

  • l
  • c

k s , b u t s e m a n t i c s a r e d i fg e r e n t

 S

e m a p h

  • r

e ( s i m p l e d e fj n i t i

  • n

)

 i

s a n i n t e g e r v a r i a b l e

 o

n l y a c c e s s e d v i a i n i t ( ) , w a i t ( ) , a n d s i g n a l ( )

  • p

e r a t i

  • n

s

 a

l l s e m a p h

  • r

e

  • p

e r a t i

  • n

s a r e a t

  • m

i c

 B

i n a r y s e m a p h

  • r

e s

 v

a l u e

  • f

s e m a p h

  • r

e c a n e i t h e r b e

  • r

1

 u

s e d f

  • r

p r

  • v

i d i n g m u t u a l e x c l u s i

  • n

 C

  • u

n t i n g s e m a p h

  • r

e

 c

a n h a v e a n y i n t e g e r v a l u e

 a

c c e s s c

  • n

t r

  • l

t

  • s
  • m

e fj n i t e r e s

  • u

r c e

slide-33
SLIDE 33

3 3

Mutual Exclusion Using Semaphores

int S; sem_init (&S); do { wait (&S); // critical section signal (&S); // remainder section } while(TRUE); void sem_init (int *S) { *S = 0; } void wait (int *S) { while (*S <= 0) ; *S–– ; } void signal (int *S) { *S++; }

slide-34
SLIDE 34

3 4

Problem With All Earlier Solutions ?

 B

u s y w a i t i n g

  • r

s p i n l

  • c

k s

 p

r

  • c

e s s m a y l

  • p

c

  • n

t i n u

  • u

s l y i n t h e e n t r y c

  • d

e t

  • t

h e c r i t i c a l s e c t i

  • n

 D

i s a d v a n t a g e

  • f

b u s y w a i t i n g

 w

a i t i n g p r

  • c

e s s h

  • l

d s

  • n

t

  • t

h e C P U d u r i n g i t s t i m e

  • s

l i c e

 d

  • e

s n

  • u

s e f u l w

  • r

k

 d

  • e

s n

  • t

l e t a n y

  • t

h e r p r

  • c

e s s d

  • u

s e f u l w

  • r

k

 M

u l t i p r

  • c

e s s

  • r

s s t i l l d

  • u

s e b u s y

  • w

a i t i n g s

  • l

u t i

  • n

s .

slide-35
SLIDE 35

3 5

Semaphore with no Busy waiting

 A

s s

  • c

i a t e w a i t i n g q u e u e w i t h e a c h s e m a p h

  • r

e

 S

e m a p h

  • r

e ( n

  • b

u s y w a i t i n g d e fj n i t i

  • n

)

 i

n t e g e r v a l u e

 w

a i t i n g q u e u e

t y p e d e f s t r u c t { i n t v a l u e ; s t r u c t p r

  • c

e s s * l i s t ; } s e m a p h

  • r

e ;

slide-36
SLIDE 36

3 6

Operations on Semaphore with no Busy waiting (2)

  • Wait ( ) operation

wait (semaphore *S) { S–>value–– ; if (S–>value < 0) { // add process to // S –>list block ( ); } } block ( ) suspends the process that invokes it.

  • Signal ( ) operation

signal (semaphore *S) { S–>value++ ; if (S–>value >= 0) { // remove process P // from S –>list wakeup (P); } } wakeup ( ) resumes execution of the blocked process P.

slide-37
SLIDE 37

3 7

Atomic Implementation of Semaphore Operations

 G

u a r a n t e e t h a t w a i t a n d s i g n a l

  • p

e r a t i

  • n

s a r e a t

  • m

i c

 c

r i t i c a l s e c t i

  • n

p r

  • b

l e m a g a i n ?

 h

  • w

t

  • e

n s u r e a t

  • m

i c i t y

  • f

w a i t a n d s i g n a l ?

 E

n s u r i n g a t

  • m

i c i t y

  • f

w a i t a n d s i g n a l

 i

m p l e m e n t s e m a p h

  • r

e

  • p

e r a t i

  • n

s u s i n g h a r d w a r e s

  • l

u t i

  • n

s

 u

n i p r

  • c

e s s

  • r

s – e n a b l e / d i s a b l e i n t e r r u p t s

 m

u l t i p r

  • c

e s s

  • r

s – u s i n g s p i n l

  • c

k s a r

  • u

n d w a i t a n d s i g n a l

 D

i d w e r e a l l y s

  • l

v e t h e b u s y

  • w

a i t i n g p r

  • b

l e m

 N

O !

 b

u t w e s h i f t e d i t s l

  • c

a t i

  • n

,

  • n

l y b u s y

  • w

a i t a r

  • u

n d w a i t a n d s i g n a l

 w

a i t a n d s i g n a l a r e s m a l l r

  • u

t i n e s

slide-38
SLIDE 38

3 8

Deadlock

 D

e a d l

  • c

k

 t

w

  • r

m

  • r

e p r

  • c

e s s e s a r e w a i t i n g i n d e fj n i t e l y f

  • r

a n e v e n t t h a t c a n b e c a u s e d b y

  • n

l y

  • n

e

  • f

t h e w a i t i n g p r

  • c

e s s e s

 E

x a m p l e : S a n d Q b e t w

  • s

e m a p h

  • r

e s i n i t i a l i z e d t

  • 1

P P 1 1

w a i t ( S ) ; w a i t ( Q ) ; w a i t ( Q ) ; w a i t ( S ) ; . . . . . . s i g n a l ( S ) ; s i g n a l ( Q ) ; s i g n a l ( Q ) ; s i g n a l ( S ) ;

slide-39
SLIDE 39

3 9

Starvation and Priority Inversion

 I

n d e fj n i t e b l

  • c

k i n g

  • r

s t a r v a t i

  • n

 p

r

  • c

e s s i s n

  • t

d e a d l

  • c

k e d

 b

u t i s n e v e r r e m

  • v

e d f r

  • m

t h e s e m a p h

  • r

e q u e u e

 P

r i

  • r

i t y i n v e r s i

  • n

 l

  • w

e r

  • p

r i

  • r

i t y p r

  • c

e s s h

  • l

d s a l

  • c

k n e e d e d b y h i g h e r

  • p

r i

  • r

i t y p r

  • c

e s s !

 a

s s u m e t h r e e p r

  • c

e s s e s L , M , a n d H

 p

r i

  • r

i t i e s i n t h e

  • r

d e r L < M < H

 L

h

  • l

d s s h a r e d r e s

  • u

r c e R , n e e d e d b y H

 M

p r e e m p t s L , H n e e d s t

  • w

a i t f

  • r

b

  • t

h L a n d M ! !

 s

  • l

u t i

  • n

s

 o

n l y s u p p

  • r

t a t m

  • s

t t w

  • p

r i

  • r

i t i e s

 p

r i

  • r

i t y i n h e r i t a n c e p r

  • t
  • c
  • l

– l

  • w

e r p r i

  • r

i t y p r

  • c

e s s a c c e s s i n g s h a r e d r e s

  • u

r c e i n h e r i t s h i g h e r p r i

  • r

i t y

slide-40
SLIDE 40

4

Problem Solving Using Semaphores

 B

  • u

n d e d

  • b

u fg e r p r

  • b

l e m

 R

e a d e r s

  • Wr

i t e r s p r

  • b

l e m

slide-41
SLIDE 41

4 1

Bounded-Buffer Problem

 P

r

  • b

l e m s y n

  • p

s i s

 a

s e t

  • f

r e s

  • u

r c e b u fg e r s s h a r e d b y p r

  • d

u c e r a n d c

  • n

s u m e r t h r e a d s

 b

u fg e r s a r e s h a r e d b e t w e e n p r

  • d

u c e r a n d c

  • n

s u m e r

 p

r

  • d

u c e r i n s e r t s r e s

  • u

r c e s i n t

  • t

h e b u fg e r s

 o

u t p u t , d i s k b l

  • c

k s , m e m

  • r

y p a g e s , p r

  • c

e s s e s , e t c .

 c

  • n

s u m e r r e m

  • v

e s r e s

  • u

r c e s f r

  • m

t h e b u fg e r s e t

 w

h a t e v e r i s g e n e r a t e d b y t h e p r

  • d

u c e r

 p

r

  • d

u c e r a n d c

  • n

s u m e r e x e c u t e a s y n c h r

  • n
  • u

s l y

 n

  • s

e r i a l i z a t i

  • n
  • f
  • n

e b e h i n d t h e

  • t

h e r

 C

P U s c h e d u l e r d e t e r m i n e s w h a t r u n w h e n

 E

n s u r e d a t a ( b u fg e r ) c

  • n

s i s t e n c y

 c

  • n

s u m e r s h

  • u

l d s e e e a c h p r

  • d

u c e d i t e m a t l e a s t

  • n

c e

 c

  • n

s u m e r s h

  • u

l d s e e e a c h p r

  • d

u c e d i t e m a t m

  • s

t

  • n

c e

slide-42
SLIDE 42

4 2

Bounded Buffer Problem (2)

 S

  • l

u t i

  • n

e m p l

  • y

s t h r e e s e m a p h

  • r

e s

 m

u t e x

 a

l l

  • w

e x c l u s i v e a c c e s s t

  • t

h e b u fg e r p

  • l

s

 m

u t e x s e m a p h

  • r

e , i n i t i a l i z e d t

  • 1

 e

m p t y

 c

  • u

n t n u m b e r

  • f

e m p t y b u fg e r s

 c

  • u

n t i n g s e m a p h

  • r

e , i n i t i a l i z e d t

  • n

( t h e t

  • t

a l n u m b e r

  • f

a v a i l a b l e b u fg e r s )

 f

u l l

 c

  • u

n t n u m b e r

  • f

f u l l b u fg e r s

 c

  • u

n t i n g s e m a p h

  • r

e , i n i t i a l i z e d t

slide-43
SLIDE 43

4 3

Bounded Buffer Problem (3)

S e m a p h

  • r

e b

  • l

m u t e x ; S e m a p h

  • r

e i n t f u l l , e m p t y ; do { Produce new resource wait (empty); wait (mutex); Add resource to next bufger signal (mutex); signal (full); } while (TRUE);

Producer

do { wait (full); wait (mutex); Remove resource from bufger signal (mutex); signal (empty); Consume resource } while (TRUE);

Consumer

slide-44
SLIDE 44

4 4

Readers – Writers Problem

 P

r

  • b

l e m s y n

  • p

s i s

 a

n

  • b

j e c t s h a r e d a m

  • n

g s e v e r a l t h r e a d s

 s

  • m

e t h r e a d s

  • n

l y r e a d t h e

  • b

j e c t ( R e a d e r s )

 s

  • m

e t h r e a d s

  • n

l y w r i t e t h e

  • b

j e c t ( Wr i t e r s )

 P

r

  • b

l e m i s t

  • e

n s u r e d a t a c

  • n

s i s t e n c y

 m

u l t i p l e r e a d e r s c a n a c c e s s t h e s h a r e d r e s

  • u

r c e s i m u l t a n e

  • u

s l y

 o

n l y

  • n

e w r i t e r s h

  • u

l d u p d a t e t h e

  • b

j e c t a t a t i m e

 r

e a d e r s s h

  • u

l d n

  • t

a c c e s s t h e

  • b

j e c t a s i t i s b e i n g u p d a t e d

 a

d d i t i

  • n

a l c

  • n

s t r a i n t

 r

e a d e r s h a v e p r i

  • r

i t y

  • v

e r w r i t e r s

 e

a s i e r t

  • i

m p l e m e n t

slide-45
SLIDE 45

4 5

Readers – Writers Problem (2)

 We

u s e t w

  • s

e m a p h

  • r

e s

 m

u t e x

 e

n s u r e m u t u a l e x c l u s i

  • n

f

  • r

t h e r e a d c

  • u

n t v a r i a b l e

 m

u t e x s e m a p h

  • r

e , i n i t i a l i z e d t

  • 1

 w

r t

 e

n s u r e m u t u a l e x c l u s i

  • n

f

  • r

w r i t e r s

 e

n s u r e m u t u a l e x c l u s i

  • n

b e t w e e n r e a d e r s a n d w r i t e r

 m

u t e x s e m a p h

  • r

e , i n i t i a l i z e d t

  • 1
slide-46
SLIDE 46

4 6

Readers – Writers Problem (3)

semaphore bool mutex, wrt; int readcount; do { wait (wrt); . . . . write object resource . . . . signal (wrt); } while (TRUE);

Writer

do { wait (mutex); readcount++; if (readcount == 1) wait (wrt); signal (mutex); read from object resource wait (mutex); readcount––; if (readcount == 0) signal (wrt); signal (mutex); } while (TRUE);

Reader

slide-47
SLIDE 47

4 7

Semaphore – Summary

 S

e m a p h

  • r

e s c a n b e u s e d t

  • s
  • l

v e a n y

  • f

t h e t r a d i t i

  • n

a l s y n c h r

  • n

i z a t i

  • n

p r

  • b

l e m s

 D

r a w b a c k s

  • f

s e m a p h

  • r

e s

 s

e m a p h

  • r

e s a r e e s s e n t i a l l y s h a r e d g l

  • b

a l v a r i a b l e s

 c

a n b e a c c e s s e d f r

  • m

a n y w h e r e i n a p r

  • g

r a m

 s

e m a p h

  • r

e s a r e v e r y l

  • w
  • l

e v e l c

  • n

s t r u c t s

 n

  • c
  • n

n e c t i

  • n

b e t w e e n s e m a p h

  • r

e a n d d a t a c

  • n

t r

  • l

l e d b y a s e m a p h

  • r

e

 d

i ffj c u l t t

  • u

s e

 u

s e d f

  • r

b

  • t

h c r i t i c a l s e c t i

  • n

( m u t u a l e x c l u s i

  • n

) a n d c

  • r

d i n a t i

  • n

( s c h e d u l i n g )

 p

r

  • v

i d e s n

  • c
  • n

t r

  • l
  • f

p r

  • p

e r u s a g e

 u

s e r m a y m i s s a w a i t

  • r

s i g n a l ,

  • r

r e p l a c e

  • r

d e r

  • f

w a i t , a n d s i g n a l

 T

h e s

  • l

u t i

  • n

i s t

  • u

s e p r

  • g

r a m m i n g

  • l

a n g u a g e l e v e l s u p p

  • r

t .

slide-48
SLIDE 48

4 8

Monitors

 M

  • n

i t

  • r

i s a p r

  • g

r a m m i n g l a n g u a g e c

  • n

s t r u c t t h a t c

  • n

t r

  • l

s a c c e s s t

  • s

h a r e d d a t a

 s

y n c h r

  • n

i z a t i

  • n

c

  • d

e a d d e d b y t h e c

  • m

p i l e r

 s

y n c h r

  • n

i z a t i

  • n

e n f

  • r

c e d b y t h e r u n t i m e

 M

  • n

i t

  • r

i s a n a b s t r a c t d a t a t y p e ( A D T ) t h a t e n c a p s u l a t e s

 s

h a r e d d a t a s t r u c t u r e s

 p

r

  • c

e d u r e s t h a t

  • p

e r a t e

  • n

t h e s h a r e d d a t a s t r u c t u r e s

 s

y n c h r

  • n

i z a t i

  • n

b e t w e e n t h e c

  • n

c u r r e n t p r

  • c

e d u r e i n v

  • c

a t i

  • n

s

 P

r

  • t

e c t s t h e s h a r e d d a t a s t r u c t u r e s i n s i d e t h e m

  • n

i t

  • r

f r

  • m
  • u

t s i d e a c c e s s .

 G

u a r a n t e e s t h a t m

  • n

i t

  • r

p r

  • c

e d u r e s (

  • r
  • p

e r a t i

  • n

s ) c a n

  • n

l y l e g i t i m a t e l y u p d a t e t h e s h a r e d d a t a .

slide-49
SLIDE 49

4 9

Monitor Semantics for Mutual Exclusion

 O

n l y

  • n

e t h r e a d c a n e x e c u t e a n y m

  • n

i t

  • r

p r

  • c

e d u r e a t a t i m e .

 O

t h e r t h r e a d s i n v

  • k

i n g a m

  • n

i t

  • r

p r

  • c

e d u r e w h e n

  • n

e i s a l r e a d y e x e c u t i n g s

  • m

e m

  • n

i t

  • r

p r

  • c

e d u r e m u s t w a i t .

 Wh

e n t h e a c t i v e t h r e a d e x i t s t h e m

  • n

i t

  • r

p r

  • c

e d u r e ,

  • n

e

  • t

h e r w a i t i n g t h r e a d c a n e n t e r .

Entry Set Owner

acquire enter release and exit

waiting thread active thread

slide-50
SLIDE 50

5

Monitor for Mutual Exclusion

Monitor Account { double balance; double withdraw (amount) { balance = balance – amount ; return balance; } } withdraw (amount) { balance = balance – amount; withdraw (amount) withdraw (amount) return balance; } ( release lock and exit ) balance = balance – amount; return balance; } ( release lock and exit ) balance = balance – amount; return balance; } ( release lock and exit )

1 2 3 1 3 2

slide-51
SLIDE 51

5 1

Monitor for Coordination

 Wh

a t i f a t h r e a d n e e d s t

  • w

a i t i n s i d e a m

  • n

i t

  • r

 w

a i t i n g f

  • r

s

  • m

e r e s

  • u

r c e , l i k e i n p r

  • d

u c e r

  • c
  • n

s u m e r r e l a t i

  • n

s h i p

 m

  • n

i t

  • r

w i t h c

  • n

d i t i

  • n

v a r i a b l e s .

 C

  • n

d i t i

  • n

v a r i a b l e s p r

  • v

i d e m e c h a n i s m t

  • w

a i t f

  • r

e v e n t s

 r

e s

  • u

r c e a v a i l a b l e , n

  • m
  • r

e w r i t e r s , e t c . Entry Set Owner

acquire enter release and exit

waiting thread active thread

release acquire

suspended thread

Wait Set

slide-52
SLIDE 52

5 2

Condition Variable Semantics

 C

  • n

d i t i

  • n

v a r i a b l e s s u p p

  • r

t t w

  • p

e r a t i

  • n

s

 w

a i t – r e l e a s e m

  • n

i t

  • r

l

  • c

k , a n d s u s p e n d t h r e a d

 c

  • n

d i t i

  • n

v a r i a b l e s h a v e w a i t q u e u e s

 s

i g n a l – w a k e u p

  • n

e w a i t i n g t h r e a d

 i

f n

  • p

r

  • c

e s s i s s u s p e n d e d , t h e n s i g n a l h a s n

  • a

fg e c t

 S

i g n a l s e m a n t i c s

 H

  • a

r e m

  • n

i t

  • r

s (

  • r

i g i n a l )

 s

i g n a l i m m e d i a t e l y s w i t c h e s f r

  • m

t h e c a l l e r t

  • t

h e w a i t i n g t h r e a d

 w

a i t e r ' s c

  • n

d i t i

  • n

i s g u a r a n t e e d t

  • h
  • l

d w h e n i t c

  • n

t i n u e s e x e c u t i

  • n

 M

e s a m

  • n

i t

  • r

s

 w

a i t e r p l a c e d

  • n

r e a d y q u e u e , s i g n a l e r c

  • n

t i n u e s

 w

a i t e r ' s c

  • n

d i t i

  • n

m a y n

  • l
  • n

g e r b e t r u e w h e n i t r u n s

 C

  • m

p r

  • m

i s e

  • s

i g n a l e r i m m e d i a t e l y l e a v e s m

  • n

i t

  • r

, w a i t e r r e s u m e s

  • p

e r a t i

  • n
slide-53
SLIDE 53

5 3

Bounded Buffer Using Monitors

Monitor bounded_bufger { Resource bufger[N]; // condition variables Condition empty, full; void producer (Resource R) { while (bufger full) empty.wait( ); // add R to bufger array full.signal( ); } Resource consumer ( ) { while (bufger empty) full.wait( ); // get Resource from bufger empty.signal( ); return R; } } // end monitor

slide-54
SLIDE 54

5 4

Condition Variables

 C

  • n

d i t i

  • n

v a r i a b l e s a r e n

  • t

b

  • l

e a n s

 '

' i f ( c

  • n

d i t i

  • n

_ v a r i a b l e ) t h e n … ' ' i s n

  • t

l

  • g

i c a l l y c

  • r

r e c t

 w

a i t ( ) a n d s i g n a l ( ) a r e t h e

  • n

l y

  • p

e r a t i

  • n

s t h a t a r e c

  • r

r e c t

 C

  • n

d i t i

  • n

v a r i a b l e ! = S e m a p h

  • r

e s

 t

h e y h a v e v e r y d i fg e r e n t s e m a n t i c s

 e

a c h c a n b e u s e d t

  • i

m p l e m e n t t h e

  • t

h e r

 Wa

i t ( ) s e m a n t i c s

 w

a i t b l

  • c

k s t h e c a l l i n g t h r e a d , a n d g i v e s u p t h e l

  • c

k

 S

e m a p h

  • r

e : : w a i t j u s t b l

  • c

k s t h e c a l l i n g t h r e a d

 o

n l y m

  • n

i t

  • r
  • p

e r a t i

  • n

s c a n c a l l w a i t ( ) a n d s i g n a l ( )

 S

i g n a l ( ) s e m a n t i c s

 i

f t h e r e a r e n

  • w

a i t i n g t h r e a d s , t h e n t h e s i g n a l i s l

  • s

t

 S

e m a p h

  • r

e : : s i g n a l j u s t i n c r e a s e s g l

  • b

a l v a r i a b l e c

  • u

n t , a l l

  • w

i n g e n t r y t

  • f

u t u r e t h r e a d

slide-55
SLIDE 55

5 5

Monitor with Condition Variables

slide-56
SLIDE 56

5 6

Dining Philosophers Problem

 R

e p r e s e n t s n e e d t

  • a

l l

  • c

a t e s e v e r a l r e s

  • u

r c e s a m

  • n

g s e v e r a l p r

  • c

e s s e s i n a d e a d l

  • c

k

  • f

r e e a n d s t a r v a t i

  • n
  • f

r e e m a n n e r .

 P

r

  • b

l e m s y n

  • p

s i s

 5

p h i l

  • s
  • p

h e r s , c i r c u l a r t a b l e

 2

s t a t e s , h u n g r y a n d t h i n k i n g

 5

s i n g l e c h

  • p

s t i c k s

 h

u n g r y , p i c k u p t w

  • c

h

  • p

s t i c k s

 r

i g h t a n d l e f t

 m

a y

  • n

l y p i c k u p

  • n

e s t i c k a t a t i m e

 e

a t w h e n h a v e b

  • t

h s t i c k s

 P

r

  • b

l e m d e fj n i t i

  • n

 a

l l

  • w

e a c h p h i l

  • s
  • p

h e r t

  • e

a t a n d t h i n k w i t h

  • u

t d e a d l

  • c

k s a n d s t a r v a t i

  • n
slide-57
SLIDE 57

5 7

Dining Philosophers Problem (2)

 R

e s t r i c t i

  • n
  • n

t h e p r

  • b

l e m

 o

n l y p i c k c h

  • p

s t i c k s i f b

  • t

h a r e a v a i l a b l e

 P

r

  • b

l e m s

  • l

u t i

  • n

 u

s e t h r e e s t a t e s , t h i n k i n g , h u n g r y , e a t i n g

 c

  • n

d i t i

  • n

v a r i a b l e f

  • r

e a c h p h i l

  • s
  • p

h e r

 d

e l a y i f h u n g r y b u t w a i t i n g f

  • r

c h

  • p

s t i c k s

 i

n v

  • k

e m

  • n

i t

  • r
  • p

e r a t i

  • n

s i n t h e f

  • l

l

  • w

i n g s e q u e n c e D i n i n g P h i l

  • s
  • p

h e r s . p i c k u p ( i ) ; . . . . . . / / e a t . . . . . . . D i n i n g P h i l

  • s
  • p

h e r s . p u t d

  • w

n ( i ) ;

slide-58
SLIDE 58

5 8

Solution to Dining Philosophers

Monitor DP { enum { THINKING; HUNGRY, EATING) state [5] ; condition self [5]; void pickup (int i) { state[i] = HUNGRY; test(i); if (state[i] != EATING) self [i].wait; } void putdown (int i) { state[i] = THINKING; // test neighbors test((i + 4) % 5); test((i + 1) % 5); } void test (int i) { if ( (state[(i + 4) % 5] != EATING) && (state[i] == HUNGRY) && (state[(i + 1) % 5] != EATING) ) { state[i] = EATING ; self[i].signal () ; } } initialization_code() { for (int i = 0; i < 5; i++) state[i] = THINKING; } } // end monitor

slide-59
SLIDE 59

5 9

OS Implementation Issues

 H

  • w

t

  • w

a i t

  • n

a l

  • c

k h e l d b y a n

  • t

h e r t h r e a d ?

 s

l e e p i n g

  • r

s p i n

  • w

a i t i n g

 O

v e r h e a d

  • f

s p i n

  • w

a i t i n g

 a

s p i n n i n g t h r e a d

  • c

c u p i e s t h e C P U ; s l

  • w

s p r

  • g

r e s s

  • f

a l l

  • t

h e r t h r e a d s , i n c l u d i n g t h e

  • n

e h

  • l

d i n g t h e l

  • c

k

 O

v e r h e a d

  • f

s l e e p i n g

 i

s s u e a w a i t a n d s l e e p ; s e n d s i g n a l t

  • s

l e e p i n g t h r e a d ; w a k e u p t h r e a d ; m u l t i p l e c

  • n

t e x t s w i t c h e s

 S

p i n

  • w

a i t i n g i s u s e d

  • n

 m

u l t i p r

  • c

e s s

  • r

s y s t e m s

 w

h e n t h e t h r e a d h

  • l

d i n g t h e l

  • c

k i s t h e

  • n

e r u n n i n g

 l

  • c

k e d d a t a i s

  • n

l y a c c e s s e d b y s h

  • r

t c

  • d

e s e g m e n t s

slide-60
SLIDE 60

6

OS Implementation Issues (2)

 R

e a d e r

  • w

r i t e r l

  • c

k s

 u

s e d w h e n s h a r e d d a t a i s r e a d m

  • r

e

  • f

t e n

 m

  • r

e e x p e n s i v e t

  • s

e t u p t h a n m u t u a l e x c l u s i

  • n

l

  • c

k s

 N

  • n
  • p

r e e m p t i v e k e r n e l

 p

r

  • c

e s s i n k e r n e l m

  • d

e c a n n

  • t

b e p r e e m p t e d

 u

s e d i n L i n u x

  • n

s i n g l e p r

  • c

e s s

  • r

m a c h i n e s

 u

s e s preempt_disable() a n d preempt_enable()s y s t e m c a l l s

 s

p i n

  • l
  • c

k s , s e m a p h

  • r

e s u s e d

  • n

m u l t i p r

  • c

e s s

  • r

m a c h i n e s

slide-61
SLIDE 61

6 1

Atomic Transactions

 T

r a n s a c t i

  • n

– c

  • l

l e c t i

  • n
  • f

i n s t r u c t i

  • n

s t h a t p e r f

  • r

m a s i n g l e l

  • g

i c a l f u n c t i

  • n

 A

t

  • m

i c i t y – e x e c u t e t r a n s a c t i

  • n

a s

  • n

e u n i n t e r r u p t i b l e u n i t

 M

u t u a l e x c l u s i

  • n

– e x e c u t e c r i t i c a l s e c t i

  • n

s a t

  • m

i c a l l y

 w

h a t h a p p e n s i f s y s t e m f a i l s d u r i n g a t r a n s a c t i

  • n

?

 h

  • w

t

  • p

r e s e r v e a t

  • m

i c i t y i n t h e p

  • s

s i b i l i t y

  • f

s y s t e m f a i l u r e s ?

 C

  • m

m i t t e d – t r a n s a c t i

  • n

h a s c

  • m

p l e t e d s u c c e s s f u l l y

 A

b

  • r

t e d – t r a n s a c t i

  • n

h a s f a i l e d

 r

  • l

l b a c k t h e t r a n s a c t i

  • n

t

  • p

r e v i

  • u

s c

  • n

s i s t e n t s t a t e , c a l l e d r e c

  • v

e r y

 S

t r a t e g i e s

 l

  • g
  • b

a s e d r e c

  • v

e r y

 c

h e c k p

  • i

n t s

slide-62
SLIDE 62

6 2

Concurrent Atomic Transactions

 S

e r i a l i z a b i l i t y – e x e c u t i

  • n
  • f

m u l t i p l e c

  • n

c u r r e n t t r a n s a c t i

  • n

s i s e q u i v a l e n t t

  • t

h e i r e x e c u t i

  • n

i n a n a r b i t r a r y

  • r

d e r