C程序对三个命令执行管道 [英] C program to perform a pipe on three commands
问题描述
我必须编写一个程序,执行与 du |相同的操作排序|头在命令行会做,但我被卡住了,我的程序不工作。现在的输出是 112。,程序不会终止。请帮助,我不知道该怎么办!
int main(void){
int fd [2];
int fd1 [2];
int pid;
if(pipe(fd)== -1){
perror(Pipe);
exit(1);
}
switch(fork()){
case -1:
perror(Fork);
exit(2);
case 0:
dup2(fd [1],STDOUT_FILENO);
close(fd [0]);
close(fd [1]);
execl(/ usr / bin / du,du,(char *)0);
exit(3);
}
if(pipe(fd1)== -1){
perror(Pipe);
exit(1);
}
switch(fork()){
case -1:
perror(Fork);
exit(2);
case 0:
dup2(fd [0],STDIN_FILENO);
dup2(fd1 [1],STDOUT_FILENO);
close(fd [0]);
close(fd [1]);
close(fd1 [0]);
close(fd1 [1]);
execl(/ usr / bin / sort,sort,(char *)0);
exit(3);
}
close(fd [0]);
close(fd [1]);
switch(fork()){
case -1:
perror(Fork);
exit(2);
case 0:
dup2(fd1 [0],STDIN_FILENO);
close(fd1 [0]);
close(fd1 [1]);
execl(/ usr / bin / head,head,(char *)0);
exit(3);
}
}
让头成为你的父进程,排序—其子进程和 du — 排序 的子项,或头 的孙子。
你需要两个管道,因此,两个数组— fd和fd1。让fd管道连接排序与标题和fd1— du 与排序。
您需要一个大的switch语句,父进程(头,pipe(fd)不为0)或子进程( sort ,pipe(fd)为0)。如果您位于排序中,则需要创建fd1管道并运行 grandchild过程 du 。现在,由于你又有两个进程(总共三个),你需要根据你的位置设置一个管道 - 无论你是在孙子还是子进程。您可以使用类似于switch fd的switch语句。这里的诀窍是正确设置fd1管道的输入和输出。
您的代码必须这样做:
int main(void){
int fd [2]; // sort< ===> head
int fd1 [2]; // du< ===> sort
pipe(fd);
switch(fork()){
case 0://我们在排序吗?
pipe(fd1); //如果是的话,让我们做一个新的管道!
switch(fork()){
case 0://我们在du吗?
dup2(fd1 [1],STDOUT_FILENO);
close(fd1 [0]);
close(fd1 [1]);
execl(/ usr / bin / du,du,(whatever directory),NULL);
exit(1);
默认值:
/ *如果不在du,我们在排序!在中间!
让我们正确设置输入和输出。
我们必须处理两个管道* /
dup2(fd1 [0],STDIN_FILENO);
dup2(fd [1],STDOUT_FILENO);
close(fd1 [0]);
close(fd1 [1]);
execl(/ usr / bin / sort,sort(flags if needed),(char *)0);
exit(2);
}
exit(3);
default://如果我们不在排序,我们在头
dup2(fd [0],STDIN_FILENO);
close(fd [0]);
close(fd [1]);
execl(/ usr / bin / head,head(flags if needed),(char *)0);
exit(4);
}
}
I have to write a program that will perform the same operation that du | sort | head in the command line would do, but I'm stuck, and my program is not working. The output right now is 112 . and the program doesn't terminate. Please help, I don't know what to do!
int main(void) {
int fd[2];
int fd1[2];
int pid;
if (pipe(fd) == -1) {
perror("Pipe");
exit(1);
}
switch (fork()) {
case -1:
perror("Fork");
exit(2);
case 0:
dup2(fd[1], STDOUT_FILENO);
close(fd[0]);
close(fd[1]);
execl("/usr/bin/du", "du", (char *) 0);
exit(3);
}
if (pipe(fd1) == -1) {
perror("Pipe");
exit(1);
}
switch (fork()) {
case -1:
perror("Fork");
exit(2);
case 0:
dup2(fd[0], STDIN_FILENO);
dup2(fd1[1], STDOUT_FILENO);
close(fd[0]);
close(fd[1]);
close(fd1[0]);
close(fd1[1]);
execl("/usr/bin/sort", "sort", (char *) 0);
exit(3);
}
close(fd[0]);
close(fd[1]);
switch (fork()) {
case -1:
perror("Fork");
exit(2);
case 0:
dup2(fd1[0], STDIN_FILENO);
close(fd1[0]);
close(fd1[1]);
execl("/usr/bin/head", "head", (char *) 0);
exit(3);
}
}
Let head be your parent process, sort — its child process, and du — the child of sort, or the grandchild of head.
You need two pipes, thus, two arrays — fd and fd1. Let the fd pipe connect sort with head, and fd1 — du with sort.
You will need one big switch statement, which will determine whether you currently are in the parent process (head, pipe(fd) is not 0) or the child (sort, pipe(fd) is 0). If you are in sort, you need to create the fd1 pipe and run the grandchild process du. Now, since you again have two processes (three in total), you need to set a pipe according to your location — whether you are in the grandchild or the child process. You can use a similar switch statement as you did for pipe fd. The trick here is to set the input and output for fd1 pipe correctly.
Your code must do something like this:
int main(void) {
int fd[2]; // sort <===> head
int fd1[2]; // du <===> sort
pipe(fd);
switch (fork()) {
case 0: // Are we in sort?
pipe(fd1); // If yes, let's make a new pipe!
switch (fork()) {
case 0: // Are we in du?
dup2(fd1[1], STDOUT_FILENO);
close(fd1[0]);
close(fd1[1]);
execl("/usr/bin/du", "du", (whatever directory), NULL);
exit(1);
default:
/* If not in du, we're in sort! in the middle!
Let's set up both input and output properly.
We have to deal with both pipes */
dup2(fd1[0], STDIN_FILENO);
dup2(fd[1], STDOUT_FILENO);
close(fd1[0]);
close(fd1[1]);
execl("/usr/bin/sort", "sort (flags if needed)", (char *) 0);
exit(2);
}
exit(3);
default: // If we're not in sort, we're in head
dup2(fd[0], STDIN_FILENO);
close(fd[0]);
close(fd[1]);
execl("/usr/bin/head", "head (flags if needed)", (char *) 0);
exit(4);
}
}
这篇关于C程序对三个命令执行管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!