p1 semaphores
play

P1 - Semaphores Drew Zagieboylo 2 / 9 / 18 Thread Death void - PowerPoint PPT Presentation

P1 - Semaphores Drew Zagieboylo 2 / 9 / 18 Thread Death void hello_w() { printf(Hello World!); return; ra(&mt_exit) hello_w } sp void mt_exit() { //do cleanup while (1) {}; } Thread Death


  1. P1 - Semaphores Drew Zagieboylo 2 / 9 / 18

  2. Thread Death … … void hello_w() { 
 printf(“Hello World!”); 
 return; 
 ra(&mt_exit) hello_w } … sp … void mt_exit() { 
 //do cleanup 
 while (1) {}; 
 }

  3. Thread Death … mt_exit … void hello_w() { 
 sp printf(“Hello World!”); 
 return; 
 } pc void mt_exit() { 
 //do cleanup 
 while (1) {}; 
 }

  4. Thread State Transitions yield, interrupt, Init Finished descheduled done, Admitted to thread_exit() Run Queue dispatch Ready Running I/O or thread I/O operation completion join(), wait() Waiting TCB: Registers:

  5. Thread State Transitions * * - Have Queues yield, interrupt, Init Finished descheduled done, Admitted to thread_exit() Run Queue dispatch Ready Running * I/O or thread I/O operation completion join(), wait() Waiting * TCB: Registers:

  6. 
 Thread Death Cleanup Thread void cleanup() { 
 while (1) { 
 void mt_exit() { 
 Problems? //remove thread to clean 
 //do cleanup 
 //from queue 
 put current thread on 
 cleanup queue 
 //free its stack 
 while (1) {}; 
 //free its tcb 
 } } 
 }

  7. 
 Thread Cleanup void cleanup() { 
 • Cleanup thread needs while (1) { 
 //remove thread to clean 
 to be scheduled //from queue 
 How ? • Shouldn’t run when //free its stack 
 //free its tcb 
 there’s nothing to clean } 
 }

  8. Semaphores! Finally I should like to thank the members of the program committee who asked for more information on the synchronizing primitives and some justification of my claim to be able to prove logical soundness a priori . In answer to this request the appendix has been added , of which I hope that it gives the desired information and justification. • Edsger W. Dijkstra. 1967. The structure of the “the”-multiprogramming system. In Proceedings of the first ACM symposium on Operating System Principles (SOSP '67), J. Gosden and B. Randell (Eds.). ACM, New York, NY, USA, 10.1-10.6. DOI=http://dx.doi.org/10.1145/800001.811672 • https://dl.acm.org/citation.cfm?id=811672

  9. Semaphores • Functions: • Stateful: • P(sema) 
 • count procure -> block • queue of threads this thread until resource can be procured • V(sema) 
 vacate -> release this resource and continue

  10. Thread State Transitions yield, interrupt, Init Finished descheduled done, Admitted to thread_exit() Run Queue dispatch Ready Running I/O or thread I/O operation completion join(), wait() Waiting TCB: On semaphore’s Queue Registers: On stack

  11. Thread State Transitions yield, interrupt, Init Finished descheduled done, Admitted to thread_exit() Run Queue dispatch Ready Running semaphore_P()* Semaphore_V()* Waiting TCB: On semaphore’s Queue Registers: On stack

  12. Example var x = 0; void inc() { 
 void dec() { 
 int i = 0; 
 int i = 0; 
 (for; i < 1000; i++) 
 (for; i < 1000; i++) 
 x++; 
 x--; 
 } }

  13. Example var x = 0; void inc() { 
 void dec() { 
 int i = 0; 
 int i = 0; 
 (for; i < 1000; i++) 
 (for; i < 1000; i++) 
 x = x + 1; 
 x = x - 1; 
 } } NOT ATOMIC!

  14. 
 Example var x = 0; 
 var lock = sema(1); void inc() { 
 void dec() { 
 int i = 0; 
 int i = 0; 
 (for; i < 1000; i++) 
 (for; i < 1000; i++) 
 x = x + 1; 
 x = x - 1; 
 } }

  15. 
 Example var x = 0; 
 var lock = sema(1); void inc() { 
 int i = 0; 
 void dec() { 
 (for; i < 1000; i++){ 
 int i = 0; 
 P(lock); 
 (for; i < 1000; i++) 
 x = x + 1; 
 x = x - 1; 
 V(lock); 
 } } 
 }

  16. 
 Example var x = 0; 
 var lock = sema(1); void inc() { 
 void dec() { 
 int i = 0; 
 int i = 0; 
 (for; i < 1000; i++){ 
 (for; i < 1000; i++){ 
 P(lock); 
 P(lock); 
 x = x + 1; 
 x = x - 1; 
 V(lock); 
 V(lock); 
 } 
 } 
 } }

  17. Semaphores • Functions: • Stateful: • P(sema) 
 • count procure -> block • queue of threads this thread until resource can be procured requests = sema(0); while (requests->count > 0) { • V(sema) 
 … … vacate -> release } this resource and continue

  18. Semaphore 
 Invariants • Count: • If c ≥ 0 • The number of resources available • If c ≤ 0 • The number of threads waiting on the queue

  19. 
 Thread Cleanup void cleanup() { 
 while (1) { 
 //wait for thread 
 • Cleanup thread needs //to be on queue 
 to be scheduled 
 //remove thread to clean 
 //from queue 
 • Shouldn’t run when there’s nothing to clean //free its stack 
 //free its tcb 
 } 
 }

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