14 #define SEM_NAME "/semtst"
16 int test_unnamed(void);
18 int test_named2(void);
27 printf("testing unnamed process-shared semaphore\n");
28 s = (sem_t *)mmap(NULL, sizeof(sem_t), PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED,
31 err(1, "mmap failed");
32 if (sem_init(s, 1, 0))
33 err(2, "sem_init failed");
34 if ((pid = fork()) == 0) {
35 printf("child: sem_wait()\n");
37 err(3, "sem_wait failed");
38 printf("child: sem_wait() returned\n");
42 printf("parent: sem_post()\n");
44 err(4, "sem_post failed");
45 waitpid(pid, &status, 0);
46 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
61 printf("testing named process-shared semaphore\n");
63 s = sem_open(SEM_NAME, O_CREAT, 0777, 0);
65 err(1, "sem_open failed");
66 s2 = sem_open(SEM_NAME, O_CREAT, 0777, 0);
68 err(2, "second sem_open call failed");
71 "two sem_open calls for same semaphore do not return same address");
73 err(4, "sem_close failed");
74 if ((pid = fork()) == 0) {
75 printf("child: sem_wait()\n");
77 err(5, "sem_wait failed");
78 printf("child: sem_wait() returned\n");
82 printf("parent: sem_post()\n");
84 err(6, "sem_post failed");
85 waitpid(pid, &status, 0);
86 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
93 err(7, "sem_close failed");
103 printf("testing named process-shared semaphore, O_EXCL cases\n");
104 sem_unlink(SEM_NAME);
105 s = sem_open(SEM_NAME, O_CREAT | O_EXCL, 0777, 0);
107 err(1, "sem_open failed");
108 s2 = sem_open(SEM_NAME, O_CREAT | O_EXCL, 0777, 0);
109 if (s2 != SEM_FAILED)
110 errx(2, "second sem_open call wrongly succeeded");
112 err(3, "second sem_open call failed with wrong errno");
114 s3 = sem_open(SEM_NAME, 0);
115 if (s3 == SEM_FAILED)
116 err(4, "third sem_open call failed");
119 "two sem_open calls for same semaphore do not return same address");
121 err(6, "sem_close failed");
124 err(7, "sem_close failed");