From c89e18ec972f165650a453aa8bd8b30309e323e6 Mon Sep 17 00:00:00 2001
From: toni <toni@devlap.local>
Date: Tue, 13 Oct 2015 09:01:50 +0200
Subject: added semaphore tests

---
 tests/semtest.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
 create mode 100644 tests/semtest.c

(limited to 'tests/semtest.c')

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);
+}
+
-- 
cgit v1.2.3