日期:2014-05-16 浏览次数:20769 次
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
struct msgbuf{
long mtype; /* type of message */
char mtext[1]; /* message text */
};
int main(int argc,char* argv[])
{
char dir[] = "svmq";//源程序目录下的文件
int msgid;
key_t key;
key = ftok(dir,0);
if(key == -1)
{
printf("ftok failed\n");
return -1;
}
msgid = msgget(key,IPC_CREAT|0666);
if(msgid == -1)
{
printf("message queue create failed!\n");
return -1;
}
struct msgbuf msx;
int i = 0;
while(i<5)//send five message
{
printf("send <%d> message!\n",i);
msx.mtype = 8;
msx.mtext[0] = '0'+ i;
if(msgsnd(msgid,&msx,1,0) == -1);
{
printf("msg send failed!%d\n",errno);
}
i++;
}
return 0;
}