将管道内容打印到屏幕
Print pipe content to screen
我正在使用 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);
}
在某些情况下,父级不必执行任何操作,而只是在屏幕上打印出管道的内容,我如何在屏幕上打印管道(由于输出大小未知,可能没有存储到变量中).
how can I print [the content of the] pipe on [the] screen
read()
逐字节从管道开始 printf("%d\n", byte);
直到管道为空,即直到 read()
返回 0
.
如果您可以确定它只是通过管道传输的文本,请不要将字节打印为 int
(如上所示每行一个),而是使用 [=16 连续打印为 char
=]
我正在使用 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);
}
在某些情况下,父级不必执行任何操作,而只是在屏幕上打印出管道的内容,我如何在屏幕上打印管道(由于输出大小未知,可能没有存储到变量中).
how can I print [the content of the] pipe on [the] screen
read()
逐字节从管道开始 printf("%d\n", byte);
直到管道为空,即直到 read()
返回 0
.
如果您可以确定它只是通过管道传输的文本,请不要将字节打印为 int
(如上所示每行一个),而是使用 [=16 连续打印为 char
=]