1 /* $NetBSD: t_sem.c,v 1.2 2010/11/08 13:05:49 njoly Exp $ */
4 * Copyright (c) 2008, 2010 The NetBSD Foundation, Inc.
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, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
30 * Copyright (C) 2000 Jason Evans <jasone@freebsd.org>.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice(s), this list of conditions and the following disclaimer as
38 * the first lines of this file unmodified other than the possible
39 * addition of one or more copyright notices.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice(s), this list of conditions and the following disclaimer in
42 * the documentation and/or other materials provided with the
45 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
46 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
49 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
52 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
53 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
54 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
55 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 #include <sys/cdefs.h>
59 __COPYRIGHT("@(#) Copyright (c) 2008, 2010\
60 The NetBSD Foundation, inc. All rights reserved.");
61 __RCSID("$NetBSD: t_sem.c,v 1.2 2010/11/08 13:05:49 njoly Exp $");
67 #include <semaphore.h>
76 ATF_TC_HEAD(basic
, tc
)
78 atf_tc_set_md_var(tc
, "descr", "Checks basic functionality of POSIX "
81 ATF_TC_BODY(basic
, tc
)
86 if (sysconf(_SC_SEMAPHORES
) == -1)
87 atf_tc_skip("POSIX semaphores not supported");
89 sem_b
= sem_open("/sem_b", O_CREAT
| O_EXCL
, 0644, 0);
90 ATF_REQUIRE(sem_b
!= SEM_FAILED
);
92 ATF_REQUIRE_EQ(sem_getvalue(sem_b
, &val
), 0);
93 ATF_REQUIRE_EQ(val
, 0);
95 ATF_REQUIRE_EQ(sem_post(sem_b
), 0);
96 ATF_REQUIRE_EQ(sem_getvalue(sem_b
, &val
), 0);
97 ATF_REQUIRE_EQ(val
, 1);
99 ATF_REQUIRE_EQ(sem_wait(sem_b
), 0);
100 ATF_REQUIRE_EQ(sem_trywait(sem_b
), -1);
101 ATF_REQUIRE_EQ(errno
, EAGAIN
);
102 ATF_REQUIRE_EQ(sem_post(sem_b
), 0);
103 ATF_REQUIRE_EQ(sem_trywait(sem_b
), 0);
104 ATF_REQUIRE_EQ(sem_post(sem_b
), 0);
105 ATF_REQUIRE_EQ(sem_wait(sem_b
), 0);
106 ATF_REQUIRE_EQ(sem_post(sem_b
), 0);
108 ATF_REQUIRE_EQ(sem_close(sem_b
), 0);
109 ATF_REQUIRE_EQ(sem_unlink("/sem_b"), 0);
113 ATF_TC_HEAD(child
, tc
)
115 atf_tc_set_md_var(tc
, "descr", "Checks using semaphores to synchronize "
116 "parent with multiple child processes");
118 ATF_TC_BODY(child
, tc
)
120 pid_t children
[NCHILDREN
];
127 if (sysconf(_SC_SEMAPHORES
) == -1)
128 atf_tc_skip("POSIX semaphores not supported");
130 sem_a
= sem_open("/sem_a", O_CREAT
| O_EXCL
, 0644, 0);
131 ATF_REQUIRE(sem_a
!= SEM_FAILED
);
133 for (j
= 1; j
<= 2; j
++) {
134 for (i
= 0; i
< NCHILDREN
; i
++) {
135 switch ((pid
= fork())) {
137 atf_tc_fail("fork() returned -1");
139 printf("PID %d waiting for semaphore...\n",
141 ATF_REQUIRE_MSG(sem_wait(sem_a
) == 0,
142 "sem_wait failed; iteration %d", j
);
143 printf("PID %d got semaphore\n", getpid());
151 for (i
= 0; i
< NCHILDREN
; i
++) {
153 printf("main loop %d: posting...\n", j
);
154 ATF_REQUIRE_EQ(sem_post(sem_a
), 0);
157 for (i
= 0; i
< NCHILDREN
; i
++) {
158 ATF_REQUIRE_EQ(waitpid(children
[i
], &status
, 0), children
[i
]);
159 ATF_REQUIRE(WIFEXITED(status
));
160 ATF_REQUIRE_EQ(WEXITSTATUS(status
), 0);
164 ATF_REQUIRE_EQ(sem_close(sem_a
), 0);
165 ATF_REQUIRE_EQ(sem_unlink("/sem_a"), 0);
171 ATF_TP_ADD_TC(tp
, basic
);
172 ATF_TP_ADD_TC(tp
, child
);
174 return atf_no_error();