Debian Codegraph @

listings/chapter-5/sem_pv.c

raw
1 /***********************************************************************
2 * Code listing from "Advanced Linux Programming," by CodeSourcery LLC *
3 * Copyright (C) 2001 by New Riders Publishing *
4 * See COPYRIGHT for license information. *
5 ***********************************************************************/
6
7 #include <sys/types.h>
8 #include <sys/ipc.h>
9 #include <sys/sem.h>
10
11 /* Wait on a binary semaphore. Block until the semaphore value is
12 positive, then decrement it by one. */
13
14 int binary_semaphore_wait (int semid)
15 {
16 struct sembuf operations[1];
17 /* Use the first (and only) semaphore. */
18 operations[0].sem_num = 0;
19 /* Decrement by 1. */
20 operations[0].sem_op = -1;
21 /* Permit undo'ing. */
22 operations[0].sem_flg = SEM_UNDO;
23
24 return semop (semid, operations, 1);
25 }
26
27 /* Post to a binary semaphore: increment its value by one. This
28 returns immediately. */
29
30 int binary_semaphore_post (int semid)
31 {
32 struct sembuf operations[1];
33 /* Use the first (and only) semaphore. */
34 operations[0].sem_num = 0;
35 /* Increment by 1. */
36 operations[0].sem_op = 1;
37 /* Permit undo'ing. */
38 operations[0].sem_flg = SEM_UNDO;
39
40 return semop (semid, operations, 1);
41 }