专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > C语言

unix消息队列mq_receive有关问题

发布时间:2011-06-28 10:26:07 文章来源:www.iduyao.cn 采编人员:星星草
unix消息队列mq_receive问题
遇到一个问题,父子进程之间通过消息队列通信,但是子进程始终收不到消息队列的消息,我用pipe测试过是可以收到的,我贴出代码和运行结果请高手帮忙看一下问题所在。。。。
#include "stdio.h"
#include "signal.h"
#include <unistd.h>
#include "stdlib.h"
#include <mqueue.h>
#include <errno.h>

#define FILE_MODE   (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
void childFun(int sig)
{
        printf("child, received the  signal SIGUSR1\n");
        struct mq_attr  attr;
        char *buf = NULL;
        unsigned int   prio;
        printf("in child Fun+++++++\n");
        mqd_t mqd_22;
        mqd_22 == mq_open("/mq1proces" ,O_RDWR);
        if(-1 == mqd_22)
        {
            printf("open failed,errno:%d\n" , errno);
        }

        printf("mqd_22:%d\n",mqd_22);
        buf =(char *) malloc(100);
        if(buf == NULL)
        {
            printf("!!!!!!!!!!!!!!!!!!!!11buf is NULL \n");
        }
        int rel_rcv = mq_receive(mqd_22,buf,20,NULL);
        printf("child Fun,mq_receive buf:%s,and rel_rcv:%d, errno:%d\n" , buf,rel_rcv,errno);

}

int main()
{
    int iPid;
    printf("father,pid:%d\n" , getpid());
    iPid = fork();
    if(iPid<0)
    {
        printf("error\n");
        exit(-1);
    }
    else if(iPid ==0)
    {
        signal(SIGUSR1,childFun);
        for (;;)
        {
            sleep(10);
        }

    }
    else
    {
        sleep(3);
        mqd_t   mqd_11;
        mqd_11 = mq_open("/mq1proces" , O_RDWR | O_CREAT |O_NONBLOCK ,0777,NULL);
        if(-1 == mqd_11)
        {
                printf("create mq1process failed\n");
        }
        const char *ptr = " my name wzw";
        int rel_send = mq_send(mqd_11, ptr,20,0);
        printf("father send and rel_send:%d,ptr:%s,mqd_11:%d\n",rel_send,ptr,mqd_11);
        printf("father, the child pid:will kill sig%d\n" , iPid);
        kill(iPid,SIGUSR1);
        pause();
    }

}


以上是源代码.下面是运行结果。 返回的错误9:EBADF  The descriptor specified in mqdes was invalid.
请帮我分析下。。。多谢了
------解决思路----------------------
路过帮结帖并接分。
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: