我试图了解管道和分叉的工作方式。因此,我编写了一个简单的程序,其中父进程向子进程发送一条消息(工作正常)。但是,如果我尝试通过添加注释的代码从子进程发送回消息,它将停止工作。并且在输出“ Parent send:hello”之后程序的执行停止。
int main() {
int child_to_parent[2];
int parent_to_child[2];
pipe(child_to_parent);
pipe(parent_to_child);
pid_t id = fork();
if (id == 0) {
close(parent_to_child[1]);
close(child_to_parent[0]);
FILE* out = fdopen(child_to_parent[1], "w");
FILE* in = fdopen(parent_to_child[0], "r");
char msg[6];
fscanf(in ,"%s", msg);
printf("Child got: %s\n", msg);
/*
fprintf(out, "hi ");
printf("Child sent: hi\n");
*/
} else {
close(parent_to_child[0]);
close(child_to_parent[1]);
FILE* in = fdopen(child_to_parent[0], "r");
FILE* out = fdopen(parent_to_child[1], "w");
fprintf(out, "hello");
printf("Parent sent: hello\n");
/*
char msg[3];
fscanf(in, "%s", msg);
printf("Parent got: %s\n", msg);
*/
}
}
我不知道为什么。最让我感到困惑的是为什么修改代码后子进程甚至无法收到消息。谁能告诉我哪里出了问题,或将我引导向正确的方向?
答案 0 :(得分:0)
第一种使用读/写来交换任意数量消息的解决方案
在这里,我用\ n指示要读取的每个缓冲区的结尾:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
void rd(int fd, int child)
{
char c;
int first = 1;
do {
if (!read(fd, &c, 1))
break;
if (first) {
printf("%s got:", (child) ? "Child" : "Parent");
first = 0;
}
putchar(c);
} while (c != '\n');
}
void wr(int fd, const char * msg, int child)
{
write(fd, msg, strlen(msg));
printf("%s sent:%s", (child) ? "Child" : "Parent", msg);
}
int main() {
int child_to_parent[2];
int parent_to_child[2];
pipe(child_to_parent);
pipe(parent_to_child);
pid_t id = fork();
if (id == 0) {
close(parent_to_child[1]);
close(child_to_parent[0]);
rd(parent_to_child[0], 1);
wr(child_to_parent[1], "hi\n", 1);
rd(parent_to_child[0], 1);
wr(child_to_parent[1], "fine, and you ?\n", 1);
rd(parent_to_child[0], 1);
} else {
close(parent_to_child[0]);
close(child_to_parent[1]);
wr(parent_to_child[1], "hello\n", 0);
rd(child_to_parent[0], 0);
wr(parent_to_child[1], "how are you ?\n", 0);
rd(child_to_parent[0], 0);
wr(parent_to_child[1], "fine too\n", 0);
}
}
编译和执行:
pi@raspberrypi:/tmp $ gcc -pedantic -Wextra p.c
pi@raspberrypi:/tmp $ ./a.out
Parent sent:hello
Child got:hello
Child sent:hi
Parent got:hi
Parent sent:how are you ?
Child got:how are you ?
Child sent:fine, and you ?
Parent got:fine, and you ?
Parent sent:fine too
Child got:fine too
还可以使用 fread / fwrite ,在 fwrite 之后必须使用 fflush 才能被阻止。幸运的是,发送后不必关闭管道即可读取和应答,否则只能交换一条消息。我仍然使用\ n指示发送的缓冲区的结尾:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
void rd(FILE * fd, int child)
{
char c;
int first = 1;
do {
if (!fread(&c, 1, 1, fd))
break;
if (first) {
printf("%s got:", (child) ? "Child" : "Parent");
first = 0;
}
putchar(c);
} while (c != '\n');
}
void wr(FILE * fd, const char * msg, int child)
{
fwrite(msg, strlen(msg), 1, fd);
fflush(fd);
printf("%s sent:%s", (child) ? "Child" : "Parent", msg);
}
int main() {
int child_to_parent[2];
int parent_to_child[2];
pipe(child_to_parent);
pipe(parent_to_child);
pid_t id = fork();
if (id == 0) {
close(parent_to_child[1]);
close(child_to_parent[0]);
FILE* out = fdopen(child_to_parent[1], "w");
FILE* in = fdopen(parent_to_child[0], "r");
rd(in, 1);
wr(out, "hi\n", 1);
rd(in, 1);
wr(out, "fine, and you ?\n", 1);
rd(in, 1);
} else {
close(parent_to_child[0]);
close(child_to_parent[1]);
FILE* in = fdopen(child_to_parent[0], "r");
FILE* out = fdopen(parent_to_child[1], "w");
wr(out, "hello\n", 0);
rd(in, 0);
wr(out, "how are you ?\n", 0);
rd(in, 0);
wr(out, "fine too\n", 0);
}
}
编译和执行:
pi@raspberrypi:/tmp $ gcc -pedantic -Wextra pp.c
pi@raspberrypi:/tmp $ ./a.out
Parent sent:hello
Child got:hello
Child sent:hi
Parent got:hi
Parent sent:how are you ?
Child got:how are you ?
Child sent:fine, and you ?
Parent got:fine, and you ?
Parent sent:fine too
Child got:fine too
最后,如果您想使用fscanf("%s", ...)
,则需要在单词后发送一个分隔符(例如空格或\ n),以免阻塞 fscanf ,当然要同时读取分隔符,在 fwrite 之后需要 fflush 。
如果我稍微改变一下您的程序,
#include <stdio.h>
#include <unistd.h>
int main() {
int child_to_parent[2];
int parent_to_child[2];
pipe(child_to_parent);
pipe(parent_to_child);
pid_t id = fork();
if (id == 0) {
close(parent_to_child[1]);
close(child_to_parent[0]);
FILE* out = fdopen(child_to_parent[1], "w");
FILE* in = fdopen(parent_to_child[0], "r");
char msg[16], c;
fscanf(in ,"%s%c", msg, &c);
printf("Child got: %s\n", msg);
fprintf(out, "hi ");
fflush(out);
printf("Child sent: hi\n");
fscanf(in ,"%s%c", msg, &c);
printf("Child got: %s\n", msg);
fprintf(out, "fine,you? ");
fflush(out);
printf("Child sent: fine,you?\n");
fscanf(in ,"%s%c", msg, &c);
printf("Child got: %s\n", msg);
} else {
close(parent_to_child[0]);
close(child_to_parent[1]);
FILE* in = fdopen(child_to_parent[0], "r");
FILE* out = fdopen(parent_to_child[1], "w");
fprintf(out, "hello\n");
fflush(out);
printf("Parent sent: hello\n");
char msg[16], c;
fscanf(in, "%s%c", msg, &c);
printf("Parent got: %s\n", msg);
fprintf(out, "how-are-you? ");
fflush(out);
printf("Parent sent: how-are-you?\n");
fscanf(in, "%s%c", msg, &c);
printf("Parent got: %s\n", msg);
fprintf(out, "fine-too ");
fflush(out);
printf("Parent sent: fine-too\n");
}
}
编译和执行:
pi@raspberrypi:/tmp $ gcc -pedantic -Wextra ppp.c
pi@raspberrypi:/tmp $ ./a.out
Parent sent: hello
Child got: hello
Child sent: hi
Parent got: hi
Parent sent: how-are-you?
Child got: how-are-you?
Child sent: fine,you?
Parent got: fine,you?
Parent sent: fine-too
Child got: fine-too
答案 1 :(得分:0)
fscanf
读取直到使用isspace
时找到一些空格字符(由%s
给出),但您没有发送一个空白字符。因此fscanf
永远不会返回,因为它正在等待空白。
使用fread
和fwrite
代替fscanf
和fprintf
,您的管道将正常工作。
答案 2 :(得分:0)
在父项和子项中,完成对管道的写入后,请用fflush
刷新缓冲区,并用close
关闭管道的写入结尾。
这是一个稍作修改的程序:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
FILE *fdopen(int fd, const char *mode);
int main() {
int child_to_parent[2];
int parent_to_child[2];
pipe(child_to_parent);
pipe(parent_to_child);
pid_t id = fork();
if (id == 0) {
close(parent_to_child[1]);
close(child_to_parent[0]);
FILE* out = fdopen(child_to_parent[1], "w");
FILE* in = fdopen(parent_to_child[0], "r");
char msg[6];
printf("Child gonna read...\n");
fscanf(in ,"%s", msg);
printf("Child got: %s\n", msg);
fprintf(out, "hi");
fflush(out);
printf("Child sent: hi\n");
close(child_to_parent[1]);
} else {
close(parent_to_child[0]);
close(child_to_parent[1]);
FILE* in = fdopen(child_to_parent[0], "r");
FILE* out = fdopen(parent_to_child[1], "w");
fprintf(out, "hello");
printf("Parent sent: hello\n");
fflush(out);
close(parent_to_child[1]);
char msg[3];
printf("Parent gonna read...\n");
fscanf(in, "%s", msg);
printf("Parent got: %s\n", msg);
}
}
运行时的输出:
Parent sent: hello
Parent gonna read...
Child gonna read...
Child got: hello
Child sent: hi
Parent got: hi