piping stdout into other program stdin fails; no message go through the pipe

Viewed 49

I'm trying to program a shell with a fork and a pipe in C. I've tried to fork my process into to 2 and make the son speak with its parent: stdout to be the stdin of the child. I thought implementing it via a pipe. Here is my try:

void split(){

int fork_id = fork();

int pipes[2];
 pipe(pipes);

if (fork_id) {
    //parent

   dup2(pipes[1], STDOUT_FILENO);
    close(pipes[0]);
    close(pipes[1]);
} else {
    //child


     dup2(pipes[0], STDIN_FILENO);
    close(pipes[0]);
    close(pipes[1]);
}
}

Afterwards I run software with execvp (they run with the right argv [ I checked it])/ Unfortunately the 2nd program doesn't get the output of the first, it seems to be that it gets "". Can anyone spot the mistake?

1 Answers

credit to @kalyum. I forked before creating the pipe, therefore each process had its own pipe. therefore no connection was established between the 2.

Related