#include <sys/types.h>
#include <sys/msg.h>
#include <unistd.h>
#include<errno.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/ipc.h>
#include<signal.h>
struct msg_buf
{
long mtype;
char mtext[255];
};
main(void)
{
key_t key;
int msgid;
struct msg_buf msgbuf;
key = ftok("./msg", 'a'); //use ftok to get the key
if((msgid = msgget(key, IPC_CREAT | 0666)) < 0)
{
printf("msgget error\n");
exit(1);
}
if(msgrcv(msgid, &msgbuf, sizeof(msgbuf), 0, IPC_NOWAIT) < 0) //get address of struct, &msgbuf
{
printf("msgrcv error:%s\n", strerror(errno));
exit(1);
}
printf("reveive %s from queue\n", msgbuf.mtext);
kill(msgbuf.mtype, SIGBUS); //seng message to another process
sleep(5);
if(msgctl(msgid, IPC_RMID, 0) < 0)
{
printf("msgctl error\n");
exit(1);
}
printf("queue is cancle!\n");
}
|