[英]linux C sending data from child to parent is fine, but fail to send data from child to parent
I would like to fork a child and connect pipes to the stdin and stdout of child.我想分叉一个孩子并将管道连接到孩子的标准输入和标准输出。 And then run the exec ./xx.然后运行 exec ./xx. After that I send 17 from parent to child and child print it.之后,我从父母发送 17 给孩子,孩子打印它。 all good so far.到目前为止一切都很好。 but when I send a 17 return to parent it doesnt work.但是当我向父母发送 17 个退货时,它不起作用。 The result is : output nothing and look like wait for some input.结果是:什么都不输出,看起来像是在等待一些输入。 if I remove the code "fscanf(b, "%d", &x); " in parent, the output is: from C 0 from p 17 I pretty confuse why I get the odd result??如果我删除父代码 "fscanf(b, "%d", &x); ",输出是: from C 0 from p 17 我很困惑为什么我得到奇怪的结果? Thank you谢谢
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
int main(int argc, char** argv) {
int fds[2], cp[2], x = 0;
pipe(fds);
pipe(cp);
int pid=fork();
// c
if (pid==0) {
close(fds[1]);
close(cp[0]);
dup2(fds[0],0);
dup2(cp[1], 1);
close(cp[1]);
close(fds[0]);
execlp("./xx", "xx", 0);
}
// p
if (pid) {
close(fds[0]);
close(cp[1]);
dup2(fds[1],1);
close(fds[1]);
FILE* a=fdopen(1, "w");
FILE* b=fdopen(cp[0], "r");
fprintf(a, "17");
fscanf(b, "%d", &x);
fprintf(stderr, "from C %d", x);
}
return 0;
}
XX XX
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
int main() {
int y = 0;
FILE* r=fdopen(STDIN_FILENO, "r");
fscanf(r, "%d", &y);
fprintf(stderr, "from p %d \n ", y);
FILE* w=fdopen(STDOUT_FILENO, "w");
fprintf(w, "17");
return 0;
}
I think I figured it out.我想我想通了。 You need to flush your output buffers.您需要刷新输出缓冲区。 fprintf only does this by default for stderr. fprintf 仅默认为 stderr 执行此操作。 So in the parent.c file:所以在 parent.c 文件中:
fprintf(a, "17");
fflush(a);
And in the child:在孩子身上:
fprintf(w, "17");
fflush(w);
I would have expected that to work on its own, but I'm not a C expert, and it didn't.我本来希望它自己工作,但我不是 C 专家,而且它没有。 However, changing the two lines in the parent to但是,将父级中的两行更改为
fprintf(a, "17\n");
fflush(a);
made it work for me.让它为我工作。
Use this code:使用此代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
int main(int argc, char** argv) {
int fds[2], cp[2], x = 0;
pipe(fds);
pipe(cp);
int pid = fork();
if (pid==0) {
close(fds[1]);
close(cp[0]);
dup2(fds[0], 0);
dup2(cp[1], 1);
execlp("./xx", "xx", NULL);
}
if (pid > 0) {
close(fds[0]);
close(cp[1]);
FILE * a = fdopen(fds[1], "w");
FILE * b = fdopen(cp[0], "r");
fprintf(a, "17\n");
fflush(a);
fscanf(b, "%d", &x);
fprintf(stderr, "from C %d\n", x);
} else {
// error while fork
perror("fork"); // print error to console.
return 1;
}
return 0;
}
And xx:和 xx:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
int main() {
int y = 0;
FILE* r = stdin;
fscanf(r, "%d", &y);
fprintf(stderr, "from p %d \n ", y);
FILE* w = stdout;
fprintf(w, "17\n");
fflush(w);
return 0;
}
It's working for me :)它对我有用:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.