群立 2015-11-02 12:39 采纳率: 0%
浏览 2589

Linux消息队列使用时,msgsnd()一直不能通过,大家帮忙看看!

#include
#include
#include
#include
#include
#include
#include
#include

#define QUEUE1 100
#define QUEUE2 101

/*

  • redefine struct msg_buf */ struct msgmbuf{ //msg type long mtype; //the field used to store URL char mtext[256]; };

int main()
{
/*
* create mesage queue!!!
*/
key_t key;
//create msg_queue
int msg_flags;
//msg is
int msg_id1;
msg_flags = IPC_CREAT|IPC_EXCL;
msg_id1 = msgget((key_t)1111, msg_flags|0x0666);
if(-1 == msg_id1){
printf("create msg_queue failed");
return -1;
}

/*
 * send mesage
 */
int msg_sflags;
struct msgmbuf msg_mbuf;
msg_sflags = IPC_NOWAIT;
msg_mbuf.mtype = QUEUE1;
char *url1 = "www.baidu.com";
strcpy(msg_mbuf.mtext, url1);
printf("%s\n", msg_mbuf.mtext);
printf("%ld\n", msg_mbuf.mtype);

int ret = msgsnd(msg_id1, &msg_mbuf, 256, msg_sflags);
if(-1 == ret){
    printf("send msg failed,errno = %d,%s\n",errno,strerrno(errno));
    msgctl(msg_id1, IPC_RMID, NULL);
    exit(1);
}


/*
 * receive mesage
 */
int msg_rflags;
msg_rflags = IPC_NOWAIT|MSG_NOERROR;
struct msgmbuf msg_rev;

ret = msgrcv(msg_id1, &msg_rev, sizeof(struct msgmbuf), QUEUE1, msg_rflags);
if(-1 == ret){
    printf("receive msg failed\n");
    msgctl(msg_id1, IPC_RMID, NULL);
    return -1;
}else{
    printf("mtext: %s\n", msg_rev.mtext);
    printf("mtype: %ld\n", msg_rev.mtype);
}

msgctl(msg_id1, IPC_RMID, NULL);

return 0;

}

  • 写回答

1条回答 默认 最新

  • 群立 2015-11-02 13:16
    关注

    求各路大神不吝指点。。。

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器