【发布时间】:2017-01-31 21:35:10
【问题描述】:
我正在尝试使用 c 代码中的简单管道执行 Linux 命令“ls -l | tail -n 2”。
我添加了您的提示,现在可以使用,但输出并不完全符合预期。它在一行而不是两行中打印输出,并等待用户输入关闭。 这是新代码:
#include "stdio.h"
#include "unistd.h"
#include "stdlib.h"
#include "sys/wait.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void main()
{
char line[100];
pid_t pid;
int fd[2];
int status;
char* ls_arguments[] = {"ls", "-l", NULL};
char* tail_arguments[] = {"tail", "-n", "2", NULL};
pipe(fd);
pid = fork();
if(pid == 0)//ls client
{
close(1);
dup(fd[1]);
close(fd[0]);
execvp("ls", ls_arguments);
}
pid = fork();
if(pid == 0)//tail client
{
close(0);
close(fd[1]);
dup(fd[0]);
execvp("tail", tail_arguments);
}
wait(pid, 0, WNOHANG);
close(fd[0]);
close(fd[1]);
}
这应该运行“ls -l”命令并输出到管道,下一个“tail”客户端会将其作为输入并运行“tail -n 2”命令并打印出最终输出,但终端会打印没有。有什么帮助吗?
【问题讨论】:
-
将代码放在
execvp()之后是没有意义的,因为除非出错,否则它永远不会返回。 -
您需要在
ls客户端中close(fd[0])和tail客户端中的close(fd[1]),然后才能分别调用exec。 -
并且父进程需要关闭它们。
-
但是如果我在 ls 客户端中关闭管道,它会在尾部客户端中关闭,对吗?
-
没有。在一个进程中关闭 fd 对其他进程没有影响。在所有进程关闭它之前,管道不会消失。