Recitation 4 Question 3: Flying off the handle Parent Child - - PowerPoint PPT Presentation

recitation 4
SMART_READER_LITE
LIVE PREVIEW

Recitation 4 Question 3: Flying off the handle Parent Child - - PowerPoint PPT Presentation

Recitation 4 Question 3: Flying off the handle Parent Child fork() count++; print(count); 1 S I G U S R 1 2 count += 2 print(count); S G I U S R 1 count += 3; print(count); 4 exit(0); c h i l d p r o c e s s


slide-1
SLIDE 1

Recitation 4

slide-2
SLIDE 2

Question 3: Flying off the handle

slide-3
SLIDE 3

Parent Child

fork() count++; print(count); S I G U S R 1 count += 2 print(count); 1 2 S I G U S R 1 count += 3; print(count); 4 c h i l d p r

  • c

e s s t e r m i n a t e d count += 4 print(count); 6 exit(0);

slide-4
SLIDE 4

Question 4: Nice Threads

slide-5
SLIDE 5

Atomic? x++

reg = x; //load() reg = reg + 1; //update x = reg; //store

slide-6
SLIDE 6

Atomic?

x += k

reg = x; //load() reg = reg + k; //inc x = reg; //store

slide-7
SLIDE 7

Concurrency What could possibly go wrong?!

Thread 1 x += 1 Thread 2 x += 2

T1: reg_1 = load(x) T2: reg_2 = load(x) T1: reg_1 = reg_1 + 1 T2: reg_2 = reg_2 + 2 T2: store(x, reg_2) T1: store(x, reg_1)

slide-8
SLIDE 8

T1 T2

p t h r e a d _ c r e a t e ( ) count++ 1 / 2 / 3 count += 2 1 / 2 / 3 pthread_create()

T3

count += 3 4 / 5 / 6 p t h r e a d _ j

  • i

n ( ) pthread_join() count += 4 8 / 9 / 10

slide-9
SLIDE 9

Question 6: The Semaphore that almost could

slide-10
SLIDE 10

Binary Sempahores

void binary_P(s) { if(s->count == 0) { // wait… } s->count = 0; } void binary_V(s) { if(s->count == 0) s->count = 1; //else // do nothing }

slide-11
SLIDE 11

binary_semaphore mutex = 1; binary_semaphore delay = 0; int C = {initvalue}; void counting_ P() { binary_P(mutex); C = C-1; if (C < 0) { binary_V(mutex); binary_P(delay); } else { binary_V(mutex); } } void counting_V() { binary_P(mutex); C = C+1; if (C <= 0) binary_V(delay); binary_V(mutex); }

slide-12
SLIDE 12

void consumer() { counting_P(); } void producer() { counting_V(); counting_V(); } void main() { start_thread(consumer); start_thread(consumer); start_thread(producer); }

slide-13
SLIDE 13

T1: binary_P(mutex) T1: C ← -1 T1: binary_V(mutex) T1: binary_P(delay) T2: binary_P(mutex) T2: C ← -2 T2: binary_V(mutex) T2: binary_P(delay) T3: binary_P(mutex) T3: C ← -1 T3: binary_V(delay) T3: binary_V(mutex) T3: binary_P(mutex) T3: C ← 0 T3: binary_V(delay) T3: binary_V(mutex)

binary_semaphore mutex = 1; binary_semaphore delay = 0; int C = {initvalue}; void counting_ P() { binary_P(mutex); C = C-1; if (C < 0) { binary_V(mutex); binary_P(delay); } else { binary_V(mutex); } } void counting_V() { binary_P(mutex); C = C+1; if (C <= 0) binary_V(delay); binary_V(mutex); }

slide-14
SLIDE 14

binary_semaphore mutex = 1; binary_semaphore delay = 0; int C = {initvalue}; void counting_ P() { binary_P(mutex); C = C-1; if (C < 0) { binary_V(mutex); binary_P(delay); } else { binary_V(mutex); } } void counting_V() { binary_P(mutex); C = C+1; if (C <= 0) binary_V(delay); binary_V(mutex); }

Binary semaphore is used as a counting semaphore

Jean-Luc Picard – Senior Software Developer

slide-15
SLIDE 15

binary_semaphore mutex; int count = {initvalue}; stack<thread> waiting; void counting_P() { mutex.lock(); if (waiting.size() > 0) { t = waiting.pop(); t.start(); } else { count += 1; } mutex.unlock(); } void counting_V() { mutex.lock(); if(count > 0) { count -= 1; mutex.unlock(); } else { waiting.push(self()); mutex.unlock(); self.stop(); } }

One way to do it correctly