#include<errno.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main(void) {
pid_t childpid;
int fd[2];
if (pipe(fd) == -1) { /* setup a pipe */
perror("Failed to setup pipeline");
return 1;
}
if ((childpid = fork()) == -1) { /* fork a child */
perror("Failed to fork a child");
return 1;
}
if (__________) {/* env is the child */
if (dup2(fd[1], __________) == -1)
perror("Failed to redirect stdout of env");
else if (____________________) /* close unused file descriptor */
perror("Failed to close extra pipe descriptors on env");
else {
execl(__________); /* execute env */
perror("Failed to exec env");
}
return 1;
}
if (dup2(fd[0],__________) == -1)
/* grep is the parent */
perror("Failed to redirect stdin of grep");
else if (____________________)
perror("Failed to close extra pipe file descriptors on grep");
else {
execl(__________); /*execute "grep HOME"*/
perror("Failed to exec grep");
}
return 1;
}
我想出了大部分的差距,但我似乎无法让其他人按照预期的方式工作。所以基本上,这个程序意味着使用重定向来实现env | grep HOME,打印出我的主目录。我将使用dup2和excel,但需要使用c和linux系统调用填补空白。