290 likes | 581 Views
Semaphores. The producer-consumer problem using semaphores. Producer/Consumer. Producer down(&empty); down(&mutex); insert_item(item); up(&mutex); up(&full);. N = 6 Semaphore mutex = 1 Semaphore full = 0 Semaphore empty = N. producer1. consumer1. producer2. consumer2. Run queue.
E N D
Semaphores The producer-consumer problem using semaphores
Producer/Consumer Producer down(&empty); down(&mutex); insert_item(item); up(&mutex); up(&full); N = 6 Semaphore mutex = 1 Semaphore full = 0 Semaphore empty = N producer1 consumer1 producer2 consumer2 Run queue Mutex queue Full queue Empty queue
Producer/Consumer Producer down(&empty); down(&mutex); insert_item(item); up(&mutex); up(&full); N = 6 Semaphore mutex = 1 Semaphore full = 0 Semaphore empty = 5 down (&empty) producer1 consumer1 producer2 consumer2 Run queue Mutex queue Full queue Empty queue
Producer/Consumer Producer down(&empty); down(&mutex); insert_item(item); up(&mutex); up(&full); N = 6 Semaphore mutex = 0 Semaphore full = 0 Semaphore empty = 5 down (&mutex) producer1 consumer1 producer2 consumer2 Run queue Mutex queue Full queue Empty queue
Producer/Consumer Producer down(&empty); down(&mutex); insert_item(item); up(&mutex); up(&full); N = 6 Semaphore mutex = 0 Semaphore full = 0 Semaphore empty = 5 insert_item(item) producer1 consumer1 producer2 consumer2 Run queue Mutex queue X Full queue Empty queue
Producer/Consumer N = 6 Semaphore mutex = 0 Semaphore full = 0 Semaphore empty = 5 … producer1 consumer1 producer2 consumer2 Run queue Quantum ends Mutex queue X Full queue Empty queue
Producer/Consumer Consumer down(&full); down(&mutex); consume_item(); up(&mutex); up(&empty); N = 6 Semaphore mutex = 0 Semaphore full = 0 Semaphore empty = 5 down (&full) producer2 consumer2 producer1 Run queue Mutex queue X Full queue consumer1 Empty queue
Producer/Consumer N = 6 Semaphore mutex = 0 Semaphore full = 0 Semaphore empty = 5 … producer2 consumer2 producer1 Run queue Mutex queue X Full queue consumer1 Empty queue
Producer/Consumer Producer down(&empty); down(&mutex); insert_item(item); up(&mutex); up(&full); N = 6 Semaphore mutex = 0 Semaphore full = 0 Semaphore empty = 4 down (&empty) producer2 consumer2 producer1 Run queue Mutex queue X Full queue consumer1 Empty queue
Producer/Consumer Producer down(&empty); down(&mutex); insert_item(item); up(&mutex); up(&full); N = 6 Semaphore mutex = 0 Semaphore full = 0 Semaphore empty = 4 down (&mutex) consumer2 producer1 Run queue Mutex queue producer2 X Full queue consumer1 Empty queue
Producer/Consumer N = 6 Semaphore mutex = 0 Semaphore full = 0 Semaphore empty = 4 … consumer2 producer1 Run queue Mutex queue producer2 X Full queue consumer1 Empty queue
Producer/Consumer Consumer down(&full); down(&mutex); consume_item(); up(&mutex); up(&empty); N = 6 Semaphore mutex = 0 Semaphore full = 0 Semaphore empty = 4 down (&full) producer1 Run queue Mutex queue producer2 X Full queue consumer1 consumer2 Empty queue
Producer/Consumer N = 6 Semaphore mutex = 0 Semaphore full = 0 Semaphore empty = 4 … producer1 Run queue Mutex queue producer2 X Full queue consumer1 consumer2 Empty queue
Producer/Consumer Producer down(&empty); down(&mutex); insert_item(item); up(&mutex); up(&full); N = 6 Semaphore mutex = 1 Semaphore full = 0 Semaphore empty = 4 up (&mutex) producer2 producer1 Run queue Mutex queue X Full queue consumer1 consumer2 Empty queue
Producer/Consumer Producer down(&empty); down(&mutex); insert_item(item); up(&mutex); up(&full); N = 6 Semaphore mutex = 1 Semaphore full = 1 Semaphore empty = 4 up (&full) producer2 consumer1 producer1 Run queue Mutex queue X Full queue consumer2 Empty queue
Producer/Consumer N = 6 Semaphore mutex = 1 Semaphore full = 1 Semaphore empty = 4 … producer2 consumer1 producer1 Run queue Quantum ends Mutex queue X Full queue consumer2 Empty queue
Producer/Consumer N = 6 Semaphore mutex = 0 Semaphore full = 1 Semaphore empty = 4 down (&mutex) producer2 consumer1 producer1 Run queue Mutex queue X Full queue consumer2 Empty queue
Producer/Consumer N = 6 Semaphore mutex = 0 Semaphore full = 1 Semaphore empty = 4 insert_item (item) producer2 consumer1 producer1 Run queue Mutex queue X Full queue consumer2 X Empty queue
Producer/Consumer N = 6 Semaphore mutex = 1 Semaphore full = 1 Semaphore empty = 4 up (&mutex) producer2 consumer1 producer1 Run queue Mutex queue X Full queue consumer2 X Empty queue
Producer/Consumer N = 6 Semaphore mutex = 1 Semaphore full = 2 Semaphore empty = 4 up (&full) producer2 consumer1 consumer2 producer1 Run queue Mutex queue X Full queue X Empty queue
Producer/Consumer N = 6 Semaphore mutex = 1 Semaphore full = 2 Semaphore empty = 4 … producer2 consumer1 consumer2 producer1 Run queue Quantum ends Mutex queue X Full queue X Empty queue
Producer/Consumer N = 6 Semaphore mutex = 1 Semaphore full = 1 Semaphore empty = 4 down (&full) producer2 consumer1 consumer2 producer1 Run queue Mutex queue X Full queue X Empty queue
Producer/Consumer N = 6 Semaphore mutex = 0 Semaphore full = 1 Semaphore empty = 4 down (&mutex) producer2 consumer1 consumer2 producer1 Run queue Mutex queue X Full queue X Empty queue
Producer/Consumer N = 6 Semaphore mutex = 0 Semaphore full = 1 Semaphore empty = 4 consume_item() producer2 consumer1 consumer2 producer1 Run queue Mutex queue X Full queue Empty queue
Producer/Consumer N = 6 Semaphore mutex = 1 Semaphore full = 1 Semaphore empty = 4 up(&mutex) producer2 consumer1 consumer2 producer1 Run queue Mutex queue X Full queue Empty queue
Producer/Consumer N = 6 Semaphore mutex = 1 Semaphore full = 1 Semaphore empty = 5 up(&empty) producer2 consumer1 consumer2 producer1 Run queue Mutex queue X Full queue Empty queue