进程间通信--消息队列

进程1:给进程2发送一个消息,在接收进程2回送的消息

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <time.h>

struct msgbuf
{
        long msg_type;
        int msg_date;
        char msg_text[1024];
};

int main()
{

        int ret;
        int qid;
        key_t key;
        struct msgbuf msg;
        msg.msg_type = 100;
        key = ftok(".",'a');
        if(key == -1)
        {
                perror("happen the standerd error of key");
                exit(1);
        }

        qid = msgget(key,IPC_CREAT|0666);
        if(qid == -1)  
        {
                perror("the create message queue is error");
                exit(1);
        }

        while(1)
        {
                printf("please enter the send message:\n");
                scanf("%s",&msg.msg_text);
                msg.msg_date = system("date|cut -b -4,4-");
                ret = msgsnd(qid,&msg,sizeof(msg.msg_text),msg.msg_type);
                if(ret<0)
                {
                        perror("insert the message is fail");
                        exit(1);
                }

                msg.msg_type = 200;
                msgrcv(qid,&msg,sizeof(msg),msg.msg_type,0);
                if(ret < 0)
                {
                        perror("read the message is fail");
                        exit(1);
                }

                printf("reading the message is :\n%s\n",msg.msg_text);
        }

        msgctl(qid,IPC_RMID,NULL);
        return 0;
}

程序2:先接收进程1的消息后,在给对方回一个消息
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include  <sys/msg.h>

struct msgbuf
{
        int msg_type;
        int msg_date;
        char msg_text[1024];
};

int main()
{
        int ret;
        int qid;
        key_t key;
        struct msgbuf msg;
        msg.msg_type = 100;
        key = ftok(".",'a');
        if(key == -1)
        {
                perror("happening the standerd of error\n");
                exit(1);
        }

        qid = msgget(key,IPC_CREAT|0666);
        if(qid == -1)
        {
                perror("creating the message queue is fail");
                exit(1);
        }
        while(1)
        {
                ret =  msgrcv(qid,&msg,sizeof(msg),msg.msg_type,0);
                if(ret == -1)
                {
                        perror("reading is fail");
                        exit(1);
                }

                printf("the reading message is:\n%s\n",msg.msg_text);

                msg.msg_type == 200;
                printf("please enter the message:\n");
                scanf("%s",&msg.msg_text);

                msg.msg_date == system("date|cut -b -4,4-");
                ret = msgsnd(qid,&msg,sizeof(msg.msg_text),msg.msg_text);
                if(ret == -1)
                {
                        perror("insert the message error");
                        exit(1);
                }
        }
        msgctl(qid,IPC_RMID,NULL);
        return 0;
}


问题是:
      当进程1给进程2发送一个消息后,进程2可以接收到,但是进程2给进程1发送消息时,进程1并没有收到,反而进程2自己接收了自己的消息。结果如下:
进程1:
[root@localhost msg_queue]# ./test2
please enter the send message:
hello
2012年 11月 27日 星期二 23:39:49 CST

进程2:

[root@localhost msg_queue]# ./test3
the reading message is:
hello
please enter the message:
ok
2012年 11月 27日 星期二 23:39:53 CST
the reading message is:
ok
please enter the message:
ok
2012年 11月 27日 星期二 23:40:48 CST
the reading message is:
ok


到底是什么原因呢,进程1接收不到进程2的消息,希望大家解答!!

解决:

进程1.
msg.msg_type = 100;
移到循环里msgsnd之前

进程2.
msg.msg_type = 100;
移到循环里msgrcv之前

msg.msg_type == 200;
改成
msg.msg_type = 200;

http://bbs.youkuaiyun.com/topics/390296930(帖子地址)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值