当前位置: 技术问答>linux和unix
一个消息传递的疑问
来源: 互联网 发布时间:2015-12-22
本文导语: 请帮忙看一下下面这段程序,我fork了两个子进程,子进程p1传递消息,p2接受并打印消息。 但运行时p2却接收不到消息,求助。。 #include #include #include #include #include #include #define KEY 500 struct msgbuf{ long mty...
请帮忙看一下下面这段程序,我fork了两个子进程,子进程p1传递消息,p2接受并打印消息。
但运行时p2却接收不到消息,求助。。
#include
#include
#include
#include
#include
#include
#define KEY 500
struct msgbuf{
long mtype;
char mtext[10];
};
int msgqid;
int main()
{
int p1, p2;
msgqid = msgget(KEY, IPC_CREAT | 0777);
while(-1 == (p1 = fork()));
if(!p1){
int i/*, msgqid*/;
struct msgbuf msg;
/* msgqid = msgget(KEY, IPC_CREAT | 0666);*/
for(i = 10; i >= 1; i--){
msg.mtype = i;
sprintf(msg.mtext, "%ldn", i);
msgsnd(msgqid, &msg, sizeof(struct msgbuf), 0);
printf("client sended message!n");
sleep(1);
}
exit(0);
}
while(-1 == (p2 = fork()));
if(!p2){
int i/*, msgqid*/;
struct msgbuf msg;
/* msgqid = msgget(KEY, IPC_CREAT | IPC_EXCL | 0666);*/
do{
if(msgrcv(msgqid, &msg, sizeof(struct msgbuf), 0, 0) > 0){
printf("serve received message!n");
printf("mtype = %ld, mtext = %sn", msg.mtype, msg.mtext);
}
sleep(1);
}while(msg.mtype != 1);
msgctl(msgqid, IPC_RMID, NULL);
exit(0);
}
wait(NULL);
wait(NULL);
return 0;
}
但运行时p2却接收不到消息,求助。。
#include
#include
#include
#include
#include
#include
#define KEY 500
struct msgbuf{
long mtype;
char mtext[10];
};
int msgqid;
int main()
{
int p1, p2;
msgqid = msgget(KEY, IPC_CREAT | 0777);
while(-1 == (p1 = fork()));
if(!p1){
int i/*, msgqid*/;
struct msgbuf msg;
/* msgqid = msgget(KEY, IPC_CREAT | 0666);*/
for(i = 10; i >= 1; i--){
msg.mtype = i;
sprintf(msg.mtext, "%ldn", i);
msgsnd(msgqid, &msg, sizeof(struct msgbuf), 0);
printf("client sended message!n");
sleep(1);
}
exit(0);
}
while(-1 == (p2 = fork()));
if(!p2){
int i/*, msgqid*/;
struct msgbuf msg;
/* msgqid = msgget(KEY, IPC_CREAT | IPC_EXCL | 0666);*/
do{
if(msgrcv(msgqid, &msg, sizeof(struct msgbuf), 0, 0) > 0){
printf("serve received message!n");
printf("mtype = %ld, mtext = %sn", msg.mtype, msg.mtext);
}
sleep(1);
}while(msg.mtype != 1);
msgctl(msgqid, IPC_RMID, NULL);
exit(0);
}
wait(NULL);
wait(NULL);
return 0;
}
|
友情UP
|
可以接收.
[root@localhost a]# ./pass_msg
client sended message!
serve received message!
mtype = 10, mtext = 10
client sended message!
serve received message!
mtype = 9, mtext = 9
client sended message!
serve received message!
mtype = 8, mtext = 8
client sended message!
serve received message!
mtype = 7, mtext = 7
client sended message!
serve received message!
mtype = 6, mtext = 6
client sended message!
serve received message!
mtype = 5, mtext = 5
client sended message!
serve received message!
mtype = 4, mtext = 4
client sended message!
serve received message!
mtype = 3, mtext = 3
client sended message!
serve received message!
mtype = 2, mtext = 2
client sended message!
serve received message!
mtype = 1, mtext = 1
[root@localhost a]# ./pass_msg
client sended message!
serve received message!
mtype = 10, mtext = 10
client sended message!
serve received message!
mtype = 9, mtext = 9
client sended message!
serve received message!
mtype = 8, mtext = 8
client sended message!
serve received message!
mtype = 7, mtext = 7
client sended message!
serve received message!
mtype = 6, mtext = 6
client sended message!
serve received message!
mtype = 5, mtext = 5
client sended message!
serve received message!
mtype = 4, mtext = 4
client sended message!
serve received message!
mtype = 3, mtext = 3
client sended message!
serve received message!
mtype = 2, mtext = 2
client sended message!
serve received message!
mtype = 1, mtext = 1
|
up
|
在redhat9上测试过,没问题。