1 /* $NetBSD: sem.c,v 1.2 2003/02/28 05:29:48 matt Exp $ */
4 * Copyright (C) 2000 Jason Evans <jasone@freebsd.org>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice(s), this list of conditions and the following disclaimer as
12 * the first lines of this file unmodified other than the possible
13 * addition of one or more copyright notices.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice(s), this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
26 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
28 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <semaphore.h>
49 printf("PID %d waiting for semaphore...\n", getpid());
54 printf("PID %d got semaphore\n", getpid());
59 create_sem(const char *name
)
63 (void)sem_unlink(name
);
64 sem
= sem_open(name
, O_CREAT
| O_EXCL
, 0644, 0);
65 assert(sem
!= SEM_FAILED
);
71 delete_sem(sem_t
*sem
, const char *name
)
74 assert(0 == sem_close(sem
));
75 assert(0 == sem_unlink(name
));
82 pid_t children
[NCHILDREN
];
87 assert(-1 != sysconf(_SC_SEMAPHORES
));
89 sem_b
= create_sem("/sem_b");
90 assert(0 == sem_getvalue(sem_b
, &val
));
93 assert(0 == sem_post(sem_b
));
94 assert(0 == sem_getvalue(sem_b
, &val
));
97 assert(0 == sem_wait(sem_b
));
98 assert(-1 == sem_trywait(sem_b
));
99 assert(EAGAIN
== errno
);
100 assert(0 == sem_post(sem_b
));
101 assert(0 == sem_trywait(sem_b
));
102 assert(0 == sem_post(sem_b
));
103 assert(0 == sem_wait(sem_b
));
104 assert(0 == sem_post(sem_b
));
106 delete_sem(sem_b
, "/sem_b");
108 sem_a
= create_sem("/sem_a");
110 for (i
= 0; i
< NCHILDREN
; i
++) {
111 switch ((pid
= fork())) {
123 for (i
= 0; i
< NCHILDREN
; i
++) {
126 printf("main loop 1: posting...\n");
128 assert(0 == sem_post(sem_a
));
131 for (i
= 0; i
< NCHILDREN
; i
++) {
132 assert(children
[i
] == waitpid(children
[i
], &status
, 0));
133 assert(WIFEXITED(status
));
134 assert(0 == WEXITSTATUS(status
));
137 for (i
= 0; i
< NCHILDREN
; i
++) {
138 switch ((pid
= fork())) {
150 for (i
= 0; i
< NCHILDREN
; i
++) {
153 printf("main loop 2: posting...\n");
155 assert(0 == sem_post(sem_a
));
158 for (i
= 0; i
< NCHILDREN
; i
++) {
159 assert(children
[i
] == waitpid(children
[i
], &status
, 0));
160 assert(WIFEXITED(status
));
161 assert(0 == WEXITSTATUS(status
));
164 delete_sem(sem_a
, "/sem_a");
168 main(int argc
, char *argv
[])
171 printf("Test begin\n");
176 printf("Test end\n");