|
The contents of this page are licensed under the following license
Operating System
IPC message queues |
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct msgbuf
{
long mtype;
char mtext[100];
};
int main()
{
int msgid;
int foo;
struct msgbuf msg;
/* create message queue and get identifier */
msgid = msgget(0x13, 0600 | IPC_CREAT | IPC_EXCL);
if (!msgid)
{
msgid = msgget(0x13, 0600 | IPC_CREAT);
if (!msgid)
{
perror("Error create message queue");
exit(1);
}
}
msg.mtype = 1;
strcpy(msg.mtext, "Hello!");
/* append a copy of message to the message queue */
foo = msgsnd(msgid, &msg, sizeof(msg.mtext), 0);
if (foo < 0) perror("Error send message");
strcpy(msg.mtext, "");
/* read a message from the message queue */
foo = msgrcv(msgid, &msg, sizeof(msg.mtext), 1, 0);
if (foo < 0) perror("Erorr read message");
printf("Message [type=%d]: %s\n", msg.mtype, msg.mtext);
/* remove the message queue */
msgctl(msgid, IPC_RMID, 0);
}
|
August, 23rd 2006
© Michał Kalewski