I'd execute this command ls -al / | tr a-j A-J | tr k-z K-Z
and I'd create three children from father process.
I know that I've to open two pipes, let father waits for all his children (closing all the pipes) and in the children I've to
- close STDIN on the first child
- close STDOUT on the first pipe and STDIN on the second pipe, on the second child
- close STDOUT on the third child
Now, this is my code. If I run it with just two children, it works. But, if I try to run it with three children, it doesn't work.
// command is ls -al / | tr a-j A-J | tr k-z K-Z
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv) {
int fd1[2];
int fd2[2];
pid_t pid = 0;
pipe(fd1);
pipe(fd2);
pid = fork();
if(pid == 0) {
close(fd1[0]);
dup2(fd1[1], 1);
close(fd1[1]);
execlp("ls", "ls", "-al", "/", NULL);
}
pid = fork();
if(pid == 0) {
close(fd1[1]);
dup2(fd1[0], 0);
close(fd1[0]);
close(fd2[0]);
dup2(fd2[1], 1);
close(fd2[1]);
execlp("tr", "tr", "a-j", "A-J", NULL);
}
pid = fork();
if(pid == 0) {
close(fd2[1]);
dup2(fd2[0], 0);
close(fd2[0]);
execlp("tr", "tr", "k-z", "K-Z", NULL);
}
close(fd1[0]);
close(fd1[1]);
close(fd2[0]);
close(fd2[1]);
while ((pid = wait(NULL)) != -1);
exit(0);
}
Thank you in advance.
tr
is not going to terminate until all the write ends of the pipe are closed. If the parentwait
s fortr
to terminate, it must close the end of the pipe before it waits, or the two process will be deadlocked. – William Pursell