c中如何通过匿名管道发送消息
How to send message through anonymous pipe in c
我尝试使用匿名管道向 parent 发送两条消息。我知道我需要将 parent 置于等待状态,让 children 写入第二条消息,然后从管道中读取它。我尝试使用 wait(NULL)
让 parent 等待 children 写入管道,但它似乎不起作用。有人可以帮帮我吗?先谢谢了。
这是我目前的代码:
#include <unistd.h>
#include <stdio.h>
#include <string.h>
# include <wait.h>
#define MaxLen 80
#define PipeStdIn 0
#define PipeStdOut 1
int main( ) {
int ret, myPipes[2];
char buff[MaxLen + 1];
if( pipe( myPipes ) == 0 ){
if( fork( ) == 0 ) {
char *reply = {"child sends first message.\n"};
write( myPipes[PipeStdOut], reply, strlen(reply) + 1);
char *reply2 = {"child sends second message.\n"};
write( myPipes[PipeStdOut], reply2, strlen(reply2) + 1);
}
else {
read(myPipes[PipeStdIn], buff, MaxLen);
printf("Parent receives message: %s", buff);
wait(NULL);
read(myPipes[PipeStdIn], buff, MaxLen);
printf("Parent receives 2nd message : %s", buff);
}
}
close( myPipes[PipeStdIn] );
close( myPipes[PipeStdOut] );
return 0;
}
这是我编译时得到的:
aaa@ubuntu:~/Desktop/fork$ ./fork
Parent receives message: child sends first message.
您必须关闭每个进程中不使用的管道末端:
int main( ) {
int ret, myPipes[2];
char buff[MaxLen + 1];
if( pipe( myPipes ) == 0 ){
if( fork( ) == 0 ) {
close (myPipes[PipeStdIn]);
char *reply = {"child sends first message.\n"};
write( myPipes[PipeStdOut], reply, strlen(reply) + 1);
char *reply2 = {"child sends second message.\n"};
write( myPipes[PipeStdOut], reply2, strlen(reply2) + 1);
}
else {
close (myPipes[StdOut]);
read(myPipes[PipeStdIn], buff, MaxLen);
printf("Parent receives message: %s", buff);
read(myPipes[PipeStdIn], buff, MaxLen);
printf("Parent receives 2nd message : %s", buff);
wait(NULL);
}
}
看,它现在可以工作了,您的 close() 函数在错误的行中。您应该在读取或写入之前关闭。
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <wait.h>
#define MaxLen 80
#define PipeStdIn 0
#define PipeStdOut 1
int main( ) {
int ret, myPipes[2];
char buff[MaxLen + 1];
if( pipe( myPipes ) == 0 ){
if( fork( ) == 0 ) {
char *reply = {"child sends first message.\n"};
char *reply2 = {"child sends second message.\n"};
close(myPipes[PipeStdIn]); // CLOSE BEFORE WRITE
write( myPipes[PipeStdOut], reply, strlen(reply) + 1);
write( myPipes[PipeStdOut], reply2, strlen(reply2) + 1);
}
else {
close(myPipes[PipeStdOut]); // CLOSE BEFORE READ
read(myPipes[PipeStdIn], buff, MaxLen);
printf("Parent receives message: %s", buff);
read(myPipes[PipeStdIn], buff, MaxLen);
printf("Parent receives 2nd message : %s", buff);
}
}
return 0;
}
我尝试使用匿名管道向 parent 发送两条消息。我知道我需要将 parent 置于等待状态,让 children 写入第二条消息,然后从管道中读取它。我尝试使用 wait(NULL)
让 parent 等待 children 写入管道,但它似乎不起作用。有人可以帮帮我吗?先谢谢了。
这是我目前的代码:
#include <unistd.h>
#include <stdio.h>
#include <string.h>
# include <wait.h>
#define MaxLen 80
#define PipeStdIn 0
#define PipeStdOut 1
int main( ) {
int ret, myPipes[2];
char buff[MaxLen + 1];
if( pipe( myPipes ) == 0 ){
if( fork( ) == 0 ) {
char *reply = {"child sends first message.\n"};
write( myPipes[PipeStdOut], reply, strlen(reply) + 1);
char *reply2 = {"child sends second message.\n"};
write( myPipes[PipeStdOut], reply2, strlen(reply2) + 1);
}
else {
read(myPipes[PipeStdIn], buff, MaxLen);
printf("Parent receives message: %s", buff);
wait(NULL);
read(myPipes[PipeStdIn], buff, MaxLen);
printf("Parent receives 2nd message : %s", buff);
}
}
close( myPipes[PipeStdIn] );
close( myPipes[PipeStdOut] );
return 0;
}
这是我编译时得到的:
aaa@ubuntu:~/Desktop/fork$ ./fork
Parent receives message: child sends first message.
您必须关闭每个进程中不使用的管道末端:
int main( ) {
int ret, myPipes[2];
char buff[MaxLen + 1];
if( pipe( myPipes ) == 0 ){
if( fork( ) == 0 ) {
close (myPipes[PipeStdIn]);
char *reply = {"child sends first message.\n"};
write( myPipes[PipeStdOut], reply, strlen(reply) + 1);
char *reply2 = {"child sends second message.\n"};
write( myPipes[PipeStdOut], reply2, strlen(reply2) + 1);
}
else {
close (myPipes[StdOut]);
read(myPipes[PipeStdIn], buff, MaxLen);
printf("Parent receives message: %s", buff);
read(myPipes[PipeStdIn], buff, MaxLen);
printf("Parent receives 2nd message : %s", buff);
wait(NULL);
}
}
看,它现在可以工作了,您的 close() 函数在错误的行中。您应该在读取或写入之前关闭。
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <wait.h>
#define MaxLen 80
#define PipeStdIn 0
#define PipeStdOut 1
int main( ) {
int ret, myPipes[2];
char buff[MaxLen + 1];
if( pipe( myPipes ) == 0 ){
if( fork( ) == 0 ) {
char *reply = {"child sends first message.\n"};
char *reply2 = {"child sends second message.\n"};
close(myPipes[PipeStdIn]); // CLOSE BEFORE WRITE
write( myPipes[PipeStdOut], reply, strlen(reply) + 1);
write( myPipes[PipeStdOut], reply2, strlen(reply2) + 1);
}
else {
close(myPipes[PipeStdOut]); // CLOSE BEFORE READ
read(myPipes[PipeStdIn], buff, MaxLen);
printf("Parent receives message: %s", buff);
read(myPipes[PipeStdIn], buff, MaxLen);
printf("Parent receives 2nd message : %s", buff);
}
}
return 0;
}