我正在学习管道,我遇到了问题。我希望我的程序可以这样工作:
grep [word to find] [file to search] | grep -i [without word] | wc -l
它可以编译并且没有错误,但它没有输出(至少没有像我想要的那样在标准输出上)。奇怪的是,当我尝试在最后一个 fork 中 printf sth 时,它正在 stdin 上打印它。我没有在这个分叉或父进程中更改标准输出,所以对我来说似乎很奇怪。我正在尝试关闭未使用的管道并刷新标准输出(它仍然在这里做某事吗?),但可能还有更多事情要做。
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
void help() {
printf( "Usage of the program:\n"
"\t./alagrep [fileToSearch] [wordToFind] [wordToExpel]\n");
}
int main(int argc, char *argv[]) {
if(argc != 4) {
help();
exit(EXIT_FAILURE);
}
int fd[2];
if(pipe(fd) != 0) {
printf("Error while opening a pipe.\n");
exit(EXIT_FAILURE);
}
pid_t pid;
if((pid = fork()) == -1) {
printf("Error while forking.\n");
exit(EXIT_FAILURE);
} else if(pid == 0) {
close(fd[0]);
if(dup2(fd[1],STDOUT_FILENO) < 0) {
printf("Cannot duplicate stdout.\n");
_exit(EXIT_FAILURE);
}
close(fd[1]);
execl("/bin/grep","grep",argv[2],argv[1],NULL);
fflush(stdout);
}
close(fd[1]);
int fd1[2];
if(pipe(fd1) != 0) {
printf("Error while opening a pipe.\n");
exit(EXIT_FAILURE);
}
if((pid = fork()) == -1) {
printf("Error while forking.\n");
exit(EXIT_FAILURE);
} else if(pid == 0) {
close(fd1[0]);
if(dup2(fd[0],STDIN_FILENO) < 0) {
printf("Cannot duplicate stdin.\n");
_exit(EXIT_FAILURE);
}
if(dup2(fd1[1],STDOUT_FILENO) < 0) {
printf("Cannot duplicate stdout.\n");
_exit(EXIT_FAILURE);
}
close(fd[0]);
close(fd1[1]);
execl("/bin/grep","grep","-i",argv[3],NULL);
fflush(stdout);
}
close(fd[0]);
close(fd1[1]);
if((pid = fork()) == -1) {
printf("Error while forking.\n");
exit(EXIT_FAILURE);
} else if(pid == 0) {
close(fd1[1]);
if(dup2(fd1[0],STDIN_FILENO) < 0) {
printf("Cannot duplicate stdin.\n");
_exit(EXIT_FAILURE);
}
close(fd1[0]);
execl("/bin/wc","wc","-l",NULL);
fflush(stdout);
}
close(fd1[0]);
return 0;
}