diff options
author | Tobias Klauser <tklauser@distanz.ch> | 2008-01-27 11:37:44 +0100 |
---|---|---|
committer | Tobias Klauser <tklauser@xenon.tklauser.home> | 2008-01-27 11:37:44 +0100 |
commit | 7e0f021a9aec35fd8e6725e87e3313b101d26f5e (patch) | |
tree | b1cacc4b24393f517aeb4610e9e1021f954307a8 /reference/C/CONTRIB/OR_USING_C/11.3.c |
Initial import (2.0.2-6)2.0.2-6
Diffstat (limited to 'reference/C/CONTRIB/OR_USING_C/11.3.c')
-rw-r--r-- | reference/C/CONTRIB/OR_USING_C/11.3.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/reference/C/CONTRIB/OR_USING_C/11.3.c b/reference/C/CONTRIB/OR_USING_C/11.3.c new file mode 100644 index 0000000..25d6e05 --- /dev/null +++ b/reference/C/CONTRIB/OR_USING_C/11.3.c @@ -0,0 +1,63 @@ +#include <sys/types.h> +#include <sys/ipc.h> +#include <sys/msg.h> +#include <stdio.h> + +#define MSGSZ 128 + +#ifdef NOT_USED +/* + * Declare the message structure. + */ +struct msgbuf { + long mtype; + char mtext[MSGSZ]; +}; +#endif /* NOT_USED */ + +main() +{ + int msqid; + key_t key; + struct msgbuf sbuf, rbuf; + + /* + * Create a message queue with "name" 1234. + */ + key = 1234; + + /* + * We want to let everyone read and + * write on this message queue, hence + * we use 0666 as the permissions. + */ + if ((msqid = msgget(key, IPC_CREAT | 0666)) < 0) { + perror("msgget"); + exit(1); + } + + /* + * Receive a message. + */ + if (msgrcv(msqid, &rbuf, MSGSZ, 0, 0) < 0) { + perror("msgrcv"); + exit(1); + } + + /* + * We send a message of type 2. + */ + sbuf.mtype = 2; + sprintf(sbuf.mtext, "I received your message."); + + /* + * Send an answer. + */ + if (msgsnd(msqid, &sbuf, strlen(sbuf.mtext) + 1, 0) < 0) { + perror("msgsnd"); + exit(1); + } + + exit(0); +} + |