From 9af2bea582c083653c08abb965eba6f63de997fa Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Mon, 6 Dec 2010 14:47:57 +0100 Subject: aufgabe2 und aufgabe3 erstellt --- aufgabe2/Makefile | 10 ++++++ aufgabe2/conv.c | 18 +++++++++++ aufgabe2/conv.h | 7 +++++ aufgabe2/log.c | 18 +++++++++++ aufgabe2/log.h | 7 +++++ aufgabe2/main.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++ aufgabe2/monitor.c | 18 +++++++++++ aufgabe2/monitor.h | 7 +++++ aufgabe2/statistic.c | 18 +++++++++++ aufgabe2/statistic.h | 7 +++++ aufgabe3/Makefile | 10 ++++++ aufgabe3/conv.c | 18 +++++++++++ aufgabe3/conv.h | 7 +++++ aufgabe3/log.c | 18 +++++++++++ aufgabe3/log.h | 7 +++++ aufgabe3/main.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++ aufgabe3/monitor.c | 18 +++++++++++ aufgabe3/monitor.h | 7 +++++ aufgabe3/statistic.c | 18 +++++++++++ aufgabe3/statistic.h | 7 +++++ 20 files changed, 392 insertions(+) create mode 100644 aufgabe2/Makefile create mode 100644 aufgabe2/conv.c create mode 100644 aufgabe2/conv.h create mode 100644 aufgabe2/log.c create mode 100644 aufgabe2/log.h create mode 100644 aufgabe2/main.c create mode 100644 aufgabe2/monitor.c create mode 100644 aufgabe2/monitor.h create mode 100644 aufgabe2/statistic.c create mode 100644 aufgabe2/statistic.h create mode 100644 aufgabe3/Makefile create mode 100644 aufgabe3/conv.c create mode 100644 aufgabe3/conv.h create mode 100644 aufgabe3/log.c create mode 100644 aufgabe3/log.h create mode 100644 aufgabe3/main.c create mode 100644 aufgabe3/monitor.c create mode 100644 aufgabe3/monitor.h create mode 100644 aufgabe3/statistic.c create mode 100644 aufgabe3/statistic.h diff --git a/aufgabe2/Makefile b/aufgabe2/Makefile new file mode 100644 index 0000000..d08af51 --- /dev/null +++ b/aufgabe2/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/aufgabe2/conv.c b/aufgabe2/conv.c new file mode 100644 index 0000000..efa8200 --- /dev/null +++ b/aufgabe2/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/aufgabe2/conv.h b/aufgabe2/conv.h new file mode 100644 index 0000000..f593aab --- /dev/null +++ b/aufgabe2/conv.h @@ -0,0 +1,7 @@ +#ifndef _CONV_H +#define _CONV_H + +void conv(); +void conv_cleanup(); + +#endif diff --git a/aufgabe2/log.c b/aufgabe2/log.c new file mode 100644 index 0000000..405cdbd --- /dev/null +++ b/aufgabe2/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/aufgabe2/log.h b/aufgabe2/log.h new file mode 100644 index 0000000..e6b171f --- /dev/null +++ b/aufgabe2/log.h @@ -0,0 +1,7 @@ +#ifndef _LOG_H +#define _LOG_H + +void log(); +void log_cleanup(); + +#endif diff --git a/aufgabe2/main.c b/aufgabe2/main.c new file mode 100644 index 0000000..f876f54 --- /dev/null +++ b/aufgabe2/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/aufgabe2/monitor.c b/aufgabe2/monitor.c new file mode 100644 index 0000000..b631e59 --- /dev/null +++ b/aufgabe2/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/aufgabe2/monitor.h b/aufgabe2/monitor.h new file mode 100644 index 0000000..c70b426 --- /dev/null +++ b/aufgabe2/monitor.h @@ -0,0 +1,7 @@ +#ifndef _MONITOR_H +#define _MONITOR_H + +void monitor(); +void monitor_cleanup(); + +#endif diff --git a/aufgabe2/statistic.c b/aufgabe2/statistic.c new file mode 100644 index 0000000..b83776a --- /dev/null +++ b/aufgabe2/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/aufgabe2/statistic.h b/aufgabe2/statistic.h new file mode 100644 index 0000000..50b82f4 --- /dev/null +++ b/aufgabe2/statistic.h @@ -0,0 +1,7 @@ +#ifndef _STATISTIC_H +#define _STATISTIC_H + +void statistic(); +void statistic_cleanup(); + +#endif diff --git a/aufgabe3/Makefile b/aufgabe3/Makefile new file mode 100644 index 0000000..d08af51 --- /dev/null +++ b/aufgabe3/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/aufgabe3/conv.c b/aufgabe3/conv.c new file mode 100644 index 0000000..efa8200 --- /dev/null +++ b/aufgabe3/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/aufgabe3/conv.h b/aufgabe3/conv.h new file mode 100644 index 0000000..f593aab --- /dev/null +++ b/aufgabe3/conv.h @@ -0,0 +1,7 @@ +#ifndef _CONV_H +#define _CONV_H + +void conv(); +void conv_cleanup(); + +#endif diff --git a/aufgabe3/log.c b/aufgabe3/log.c new file mode 100644 index 0000000..405cdbd --- /dev/null +++ b/aufgabe3/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/aufgabe3/log.h b/aufgabe3/log.h new file mode 100644 index 0000000..e6b171f --- /dev/null +++ b/aufgabe3/log.h @@ -0,0 +1,7 @@ +#ifndef _LOG_H +#define _LOG_H + +void log(); +void log_cleanup(); + +#endif 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 +#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/aufgabe3/monitor.c b/aufgabe3/monitor.c new file mode 100644 index 0000000..b631e59 --- /dev/null +++ b/aufgabe3/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/aufgabe3/monitor.h b/aufgabe3/monitor.h new file mode 100644 index 0000000..c70b426 --- /dev/null +++ b/aufgabe3/monitor.h @@ -0,0 +1,7 @@ +#ifndef _MONITOR_H +#define _MONITOR_H + +void monitor(); +void monitor_cleanup(); + +#endif diff --git a/aufgabe3/statistic.c b/aufgabe3/statistic.c new file mode 100644 index 0000000..b83776a --- /dev/null +++ b/aufgabe3/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/aufgabe3/statistic.h b/aufgabe3/statistic.h new file mode 100644 index 0000000..50b82f4 --- /dev/null +++ b/aufgabe3/statistic.h @@ -0,0 +1,7 @@ +#ifndef _STATISTIC_H +#define _STATISTIC_H + +void statistic(); +void statistic_cleanup(); + +#endif -- cgit v1.2.3-70-g09d2