大雄_重庆 发表于 2014-10-07 11:27

关于linux队列的问题

下面程序为发送端代码
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
struct msgbuf
{
long mtype;       /* message type, must be > 0 */
char mtext;    /* message data */
};

int main()
{
int i;
key_t key;
int msgid;
struct msgbuf msg;
key=ftok(".",100);
if(key==-1)
printf("ftok error!\n");
msgid=msgget(key,IPC_CREAT|IPC_EXCL|0666);
if(msgid==-1)
printf("msgget error!\n");
for(i=0;i<3;i++)
{
bzero(&msg,sizeof(msg));
msg.mtype=1;
sprintf(msg.mtext,"hello,I am message %d",i);
msgsnd(msgid,&msg,sizeof(msg.mtext),0);
}
printf("send completed!\n");
}
下面程序为接收端代码
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
struct msgbuf
{
long mtype;       /* message type, must be > 0 */
char mtext;    /* message data */
};

int main()
{
int i;
key_t key;
int msgid;
struct msgbuf msg;
key=ftok(".",100);
if(key==-1)
printf("ftok error!\n");
msgid=msgget(key,0);
if(msgid==-1)
printf("msgget error!\n");

while(1)
{
bzero(&msg,sizeof(msg));
msg.mtype=1;
msgrcv(msgid,&msg,sizeof(msg.mtext),1,0);
printf("%s\n",msg.mtext);
sleep(1);

}

printf("receive completed!\n");
}
我的接收端始终在循环的接收数据,当发送端msgget参数为IPC_CREAT时,为什么接收端可以循环的显示发送的数据。当msgget为IPC_CREAT|EXCL时,我利用发送端多次发送数据,查看ipcs始终为0,接收端也不接受任何数据。求大神解答!

linux_c_py_php 发表于 2014-10-09 11:53

excl如果发现有队列就返回失败了,压根没打开。
页: [1]
查看完整版本: 关于linux队列的问题