I'm trying to create a simple program which simulates the "ls -l | tail -n 2" call in terminal. I'm using "fork" and "execvp" for that purpose. Well, here is the code:
int main(int argc, char *argv[])
{
int pipefd[2];
pid_t child1;
pid_t child2;
char* com1[] = {"ls", "-l",NULL};
char* com2[] = {"tail", "-n","2",NULL};
if (!(child1 = fork()))
{
close(STDOUT);
dup(pipefd[1]);
close(pipefd[1]);
execvp (com1[0], com1);
_exit(EXIT_SUCCESS);
}
else
{
close(pipefd[1]);
if (!(child2 = fork()))
{
close(STDIN);
dup(pipefd[0]); /* dupWR now holds the lowest fd available, meaning STDOUT's */
perror("dup 2");
close(pipefd[0]); /* reader will see EOF */
execvp (com2[0], com2);
_exit(EXIT_SUCCESS);
}
else
{
close(pipefd[0]);
waitpid(child2,0,0);
}
waitpid(child1,0,0);
}
return 0;
}
I get these errors:
dup 2: Bad file descriptor
tail: cannot fstat `standard input': Bad file descriptor
tail: -: Bad file descriptor
It seems to me that there is a problem in synchronization. In fact, if I declare: com2[] = {"ls", "-l",NULL}; It works fine (I mean as in normal shell). Moreover, I found that the second "dup" in the second "fork" returns error. Why is that? I don't know where is the problem with this code. Please help!
Edit: I added this code (forgot to create pipes):
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
Thanks, Useless!