免费注册 查看新帖 |

Chinaunix

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

[Linux] mqnotify函数 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-07-07 20:35 |只看该作者 |倒序浏览
1 #include <mqueue.h>
  2 #include <unistd.h>
  3 #include <stdio.h>
  4 #include <stdlib.h>
  5 #include <pthread.h>
  6 #include <errno.h>
  7
  8 mqd_t mqd;
  9 struct mq_attr attr;
10 struct sigevent sigev;
11
12 static void notify_thread(union sigval);
13
14 int main(int argc,char **argv)
15 {
16     if(argc!=2){
17         printf("usage : mqnotifyread1 <name>";
18         return -1;
19     }
20
21     mqd=mq_open(argv[1],O_RDONLY|O_NONBLOCK);
22     mq_getattr(mqd,&attr);
23

24     sigev.sigev_notify=SIGEV_THREAD;
25     sigev.sigev_value.sival_ptr=NULL;
26     sigev.sigev_notify_function=notify_thread;
27     sigev.sigev_notify_attributes=NULL;
28     mq_notify(mqd,&sigev);
29
30     for(;
31         pause();
32
33     return 0;
34
35 }
36 static void notify_thread(union sigval arg)
37 {
38     ssize_t n;
39     void *buff;
40
41     printf("notify_thread started \n";
42     buff=malloc(attr.mq_msgsize);
43     mq_notify(mqd,&sigev);
44
45     while((n=mq_receive(mqd,buff,attr.mq_msgsize,NULL))>=0)
46     {

47         printf("mq_receive error\n";
48     }
49     if(errno!=EAGAIN){
50         printf("mq_receive error \n";
51     }
52     free(buff);
53     pthread_exit(NULL);
54
55
56
57 }
这段代码是Unix网络编程卷二中的77 的,

在一个窗口
[root@localhost 5]# ./mqsend /chao 23 56
在另一个窗口
[root@localhost 5]# ./mqnotifyread1 /chao
notify_thread started
mq_receive error
为什么显示错误,应该是打印你接收到的字节数呀

论坛徽章:
0
2 [报告]
发表于 2013-07-10 11:19 |只看该作者
45     while((n=mq_receive(mqd,buff,attr.mq_msgsize,NULL))>=0)
46     {

47         printf("mq_receive error\n";
48     }

n为接收到size,收到消息时条件成立,为什么打印error? 自己的输出描述与逻辑不符吧?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP