0

I'm practicing piping two processes together. I want to have both processes communicating with each other using two pipes. Here's what I have so far:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>

int main(){

  //setting up two pipes
  int pipe1[2];
  int pipe2[2];
  pipe(pipe1);
  pipe(pipe2);

  //setting up child and parent
  pid_t pid = fork();
  if(pid < 0){
    perror("fork failed\n");
    exit(-1);
  }
  if(pid == 0){
    printf("child\n");
    dup2(pipe1[1], STDOUT_FILENO);
    close(pipe1[1]);
    close(pipe1[0]);
    dup2(pipe2[0], STDIN_FILENO);
    close(pipe2[0]);
    close(pipe2[1]);
  }
  if(pid > 0){
    wait(NULL);
    printf("parent\n");
    dup2(pipe1[0], STDIN_FILENO);
    close(pipe1[0]);
    close(pipe2[1]);
    dup2(pipe2[1], STDOUT_FILENO);
    close(pipe2[0]);
    close(pipe2[1]);
  }

  return 0;
}

I would like both processes communicate with each other by having the input of one process be the output of the other. Have I set this up correctly? If so, where could I go from here if for example I would like the parent to send integers to child and the child were to perform operations on them, and then send them back to the parent for printing.

Thanks.

HoopsMcCann
  • 347
  • 2
  • 3
  • 18
  • If you want parent & child to be able to communicate via a pipes, there is no need for `dup2()`. The file descriptors opened in the parent are available in the `fork()'ed` child as well, so in a simple scenario like this you can safely refer to the 'ends' of the pipe directly without needing any form of I/O redirection set up... – user268396 Aug 31 '18 at 22:31
  • Of course things change a bit if you plan on `exec()'ing` anything in the child process and you want output from the `exec()'ed` binary to end up in the pipes. In *that* case I/O redirection using `dup2()` (like your code, but simpler) may be the simplest way to go. See also: https://stackoverflow.com/questions/2605130/redirecting-exec-output-to-a-buffer-or-file or any of the many tutorials out there. Do add proper error checking, though! – user268396 Aug 31 '18 at 22:36
  • Just running my eyes over this, nothing looks too terribly wrong. std::disclaimer("This is only a visual inspection, I haven't actually tried to compile and run this"). That said, it might be worth your while to try asking about this over on SO's sister site: [Code Review Stack Exchange](https://codereview.stackexchange.com/). Good luck! – dgnuff Aug 31 '18 at 23:14
  • Could you explain what the `wait(NULL);` is intended to do? – Mark Plotnick Aug 31 '18 at 23:41
  • @user268396 could you elaborate using code? I'm not quite sure what you mean. I'm still learning C, but could you also explain why after redirecting stdout and calling printf on any string still gets printed to the screen? Shouldn't the output be redirected into the pipe? Or is my understanding not correct – HoopsMcCann Aug 31 '18 at 23:56
  • @MarkPlotnick I'm using it to wait for the child process to terminate first. – HoopsMcCann Aug 31 '18 at 23:57
  • If the parent's standard output is connected to the child's standard input, and the parent's standard input is connected to the child's standard output, how are you going to print the results from the child (and how are you going to get input)? Also, the parent waits for the child to die before reconnecting its standard I/O channels to the pipe. This is not going to lead to happiness. (You do seem to be closing enough file descriptors; that much is good and avoids common problems. Unfortunately, I think you've been a bit too enthusiastic in duplicating the parent's standard I/O to the pipe.) – Jonathan Leffler Sep 01 '18 at 00:36

0 Answers0