免费注册 查看新帖 |

Chinaunix

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

关于使用消息队列收发消息的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-07-20 18:03 |只看该作者 |倒序浏览
最近刚开始使用消息队列进行事件的处理,但是在编译后发现程序可以正确的发送消息,但却不能接受到消息,这是什么问题?哪位高手能解决下。具体程序代码如下:
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct msgbuf{
                      int mtype;
                      char mtext[1];
      }msgsbuf;
struct msgmbuf{
                    int mtype;
                    char mtext[10];
            }msgrbuf;
int msgid;
void catch_child(int sig_num)
{
    int i=0;
    char a;
    extern char TxDataBuffer[2034];//文件发送缓冲区TxDataBuffer
    FILE *ifp,*ofp;
    ifp=fopen("p.c","r");
    while(fscanf(ifp,"%c",&a)==1)
             {
        TxDataBuffer=a;
        i++;
        if(i==2034) break;
        }
    fclose(ifp);
    printf("i=%d\n",i);
    printf("%s\n",TxDataBuffer);
    int gflags,rflags;
        int reval;
        extern int msgid;

            msgsbuf.mtype=10;
            msgsbuf.mtext[0]='a';
             reval=msgsnd(msgid,&msgsbuf,sizeof(msgsbuf.mtext),0);//发送消息
            if (reval==-1)
            {
                    printf("message send error\n");
            }
        printf("reval=%d\n",reval);
}
char TxDataBuffer[2034];

main()
{
        key_t key;
             int rflags,gflags,reval;
        extern int msgid;
        char * msgpath="unix/msgqueue";
            key=ftok(msgpath,'a');
            gflags=IPC_CREAT|IPC_EXCL;
            msgid=msgget(key,gflags|00666);//创建一个消息队列
        if (msgid==-1)
            {
                    printf("msg create error\n");
                    return;
            }
        signal(SIGINT, catch_child);
            int child_pid[2];
   
      reval=msgrcv(msgid,&msgrbuf,4,10,0);//读消息队列,没收到消息进程就阻塞
            if (reval==-1)
            printf("read mssage error\n");
        else
            printf("read from msg queue %d bytes\n",reval);
      for (j=0; j<30; j++) {
          printf("%d\n", j);
         
        sleep(1);   
    }

}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP