linux 消息队列发送失败,linux systemV消息队列msgsnd失败解决办法

linux systemV消息队列msgsnd失败

在linux下使用syetmV消息队列方式来传递数据

现在msgsnd返回-1,但是使用ipcs查看 对应的消息队列messages个数有相应的增加。。

这是为嘛?

实际上 我使用msgrcv函数也能接收到数据,但是同msgsnd类似msgrcv也提示失败。。。

发送方代码如下:

#include 

#include 

#include 

#include 

#include 

#include 

#include 

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  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;

}

------解决方案--------------------

if(msgsnd(msgid,&msx,1,0) == -1);

第二个参数用的不对吧,API的说明,第二个参数是消息体的大小,也就是说,是msg text的大小,你给的是整个控制结构体的大小

------解决方案--------------------

大哥你坑死我了

if(msgsnd(msgid,&msx,1,0) == -1);

这多个;

我查了一个小时。擦得。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值