我正在使用execlp()
来在子进程上执行命令,并保存到管道中以供父进程读取,例如
int pipefd[2];
if (pipe(pipefd)) {
perror("pipe");
exit(127);
}
if(!fork()){
close(pipefd[0]);
dup2(pipefd[1], 1);
close(pipefd[1]);
execlp("ls", "ls", NULL);
} else {
close(pipefd[1]);
dup2(pipefd[0], 0);
close(pipefd[0]);
execlp("wc", "wc", NULL);
}
在某些情况下,父级不必执行任何操作,而只是在屏幕上打印出管道的内容,如何在屏幕上打印管道(由于未知的输出大小,可能不存储到变量中)。
我如何在[the]屏幕上打印[管道的内容]
read()
从管道开始printf("%d\n", byte);
,直到管道为空为止,直到read()
返回为止0
。
如果可以确保只是文本通过管道,则不要将字节打印为int
(如上所示,每行一个),而是char
使用printf("%c", byte);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句