I am writing a C program which will run Linux commands, like:
$ cat /etc/passwd | cut -f1 -d: | sort
The idea is to create child process using fork() to run the commands using execlp(). I planned to use two pipes for the communication and direct the input-output using dup().
The output is wrong:
ls -l | wc -c on command returns 1746
the program returns 1761
The code(edited to reflect suggestions):
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <limits.h>
int main()
{
int i,fd1[2],status,listpid[2];
pid_t child;
pipe(fd1);
for(i=0; i< 2; i++)
{
printf("\ncreating child\n");
if((child = fork()) == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}
else if(child == 0)
{
if(i == 0)
{
close(1); dup(fd1[1]);
close(fd1[0]);
close(fd1[1]);
printf("\nrunning ls -l\n");
fflush(stdout);
execlp("ls","ls","-l", (char *)NULL);
exit(EXIT_SUCCESS);
}
else if(i == 1)
{
close(0); dup(fd1[0]);
close(fd1[1]);
close(fd1[0]);
printf("\nrunning wc -c\n");
fflush(stdout);
execlp("wc","wc","-c", (char *)NULL);
exit(EXIT_SUCCESS);
}
}
else
{
listpid[i]=child;
}
}
close(fd1[0]);
close(fd1[1]);
for(i = 0; i < 2; i++)
{
waitpid(listpid[i], &status, 0);
if(WIFEXITED(status))
{
printf("\n[%d] TERMINATED (Status: %d)\n",listpid[i], WEXITSTATUS(status));
}
}
exit(EXIT_SUCCESS);
}
After your update, the two child processes are behaving correctly. However, you still need to add:
between the
forloop that launches the children and theforloop that collects the exit statuses.Because the write end of the pipe is still open,
wcdoes not receive EOF, so it doesn’t terminate, so your process is waiting indefinitely.