80 likes | 276 Views
Semaphores. Race Condition. Occurs when multiple processes access and manipulate the same data concurrently The outcome of the execution depends on the particular order in which the access takes place
E N D
Race Condition • Occurs when multiple processes access and manipulate the same data concurrently • The outcome of the execution depends on the particular order in which the access takes place • Ex. Two processes try to write to the same memory location, which one should go ahead and write?
To enforce execution Order • Process busy waits • While (CONDITION_NOT_SATISFIED); • Process goes to sleep until being explicitly woke up by others • Semaphores
Semaphores • A lock that is associated with a counter value • Counter: Zero or positive number
POSIX Semaphores • #include <semaphore.h> • sem_t sem_name; • sem_init(sem_t *sem, int pshared, unsigned int value); • sem_wait(&sem_name); • sem_post(&sem_name); • sem_getvalue(sem_t *sem, int *valp); • sem_destroy(&sem_name); • gcc program.c -lrt
Links http://www.csc.villanova.edu/~mdamian/threads/posixsem.html System V Semaphores API: http://beej.us/guide/bgipc/output/html/multipage/semaphores.html