Semaphores

Semaphores - Semaphores Semaphores are a generalization of...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Semaphores Semaphores are a generalization of mutexes. A mutex allows only one thread to execute the code guarded by a mutex_lock / mutex_unlock pair. A semaphore allows a maximum number of threads between a sema_wait, sema_post. A semaphore is initialized with an initial counter that is greater or equal to 0. You could implement a mutex using a semaphore with an initial counter of 1.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Semaphore Calls Declaration: #include <synch.h> sema_t sem; Initialization: sema_init( sema_t *sem, int counter, int type, void * arg)); Decrement Semaphore: sema_wait( sema_t *sem); Increment Semaphore sema_post(sema_t *sem);
Background image of page 2
Semaphore Calls Implementation Pseudocode: sema_init(sem_t *sem, counter){ sem -> count = counter; } sema_wait(sema_t *sem){ sem ->count--; if(sem ->count < 0){ wait(); }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Semaphore Calls Implementation sema_post(sema_t *sem){ sem ->count++; if(there is a thread waiting){ wake up the thread that has been waiting the longest.
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/30/2012 for the course CS 252 taught by Professor Gustavorodriguez during the Fall '11 term at Purdue.

Page1 / 9

Semaphores - Semaphores Semaphores are a generalization of...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online