Expand PMF_FN_* macros.
[netbsd-mini2440.git] / regress / lib / librt / sem / sem.c
blobde0e4c3d0e8ae8aab789de4732b9b5b2fde28532
1 /* $NetBSD: sem.c,v 1.2 2003/02/28 05:29:48 matt Exp $ */
3 /*
4 * Copyright (C) 2000 Jason Evans <jasone@freebsd.org>.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
17 * distribution.
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.
32 #include <sys/wait.h>
33 #include <assert.h>
34 #include <stdio.h>
35 #include <err.h>
36 #include <fcntl.h>
37 #include <errno.h>
38 #include <semaphore.h>
39 #include <stdlib.h>
40 #include <unistd.h>
42 #define NCHILDREN 10
44 static void
45 child(sem_t *sem)
48 #ifdef DEBUG
49 printf("PID %d waiting for semaphore...\n", getpid());
50 #endif
51 if (sem_wait(sem))
52 err(1, "sem_wait");
53 #ifdef DEBUG
54 printf("PID %d got semaphore\n", getpid());
55 #endif
58 static sem_t *
59 create_sem(const char *name)
61 sem_t *sem;
63 (void)sem_unlink(name);
64 sem = sem_open(name, O_CREAT | O_EXCL, 0644, 0);
65 assert(sem != SEM_FAILED);
67 return (sem);
70 static void
71 delete_sem(sem_t *sem, const char *name)
74 assert(0 == sem_close(sem));
75 assert(0 == sem_unlink(name));
78 static void
79 dosem(void)
81 sem_t *sem_a, *sem_b;
82 pid_t children[NCHILDREN];
83 unsigned i;
84 int val, status;
85 pid_t pid;
87 assert(-1 != sysconf(_SC_SEMAPHORES));
89 sem_b = create_sem("/sem_b");
90 assert(0 == sem_getvalue(sem_b, &val));
91 assert(0 == val);
93 assert(0 == sem_post(sem_b));
94 assert(0 == sem_getvalue(sem_b, &val));
95 assert(1 == 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())) {
112 case -1:
113 abort();
114 case 0:
115 child(sem_a);
116 _exit(0);
117 default:
118 children[i] = pid;
119 break;
123 for (i = 0; i < NCHILDREN; i++) {
124 sleep(1);
125 #ifdef DEBUG
126 printf("main loop 1: posting...\n");
127 #endif
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())) {
139 case -1:
140 abort();
141 case 0:
142 child(sem_a);
143 _exit(0);
144 default:
145 children[i] = pid;
146 break;
150 for (i = 0; i < NCHILDREN; i++) {
151 sleep(1);
152 #ifdef DEBUG
153 printf("main loop 2: posting...\n");
154 #endif
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[])
170 #ifdef DEBUG
171 printf("Test begin\n");
172 #endif
173 dosem();
175 #ifdef DEBUG
176 printf("Test end\n");
177 #endif
178 return 0;