xref: /minix3/tests/lib/librt/t_sem.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /* $NetBSD: t_sem.c,v 1.2 2010/11/08 13:05:49 njoly Exp $ */
2*11be35a1SLionel Sambuc 
3*11be35a1SLionel Sambuc /*
4*11be35a1SLionel Sambuc  * Copyright (c) 2008, 2010 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc  * All rights reserved.
6*11be35a1SLionel Sambuc  *
7*11be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc  * are met:
10*11be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc  *
16*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*11be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*11be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*11be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*11be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*11be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*11be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*11be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*11be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*11be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*11be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
27*11be35a1SLionel Sambuc  */
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc /*
30*11be35a1SLionel Sambuc  * Copyright (C) 2000 Jason Evans <jasone@freebsd.org>.
31*11be35a1SLionel Sambuc  * All rights reserved.
32*11be35a1SLionel Sambuc  *
33*11be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
34*11be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
35*11be35a1SLionel Sambuc  * are met:
36*11be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
37*11be35a1SLionel Sambuc  *    notice(s), this list of conditions and the following disclaimer as
38*11be35a1SLionel Sambuc  *    the first lines of this file unmodified other than the possible
39*11be35a1SLionel Sambuc  *    addition of one or more copyright notices.
40*11be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
41*11be35a1SLionel Sambuc  *    notice(s), this list of conditions and the following disclaimer in
42*11be35a1SLionel Sambuc  *    the documentation and/or other materials provided with the
43*11be35a1SLionel Sambuc  *    distribution.
44*11be35a1SLionel Sambuc  *
45*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
46*11be35a1SLionel Sambuc  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47*11be35a1SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48*11be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
49*11be35a1SLionel Sambuc  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50*11be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51*11be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
52*11be35a1SLionel Sambuc  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
53*11be35a1SLionel Sambuc  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
54*11be35a1SLionel Sambuc  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
55*11be35a1SLionel Sambuc  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56*11be35a1SLionel Sambuc  */
57*11be35a1SLionel Sambuc 
58*11be35a1SLionel Sambuc #include <sys/cdefs.h>
59*11be35a1SLionel Sambuc __COPYRIGHT("@(#) Copyright (c) 2008, 2010\
60*11be35a1SLionel Sambuc  The NetBSD Foundation, inc. All rights reserved.");
61*11be35a1SLionel Sambuc __RCSID("$NetBSD: t_sem.c,v 1.2 2010/11/08 13:05:49 njoly Exp $");
62*11be35a1SLionel Sambuc 
63*11be35a1SLionel Sambuc #include <sys/wait.h>
64*11be35a1SLionel Sambuc 
65*11be35a1SLionel Sambuc #include <errno.h>
66*11be35a1SLionel Sambuc #include <fcntl.h>
67*11be35a1SLionel Sambuc #include <semaphore.h>
68*11be35a1SLionel Sambuc #include <stdio.h>
69*11be35a1SLionel Sambuc #include <unistd.h>
70*11be35a1SLionel Sambuc 
71*11be35a1SLionel Sambuc #include <atf-c.h>
72*11be35a1SLionel Sambuc 
73*11be35a1SLionel Sambuc #define NCHILDREN 10
74*11be35a1SLionel Sambuc 
75*11be35a1SLionel Sambuc ATF_TC(basic);
ATF_TC_HEAD(basic,tc)76*11be35a1SLionel Sambuc ATF_TC_HEAD(basic, tc)
77*11be35a1SLionel Sambuc {
78*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks basic functionality of POSIX "
79*11be35a1SLionel Sambuc 	    "semaphores");
80*11be35a1SLionel Sambuc }
ATF_TC_BODY(basic,tc)81*11be35a1SLionel Sambuc ATF_TC_BODY(basic, tc)
82*11be35a1SLionel Sambuc {
83*11be35a1SLionel Sambuc 	int val;
84*11be35a1SLionel Sambuc 	sem_t *sem_b;
85*11be35a1SLionel Sambuc 
86*11be35a1SLionel Sambuc 	if (sysconf(_SC_SEMAPHORES) == -1)
87*11be35a1SLionel Sambuc 		atf_tc_skip("POSIX semaphores not supported");
88*11be35a1SLionel Sambuc 
89*11be35a1SLionel Sambuc 	sem_b = sem_open("/sem_b", O_CREAT | O_EXCL, 0644, 0);
90*11be35a1SLionel Sambuc 	ATF_REQUIRE(sem_b != SEM_FAILED);
91*11be35a1SLionel Sambuc 
92*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(sem_getvalue(sem_b, &val), 0);
93*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(val, 0);
94*11be35a1SLionel Sambuc 
95*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(sem_post(sem_b), 0);
96*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(sem_getvalue(sem_b, &val), 0);
97*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(val, 1);
98*11be35a1SLionel Sambuc 
99*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(sem_wait(sem_b), 0);
100*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(sem_trywait(sem_b), -1);
101*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(errno, EAGAIN);
102*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(sem_post(sem_b), 0);
103*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(sem_trywait(sem_b), 0);
104*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(sem_post(sem_b), 0);
105*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(sem_wait(sem_b), 0);
106*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(sem_post(sem_b), 0);
107*11be35a1SLionel Sambuc 
108*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(sem_close(sem_b), 0);
109*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(sem_unlink("/sem_b"), 0);
110*11be35a1SLionel Sambuc }
111*11be35a1SLionel Sambuc 
112*11be35a1SLionel Sambuc ATF_TC(child);
ATF_TC_HEAD(child,tc)113*11be35a1SLionel Sambuc ATF_TC_HEAD(child, tc)
114*11be35a1SLionel Sambuc {
115*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks using semaphores to synchronize "
116*11be35a1SLionel Sambuc 	    "parent with multiple child processes");
117*11be35a1SLionel Sambuc }
ATF_TC_BODY(child,tc)118*11be35a1SLionel Sambuc ATF_TC_BODY(child, tc)
119*11be35a1SLionel Sambuc {
120*11be35a1SLionel Sambuc 	pid_t children[NCHILDREN];
121*11be35a1SLionel Sambuc 	unsigned i, j;
122*11be35a1SLionel Sambuc 	sem_t *sem_a;
123*11be35a1SLionel Sambuc 	int status;
124*11be35a1SLionel Sambuc 
125*11be35a1SLionel Sambuc 	pid_t pid;
126*11be35a1SLionel Sambuc 
127*11be35a1SLionel Sambuc 	if (sysconf(_SC_SEMAPHORES) == -1)
128*11be35a1SLionel Sambuc 		atf_tc_skip("POSIX semaphores not supported");
129*11be35a1SLionel Sambuc 
130*11be35a1SLionel Sambuc 	sem_a = sem_open("/sem_a", O_CREAT | O_EXCL, 0644, 0);
131*11be35a1SLionel Sambuc 	ATF_REQUIRE(sem_a != SEM_FAILED);
132*11be35a1SLionel Sambuc 
133*11be35a1SLionel Sambuc 	for (j = 1; j <= 2; j++) {
134*11be35a1SLionel Sambuc 		for (i = 0; i < NCHILDREN; i++) {
135*11be35a1SLionel Sambuc 			switch ((pid = fork())) {
136*11be35a1SLionel Sambuc 			case -1:
137*11be35a1SLionel Sambuc 				atf_tc_fail("fork() returned -1");
138*11be35a1SLionel Sambuc 			case 0:
139*11be35a1SLionel Sambuc 				printf("PID %d waiting for semaphore...\n",
140*11be35a1SLionel Sambuc 				    getpid());
141*11be35a1SLionel Sambuc 				ATF_REQUIRE_MSG(sem_wait(sem_a) == 0,
142*11be35a1SLionel Sambuc 				    "sem_wait failed; iteration %d", j);
143*11be35a1SLionel Sambuc 				printf("PID %d got semaphore\n", getpid());
144*11be35a1SLionel Sambuc 				_exit(0);
145*11be35a1SLionel Sambuc 			default:
146*11be35a1SLionel Sambuc 				children[i] = pid;
147*11be35a1SLionel Sambuc 				break;
148*11be35a1SLionel Sambuc 			}
149*11be35a1SLionel Sambuc 		}
150*11be35a1SLionel Sambuc 
151*11be35a1SLionel Sambuc 		for (i = 0; i < NCHILDREN; i++) {
152*11be35a1SLionel Sambuc 			sleep(1);
153*11be35a1SLionel Sambuc 			printf("main loop %d: posting...\n", j);
154*11be35a1SLionel Sambuc 			ATF_REQUIRE_EQ(sem_post(sem_a), 0);
155*11be35a1SLionel Sambuc 		}
156*11be35a1SLionel Sambuc 
157*11be35a1SLionel Sambuc 		for (i = 0; i < NCHILDREN; i++) {
158*11be35a1SLionel Sambuc 			ATF_REQUIRE_EQ(waitpid(children[i], &status, 0), children[i]);
159*11be35a1SLionel Sambuc 			ATF_REQUIRE(WIFEXITED(status));
160*11be35a1SLionel Sambuc 			ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
161*11be35a1SLionel Sambuc 		}
162*11be35a1SLionel Sambuc 	}
163*11be35a1SLionel Sambuc 
164*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(sem_close(sem_a), 0);
165*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(sem_unlink("/sem_a"), 0);
166*11be35a1SLionel Sambuc }
167*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)168*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
169*11be35a1SLionel Sambuc {
170*11be35a1SLionel Sambuc 
171*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, basic);
172*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, child);
173*11be35a1SLionel Sambuc 
174*11be35a1SLionel Sambuc 	return atf_no_error();
175*11be35a1SLionel Sambuc }
176