免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1262 | 回复: 1
打印 上一主题 下一主题

[Linux] 关于linux队列的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-10-07 11:27 |只看该作者 |倒序浏览
下面程序为发送端代码
#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[30];    /* 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[30];    /* 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,接收端也不接受任何数据。求大神解答!

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
2 [报告]
发表于 2014-10-09 11:53 |只看该作者
excl如果发现有队列就返回失败了,压根没打开。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP