aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile5
-rw-r--r--tests/semconfig.h1
-rw-r--r--tests/semtest.c8
-rw-r--r--tests/semtest2.c43
4 files changed, 54 insertions, 3 deletions
diff --git a/tests/Makefile b/tests/Makefile
index 5a041e6..436d00f 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -3,13 +3,18 @@ LDFLAGS = -lpthread -lrt
CC = gcc
SOURCES = $(wildcard *.c)
BINARIES = $(patsubst %.c,%,$(SOURCES))
+DEPS = $(patsubst %.c,%.d,$(SOURCES))
all: $(BINARIES)
+semtest2:
+ $(CC) $(CFLAGS) $(LDFLAGS) ../ui_ipc.o semtest2.c -o semtest2
+
%: %.c
$(CC) $(CFLAGS) $(LDFLAGS) $< -o $(patsubst %.c,%,$<)
clean:
+ rm -f $(DEPS)
rm -f $(BINARIES)
.PHONY: all install clean
diff --git a/tests/semconfig.h b/tests/semconfig.h
index e34984d..0ad08b3 100644
--- a/tests/semconfig.h
+++ b/tests/semconfig.h
@@ -2,6 +2,7 @@
#define CONFIG_H 1
#define LOG(text) fprintf(stderr, "%s\n", text);
+#define CMD(cmd) LOG(cmd); cmd;
#define TESTSEM "/testsem"
#define CNTSEM "/testcnt"
diff --git a/tests/semtest.c b/tests/semtest.c
index 7968127..f462a6e 100644
--- a/tests/semtest.c
+++ b/tests/semtest.c
@@ -7,15 +7,16 @@
#include <sys/wait.h>
#include <fcntl.h>
+#include "semconfig.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 ) {
+ sem_unlink(TESTSEM);
+ if ( (mysem = sem_open(TESTSEM, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 0)) != NULL ) {
if ( (child = fork()) == 0 ) {
/* child */
sleep(1);
@@ -37,6 +38,7 @@ int main(int argc, char **argv) {
sem_close(mysem);
exit(1);
}
+ sem_unlink(TESTSEM);
exit(0);
}
diff --git a/tests/semtest2.c b/tests/semtest2.c
new file mode 100644
index 0000000..c015df8
--- /dev/null
+++ b/tests/semtest2.c
@@ -0,0 +1,43 @@
+#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>
+
+#include "../ui_ipc.h"
+
+pid_t child;
+
+#define LOG(cmd) fprintf(stderr, "%s\n", cmd);
+
+int main(int argc, char **argv) {
+ if (ui_ipc_init(1) != 0) {
+ perror("ui_ipc_init");
+ exit(1);
+ }
+ if ( (child = fork()) == 0 ) {
+ /* child */
+ sleep(1);
+ LOG("child: sempost");
+ ui_ipc_sempost(SEM_RD);
+ LOG("child: done");
+ sleep(1);
+ exit(0);
+ } else if (child > 0) {
+ /* parent */
+ LOG("parent: semwait");
+ ui_ipc_semwait(SEM_RD);
+ LOG("parent: waitpid");
+ waitpid(child, NULL, 0);
+ } else if (child < 0) {
+ perror("fork");
+ exit(1);
+ }
+ ui_ipc_free(1);
+
+ exit(0);
+}
+