aboutsummaryrefslogtreecommitdiff
path: root/aufgabe3/main.c
diff options
context:
space:
mode:
authorMichael Stapelberg <michael@stapelberg.de>2010-12-06 14:47:57 +0100
committerMichael Stapelberg <michael@stapelberg.de>2010-12-06 14:47:57 +0100
commit9af2bea582c083653c08abb965eba6f63de997fa (patch)
treeac553217ec7cde33b80722b1c1ba42b9e76f021a /aufgabe3/main.c
parent6762515c8e5861aa80675fe6f27be54689651e76 (diff)
downloadprozesskommunikation-9af2bea582c083653c08abb965eba6f63de997fa.tar.gz
prozesskommunikation-9af2bea582c083653c08abb965eba6f63de997fa.tar.bz2
aufgabe2 und aufgabe3 erstellt
Diffstat (limited to 'aufgabe3/main.c')
-rw-r--r--aufgabe3/main.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/aufgabe3/main.c b/aufgabe3/main.c
new file mode 100644
index 0000000..f876f54
--- /dev/null
+++ b/aufgabe3/main.c
@@ -0,0 +1,86 @@
+/*
+ * vim:ts=4:sw=4:expandtab
+ *
+ */
+#include <stdio.h>
+#include <unistd.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include "conv.h"
+#include "log.h"
+#include "monitor.h"
+#include "statistic.h"
+
+typedef void (*funcptr)();
+
+pid_t pconv;
+pid_t plog;
+pid_t pstatistic;
+pid_t pmonitor;
+
+/*
+ * Signalhandler für SIGTERM. Sendet SIGINT an die 4 Prozesse.
+ *
+ */
+void sigterm() {
+ kill(pconv, SIGINT);
+ kill(plog, SIGINT);
+ kill(pstatistic, SIGINT);
+ kill(pmonitor, SIGINT);
+}
+
+/*
+ * fork()t einen neuen Prozess. In diesem Prozess wird die übergebene Funktion
+ * work() aufgerufen, außerdem wird der Signalhandler cleanup() installiert für
+ * das Signal SIGINT.
+ *
+ */
+pid_t fork_child(funcptr work, funcptr cleanup) {
+ pid_t pid = fork();
+ if (pid == 0) {
+ /* child process */
+ signal(SIGINT, cleanup);
+
+ work();
+
+ /* Die Prozesse laufen endlos. Daher beenden wir mit Fehlercode, sofern
+ * die Prozessfunktion doch zurückkehrt. */
+ exit(EXIT_FAILURE);
+ }
+
+ if (pid == -1) {
+ /* error */
+ perror("fork()");
+ exit(EXIT_FAILURE);
+ }
+
+ /* parent process */
+ printf("forked\n");
+ return pid;
+}
+
+/*
+ * main-Funktion. Startet die 4 Prozesse, registriert einen signal-handler für
+ * SIGTERM (dieser sendet SIGINT an die 4 Prozesse) und wartet schließlich auf
+ * das Ende der Prozesse.
+ *
+ */
+int main() {
+ pconv = fork_child(conv, conv_cleanup);
+ plog = fork_child(log, log_cleanup);
+ pstatistic = fork_child(statistic, statistic_cleanup);
+ pmonitor = fork_child(monitor, monitor_cleanup);
+
+ signal(SIGTERM, sigterm);
+
+ int status;
+ waitpid(pconv, &status, 0);
+ waitpid(plog, &status, 0);
+ waitpid(pstatistic, &status, 0);
+ waitpid(pmonitor, &status, 0);
+
+ return 0;
+}