进程间通信:消息队列问题:进程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);
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;
[解决办法]
你的进程1本来就可以自己接受自己发的消息了,还有你的进程2也本来就可以自己接受自己发的消息了!!
你可以单独运行进程1和进程2 试试,都是可以自己接受自己的消息的!!
这里只不过你的进程2 里的while里是先等待接受消息的,而不是发送消息的。当你运行进程2时,他会一直阻塞在那里等待消息的到来。
但你的进程1是先发送的,后才接受的。你的进程1,在后来的发送过程中,是自己已经可以接受消息了。
所以你的进程2当然在后来的(除了第一次外)当然可以接收到自己的消息了、、、、
[解决办法]
楼主发送和接收都使用同一个msg结构体,
比如在进程2里,
发送以后msg.msg_type就是200了,然后循环回去接收200的消息,
自然接收到的是自己刚刚发的消息.
/* 第二次执行这里会接收200类型的消息,也就是刚发送的200消息-
[解决办法]
*/
ret = msgrcv(qid,&msg,sizeof(msg),msg.msg_type,0); /*
[解决办法]
*/
/*
[解决办法]
...
[解决办法]
[解决办法]
*/
msg.msg_type = 200; /* 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);