免费注册 查看新帖 |

Chinaunix

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

请教,消息队列中的消息偶为什么读不全 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-10-16 09:51 |只看该作者 |倒序浏览
code]
struct mymsgbuf{
                long mtype;/*Message type*/
                int request;/*Work request number*/
                float salary;/*Employee's salary*/
        }msg,rmsg;
msg.mtype=1;
msg.request=1;
msg.salary=55.00;
已将此消息成功送到消息中:
[root@lx linux_c]# ipcs

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages
0x6d0690dc 262152     root       660        8            1
定义的读函数:
int read_message(int qid,long type,struct mymsgbuf* qbuf)
{
        int result,length;
/*The length is essentially the size of the structure minus sizeof(mtype)*/
        length=sizeof(struct mymsgbuf)-sizeof(long);
        printf("%d\n",length);
        if((result=msgrcv(qid,qbuf,length,type,0))==-1){
                return(-1);
        }
        return(result);
}
在main中:
if((length=read_message(qid,0,&rmsg))==-1){
                        perror("send_message";
                        exit(1);
                }

                printf("mtype:%drequest:%d,salary:%ld",rmsg.mtype,rmsg.request,r
msg.salary);
输出结果:
mtype:1request:1,salary:0

不知为什么slary等于0呢

大侠帮忙看看吧,
//bow
[/code][/quote]

论坛徽章:
0
2 [报告]
发表于 2004-10-16 10:53 |只看该作者

请教,消息队列中的消息偶为什么读不全

length=sizeof(struct mymsgbuf)-sizeof(long);
=>;读取的长度中已经减去long的长度了,所以salary没有读到

论坛徽章:
0
3 [报告]
发表于 2004-10-16 15:41 |只看该作者

请教,消息队列中的消息偶为什么读不全

可是我减的是 long mtype不是 float salary啊
不过,还是要按大侠的方法试试

先谢谢啊

论坛徽章:
0
4 [报告]
发表于 2004-10-16 15:43 |只看该作者

请教,消息队列中的消息偶为什么读不全

刚试了,减了也无济于事,salary还是0
这是咋回事

论坛徽章:
0
5 [报告]
发表于 2004-10-16 20:27 |只看该作者

请教,消息队列中的消息偶为什么读不全

大侠们啊,快帮我解决一下吧,

我都郁闷死了。。。。。

论坛徽章:
0
6 [报告]
发表于 2004-10-16 20:41 |只看该作者

请教,消息队列中的消息偶为什么读不全

1。length=sizeof(struct mymsgbuf);
2。确认发送字节数是正确的,通过ipcs -q 命令。
3。再不行贴出完整代码让大家看看。注意用上方的code 属性标注,方便别人阅读。

论坛徽章:
0
7 [报告]
发表于 2004-10-17 22:53 |只看该作者

请教,消息队列中的消息偶为什么读不全

salary:%ld

当然是输出0了。基本语法都错了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP