1.读消息的代码:
msgGet.c
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include<stdio.h>
#include<string.h>
struct msgbuf {
long mtype; //消息类型 /* message type, must be > 0 */
char mtext[128]; //消息内容 /* message data */
};
int main()
{
//int msgget(key_t key, int msgflg);
struct msgbuf readBuf;
int msgId = msgget(0x1235,IPC_CREAT|0777);//0777可读可写可执行
if(msgId == -1)//创建消息队列失败
{
printf("get que failuer\n");
}
//int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
//ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,int msgflg);
msgrcv(msgId,&readBuf,sizeof(readBuf.mtext),888,0);//读取消息
printf("read form que:%s\n",readBuf.mtext);
struct msgbuf sendBuf = {988,"thank you for reach"};
msgsnd(msgId,&sendBuf,strlen(sendBuf.mtext),0);//发送消息
return 0;