群立 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 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作