//发送消息队列
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <string.h>
struct MSG{
long mtype;
char mtext[256];
};
int main(){
key_t key = ftok(".",100);
int msgid = msgget(key,IPC_CREAT|0666);
struct MSG m;
m.mtype=10;
strcpy(m.mtext,"aaaaa");
int s = msgsnd(msgid,&m,sizeof(m.mtext),0);
if(s==-1){
perror("msgsnd");
}
}
//接收消息队列
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/msg.h>
#include <sys/ipc.h>
struct MSG{
long mtype;
char mtext[256];
};
int main(){
key_t key = ftok(".",100);
int msgid = msgget(key,0);
struct MSG m;
int s = msgrcv(msgid,&m,sizeof(m.mtext),10,0);
if(s==-1){
perror("msgsnd");
}
printf("%s\n",m.mtext);
}
unix ipc编程
最新推荐文章于 2014-01-20 00:33:55 发布