diff options
| author | Thorsten Töpper <atsutane@freethoughts.de> | 2010-12-31 16:02:19 +0100 |
|---|---|---|
| committer | Thorsten Töpper <atsutane@freethoughts.de> | 2010-12-31 16:02:19 +0100 |
| commit | 43d16388f5ab5ec87f2373f10ae9a648c3464c3b (patch) | |
| tree | 555935dadaa9c96138af5126dcc0a62ba83e1248 /aufgabe3/main.c | |
| parent | 6af0f110bbd879fe4c49c2ebd64715a6e692e7c0 (diff) | |
| download | prozesskommunikation-43d16388f5ab5ec87f2373f10ae9a648c3464c3b.tar.gz prozesskommunikation-43d16388f5ab5ec87f2373f10ae9a648c3464c3b.tar.bz2 | |
Aufgabe3: Erstellung der Message Queue implementiert.
Diffstat (limited to 'aufgabe3/main.c')
| -rw-r--r-- | aufgabe3/main.c | 44 |
1 files changed, 40 insertions, 4 deletions
diff --git a/aufgabe3/main.c b/aufgabe3/main.c index f876f54..ee54c6d 100644 --- a/aufgabe3/main.c +++ b/aufgabe3/main.c @@ -1,6 +1,6 @@ /* * vim:ts=4:sw=4:expandtab - * + * */ #include <stdio.h> #include <unistd.h> @@ -13,6 +13,7 @@ #include "log.h" #include "monitor.h" #include "statistic.h" +#include "queue.h" typedef void (*funcptr)(); @@ -50,7 +51,7 @@ pid_t fork_child(funcptr work, funcptr cleanup) { * die Prozessfunktion doch zurückkehrt. */ exit(EXIT_FAILURE); } - + if (pid == -1) { /* error */ perror("fork()"); @@ -62,13 +63,48 @@ pid_t fork_child(funcptr work, funcptr cleanup) { return pid; } +mqd_t mqueue_init() { + mqd_t msgqueue_id; + + /* + * Festlegung der Attribute. + */ + struct *mq_attr attributes = calloc(1, sizeof(mq_attr)); + if (attributes == NULL) { + sprintf(stderr, "main.c mq_init: calloc failed\n"); + return -1; + } + + attributes->mq_flags = 0; /* Queue blockiert bei mq_send()/mq_receive() */ + attributes->mq_maxmsg = 50; /* maximal 50 Messages in der Queue */ + attributes->mq_msgsize = MAX_QMSG_SIZE; /* Maximale Länge einer Nachricht */ + attributes->mq_curmsgs = 0; /* Anzahl der Messages momentan in der Queue */ + + /* + * Erstellung der Message Queue. + * O_CREAT zur Erstellung, + * O_EXCL zur Fehlererzeugung, falls es bereits eine Queue mit diesem + * Namen gibt. + * 660 Schreib- und Leserechte für Prozessnutzer und -gruppe. + */ + msgqueue_id = mq_open(MQ_ID, O_CREAT | O_EXCL, 660, attributes); + if (msgqueue_id == -1) + sprintf(stderr, "main.c mq_init: mq_open failed\n"); + + return msgqueue_id; +} + /* * 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() { + + mqd_t mq_id = mqueue_init(); + if (mq_id == -1) + return EXIT_FAILURE; + pconv = fork_child(conv, conv_cleanup); plog = fork_child(log, log_cleanup); pstatistic = fork_child(statistic, statistic_cleanup); @@ -82,5 +118,5 @@ int main() { waitpid(pstatistic, &status, 0); waitpid(pmonitor, &status, 0); - return 0; + return EXIT_SUCCESS; } |
