Semaphores
An Introduction |
Prof. David Bernstein
|
Computer Science Department |
bernstdh@jmu.edu |
sem_t *sem_open(const char *name, int oflag, /* mode_t mode, unsigned value */)
name
|
The name of the semaphore |
oflag
|
A flag that controls the opening (see below) |
mode
|
O_RDONLY, O_WRONLY, or O_RDWR (required iff O_CREAT is set) |
value
|
Required only O_CREAT is set |
Return | The handle for the semaphore on success; SEM_FAILED on error |
O_CREAT
is used to create a semaphore. O_CREAT
& O_EXCL
is used to create a semaphore but fail if it
already exists.
NAME_MAX - 4
characterssem_t
valueint sem_close(sem_t *sem)
sem
|
The handle to the semaphore |
Return | 0 on success; -1 on error |
Note that sem_close()
does not delete the semaphore.
ls -l /dev/shm/sem.*
EINTR
(regardless of whther the SA_RESTART
flag
was used when the signal handler was installed/established)sem_wait()
-pthread
wait()
Note: One semaphore is being used to satisfy the sequencing constraint and another is being used for "alerting".
Note: The semaphore is initialized to 1 so that whichever process checks first can "enter".