日期:2014-05-16  浏览次数:20677 次

进程间通信:消息队列问题:进程1接收不到进程2的消息
进程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);