recitation 4
play

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


  1. Recitation 4

  2. Question 3: Flying off the handle

  3. 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 t e r m i n a t e d 6 count += 4 print(count);

  4. Question 4: Nice Threads

  5. Atomic? reg = x; //load() reg = reg + 1; //update x++ x = reg; //store

  6. Atomic? reg = x; //load() x += k reg = reg + k; //inc x = reg; //store

  7. Concurrency What could possibly go wrong?! T1: reg_1 = load(x) Thread 1 x += 1 T2: reg_2 = load(x) T1: reg_1 = reg_1 + 1 Thread 2 T2: reg_2 = reg_2 + 2 x += 2 T2: store(x, reg_2) T1: store(x, reg_1)

  8. T1 p T2 t h r e a d _ c r e a t e ( ) count++ count += 2 1 / 2 / 3 1 / 2 / 3 pthread_create() T3 p t h r e a d _ j o i n ( ) count += 3 4 / 5 / 6 pthread_join() count += 4 8 / 9 / 10

  9. Question 6: The Semaphore that almost could

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

  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); }

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

  13. T1: binary_P(mutex) binary_semaphore mutex = 1; binary_semaphore delay = 0; T1: C ← -1 int C = {initvalue}; T1: binary_V(mutex) T1: binary_P(delay) void counting_ P() { binary_P(mutex); T2: binary_P(mutex) C = C-1; T2: C ← -2 if (C < 0) { T2: binary_V(mutex) binary_V(mutex); binary_P(delay); T2: binary_P(delay) } else { T3: binary_P(mutex) binary_V(mutex); T3: C ← -1 } } T3: binary_V(delay) void counting_V() { T3: binary_V(mutex) binary_P(mutex); T3: binary_P(mutex) C = C+1; if (C <= 0) T3: C ← 0 binary_V(delay); T3: binary_V(delay) binary_V(mutex); T3: binary_V(mutex) }

  14. binary_semaphore mutex = 1; binary_semaphore delay = 0; Binary semaphore is used int C = {initvalue}; as a counting semaphore 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); Jean-Luc Picard – Senior Software Developer C = C+1; if (C <= 0) binary_V(delay); binary_V(mutex); }

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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend