aboutsummaryrefslogtreecommitdiff
path: root/tests/semtest.c
diff options
context:
space:
mode:
authortoni <toni@devlap.local>2015-10-13 09:01:50 +0200
committertoni <toni@devlap.local>2015-10-13 09:36:52 +0200
commitc89e18ec972f165650a453aa8bd8b30309e323e6 (patch)
tree574ed98c31b610fca94cff193767af3d10aabd52 /tests/semtest.c
parent8315e840358f1ef19295a2cf899144b2faa3ad3d (diff)
added semaphore tests
Diffstat (limited to 'tests/semtest.c')
-rw-r--r--tests/semtest.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/tests/semtest.c b/tests/semtest.c
new file mode 100644
index 0000000..7968127
--- /dev/null
+++ b/tests/semtest.c
@@ -0,0 +1,42 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <semaphore.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <fcntl.h>
+
+
+sem_t *mysem = NULL;
+pid_t child;
+
+#define LOG(cmd) fprintf(stderr, "%s\n", cmd);
+
+int main(int argc, char **argv) {
+ sem_unlink("/mysem");
+ if ( (mysem = sem_open("/mysem", O_CREAT, S_IRUSR | S_IWUSR, 1)) != NULL ) {
+ if ( (child = fork()) == 0 ) {
+ /* child */
+ sleep(1);
+ LOG("child: sempost");
+ sem_post(mysem);
+ LOG("child: done");
+ sleep(1);
+ exit(0);
+ } else if (child > 0) {
+ /* parent */
+ LOG("parent: semwait");
+ sem_wait(mysem);
+ LOG("parent: waitpid");
+ waitpid(child, NULL, 0);
+ } else if (child < 0) {
+ perror("fork");
+ }
+ } else {
+ sem_close(mysem);
+ exit(1);
+ }
+ exit(0);
+}
+