免费注册 查看新帖 |

Chinaunix

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

[通信方式] linux systemV消息队列msgsnd失败 [复制链接]

论坛徽章:
1
辰龙
日期:2013-11-28 20:23:14
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-11-18 17:48 |只看该作者 |倒序浏览
在linux下使用syetmV消息队列方式来传递数据
现在msgsnd返回-1,但是使用ipcs查看 对应的消息队列messages个数有相应的增加。。
这是为嘛?
实际上 我使用msgrcv函数也能接收到数据,但是同msgsnd类似msgrcv也提示失败。。。

发送方代码如下:
#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;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP