From efd23153d6c094c7d60833548621e13f553871e9 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Mon, 6 Dec 2010 14:41:41 +0100 Subject: Grundgerüst erstellt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 10 +++++++ conv.c | 18 +++++++++++++ conv.h | 7 +++++ log.c | 18 +++++++++++++ log.h | 7 +++++ main.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ monitor.c | 18 +++++++++++++ monitor.h | 7 +++++ statistic.c | 18 +++++++++++++ statistic.h | 7 +++++ 10 files changed, 196 insertions(+) create mode 100644 Makefile create mode 100644 conv.c create mode 100644 conv.h create mode 100644 log.c create mode 100644 log.h create mode 100644 main.c create mode 100644 monitor.c create mode 100644 monitor.h create mode 100644 statistic.c create mode 100644 statistic.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d08af51 --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +all: + gcc -Wall -c -o conv.o conv.c + gcc -Wall -c -o log.o log.c + gcc -Wall -c -o statistic.o statistic.c + gcc -Wall -c -o monitor.o monitor.c + gcc -Wall -c -o main.o main.c + gcc -o pk main.o conv.o log.o statistic.o monitor.o + +clean: + rm -f *.o diff --git a/conv.c b/conv.c new file mode 100644 index 0000000..efa8200 --- /dev/null +++ b/conv.c @@ -0,0 +1,18 @@ +/* + * vim:ts=4:sw=4:expandtab + * + */ +#include +#include +#include + +void conv() { + printf("conv started\n"); + for (;;) { + } +} + +void conv_cleanup() { + printf("conv cleanup\n"); + _exit(EXIT_SUCCESS); +} diff --git a/conv.h b/conv.h new file mode 100644 index 0000000..f593aab --- /dev/null +++ b/conv.h @@ -0,0 +1,7 @@ +#ifndef _CONV_H +#define _CONV_H + +void conv(); +void conv_cleanup(); + +#endif diff --git a/log.c b/log.c new file mode 100644 index 0000000..405cdbd --- /dev/null +++ b/log.c @@ -0,0 +1,18 @@ +/* + * vim:ts=4:sw=4:expandtab + * + */ +#include +#include +#include + +void log() { + printf("log started\n"); + for (;;) { + } +} + +void log_cleanup() { + printf("log cleanup\n"); + _exit(EXIT_SUCCESS); +} diff --git a/log.h b/log.h new file mode 100644 index 0000000..e6b171f --- /dev/null +++ b/log.h @@ -0,0 +1,7 @@ +#ifndef _LOG_H +#define _LOG_H + +void log(); +void log_cleanup(); + +#endif diff --git a/main.c b/main.c new file mode 100644 index 0000000..f876f54 --- /dev/null +++ b/main.c @@ -0,0 +1,86 @@ +/* + * vim:ts=4:sw=4:expandtab + * + */ +#include +#include +#include +#include +#include +#include + +#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; +} diff --git a/monitor.c b/monitor.c new file mode 100644 index 0000000..b631e59 --- /dev/null +++ b/monitor.c @@ -0,0 +1,18 @@ +/* + * vim:ts=4:sw=4:expandtab + * + */ +#include +#include +#include + +void monitor() { + printf("monitor started\n"); + for (;;) { + } +} + +void monitor_cleanup() { + printf("monitor cleanup\n"); + _exit(EXIT_SUCCESS); +} diff --git a/monitor.h b/monitor.h new file mode 100644 index 0000000..c70b426 --- /dev/null +++ b/monitor.h @@ -0,0 +1,7 @@ +#ifndef _MONITOR_H +#define _MONITOR_H + +void monitor(); +void monitor_cleanup(); + +#endif diff --git a/statistic.c b/statistic.c new file mode 100644 index 0000000..b83776a --- /dev/null +++ b/statistic.c @@ -0,0 +1,18 @@ +/* + * vim:ts=4:sw=4:expandtab + * + */ +#include +#include +#include + +void statistic() { + printf("statistic started\n"); + for (;;) { + } +} + +void statistic_cleanup() { + printf("statistic cleanup\n"); + _exit(EXIT_SUCCESS); +} diff --git a/statistic.h b/statistic.h new file mode 100644 index 0000000..50b82f4 --- /dev/null +++ b/statistic.h @@ -0,0 +1,7 @@ +#ifndef _STATISTIC_H +#define _STATISTIC_H + +void statistic(); +void statistic_cleanup(); + +#endif -- cgit v1.2.3-70-g09d2