13 #define SEM_NAME "/semtst"
15 int test_unnamed(void);
17 int test_named2(void);
26 printf("testing unnamed process-shared semaphore\n");
27 s = (sem_t *)mmap(NULL, sizeof(sem_t), PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED,
30 err(1, "mmap failed");
31 if (sem_init(s, 1, 0))
32 err(2, "sem_init failed");
33 if ((pid = fork()) == 0) {
34 printf("child: sem_wait()\n");
36 err(3, "sem_wait failed");
37 printf("child: sem_wait() returned\n");
41 printf("parent: sem_post()\n");
43 err(4, "sem_post failed");
44 waitpid(pid, &status, 0);
45 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
60 printf("testing named process-shared semaphore\n");
62 s = sem_open(SEM_NAME, O_CREAT, 0777, 0);
64 err(1, "sem_open failed");
65 s2 = sem_open(SEM_NAME, O_CREAT, 0777, 0);
67 err(2, "second sem_open call failed");
70 "two sem_open calls for same semaphore do not return same address");
72 err(4, "sem_close failed");
73 if ((pid = fork()) == 0) {
74 printf("child: sem_wait()\n");
76 err(5, "sem_wait failed");
77 printf("child: sem_wait() returned\n");
81 printf("parent: sem_post()\n");
83 err(6, "sem_post failed");
84 waitpid(pid, &status, 0);
85 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
92 err(7, "sem_close failed");
102 printf("testing named process-shared semaphore, O_EXCL cases\n");
103 sem_unlink(SEM_NAME);
104 s = sem_open(SEM_NAME, O_CREAT | O_EXCL, 0777, 0);
106 err(1, "sem_open failed");
107 s2 = sem_open(SEM_NAME, O_CREAT | O_EXCL, 0777, 0);
108 if (s2 != SEM_FAILED)
109 errx(2, "second sem_open call wrongly succeeded");
111 err(3, "second sem_open call failed with wrong errno");
113 s3 = sem_open(SEM_NAME, 0);
114 if (s3 == SEM_FAILED)
115 err(4, "third sem_open call failed");
118 "two sem_open calls for same semaphore do not return same address");
120 err(6, "sem_close failed");
123 err(7, "sem_close failed");