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

p1 semaphores
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

P1 - Semaphores

Drew Zagieboylo 2 / 9 / 18

slide-2
SLIDE 2

Thread Death

void hello_w() {
 printf(“Hello World!”);
 return;
 }

ra(&mt_exit) … … hello_w … … sp

void mt_exit() {
 //do cleanup
 while (1) {};
 }

slide-3
SLIDE 3

Thread Death

void hello_w() {
 printf(“Hello World!”);
 return;
 }

… … sp

void mt_exit() {
 //do cleanup
 while (1) {};
 }

mt_exit

pc

slide-4
SLIDE 4

done, thread_exit()

Init

Admitted to Run Queue

Ready

dispatch

Running TCB: Registers:

yield, interrupt, descheduled

Waiting

I/O operation join(), wait() I/O or thread completion

Finished

Thread State Transitions

slide-5
SLIDE 5

done, thread_exit()

Init

Admitted to Run Queue

Ready

dispatch

Running TCB: Registers:

yield, interrupt, descheduled

Waiting

I/O operation join(), wait() I/O or thread completion

Finished

Thread State Transitions

* * * * - Have Queues

slide-6
SLIDE 6

Thread Death

void mt_exit() {
 //do cleanup
 put current thread on
 cleanup queue
 while (1) {};
 } void cleanup() {
 while (1) {
 //remove thread to clean
 //from queue
 
 //free its stack
 //free its tcb
 }
 }

Cleanup Thread

Problems?

slide-7
SLIDE 7

Thread Cleanup

  • Cleanup thread needs

to be scheduled

  • Shouldn’t run when

there’s nothing to clean

void cleanup() {
 while (1) {
 //remove thread to clean
 //from queue
 
 //free its stack
 //free its tcb
 }
 }

How?

slide-8
SLIDE 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
slide-9
SLIDE 9
slide-10
SLIDE 10

Semaphores

  • Stateful:
  • count
  • queue of threads
  • Functions:
  • P(sema)


procure -> block this thread until resource can be procured

  • V(sema)


vacate -> release this resource and continue

slide-11
SLIDE 11

done, thread_exit()

Init

Admitted to Run Queue

Ready

dispatch

Running TCB: On semaphore’s Queue Registers: On stack

yield, interrupt, descheduled

Waiting

I/O operation join(), wait() I/O or thread completion

Finished

Thread State Transitions

slide-12
SLIDE 12

done, thread_exit()

Init

Admitted to Run Queue

Ready

dispatch

Running TCB: On semaphore’s Queue Registers: On stack

yield, interrupt, descheduled

Waiting

semaphore_P()* Semaphore_V()*

Finished

Thread State Transitions

slide-13
SLIDE 13

Example

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

slide-14
SLIDE 14

Example

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

NOT ATOMIC!

slide-15
SLIDE 15

Example

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

slide-16
SLIDE 16

Example

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

slide-17
SLIDE 17

Example

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

slide-18
SLIDE 18

Semaphores

  • Stateful:
  • count
  • queue of threads
  • Functions:
  • P(sema)


procure -> block this thread until resource can be procured

  • V(sema)


vacate -> release this resource and continue

requests = sema(0); while (requests->count > 0) { … … }

slide-19
SLIDE 19

Semaphore
 Invariants

  • Count:
  • If c ≥ 0
  • The number of resources available
  • If c ≤ 0
  • The number of threads waiting on the queue
slide-20
SLIDE 20

Thread Cleanup

  • Cleanup thread needs

to be scheduled

  • Shouldn’t run when

there’s nothing to clean

void cleanup() {
 while (1) {
 //wait for thread
 //to be on queue
 
 //remove thread to clean
 //from queue
 
 //free its stack
 //free its tcb
 }
 }