I am writing a C program which will run Linux commands, like:
cat /etc/passwd | grep list | cut -c 1-5
and i didnt have any result
*here the parent wait for the first child(child_pid) to finish;
and the first child wait for the second(pfils) !!
any idea?
code :
main()
{
pid_t child_pid;
int fd[2];
int pfd[2];
pipe(pfd);
child_pid = fork ();
if (child_pid != 0)
{
wait(child_pid);
close(pfd[1]);
dup2(pfd[0],0);
close(pfd[0]);
execlp("cut","cut","-c","1-5",NULL);
}
else
{
pid_t pfils = fork();
pipe(fd);
if(pfils == 0)
{
close(fd[0]);
dup2(fd[1],1);
close(fd[1]);
execlp("cat", "cat","/etc/passwd",NULL);
}
else
{
wait(pfils);
close(fd[1]);
dup2(fd[0],0);
close(fd[0]);
close(pfd[0]);
dup2(pfd[1],1);
close(pfd[1]);
execlp("grep","grep","list",NULL);
}
}
}