简体   繁体   中英

Redirect child process's stdin and stdout to pipes


EDIT: The solution is

 int c1=dup2(pipes[0][1],STDOUT_FILENO);
 int c2=dup2(pipes[1][0],STDIN_FILENO);


 setvbuf(stdout,NULL,_IONBF,0);

It is SETVBUF to set stdout to be non-buffered. Even though I was printing the newline character if the destination is not an actual screen, I guess, it becomes buffered.



EDIT: When I put fflush(stdout) after LINE 1 and fflush(fout) after LINE 4 it works as expected. However, it does not work without the fflush(stdout) after LINE 1. The problem is that I would not be able to put fflush in the program which I am planning to run.


I am trying to start another program from my process. I don't have access to its code but I know it uses stdin and stdout for user interaction. I am trying to start that program by creating 2 pipes, fork-ing and redirecting the child's stdin/stdout to the proper pipe ends. The points is that the parent should be able communicate with the child via file descriptors, while its stdin/stdout should be intact. The POPEN syscall only opens unidirectional pipe. The following code almost works.

There are 4 lines marked as LINE 1..4.

LINE 1 is child sending to pipe, LINE 2 is child receiving from pipe, LINE 3 is parent sending to pipe, LINE 4 is parent receiving from pipe,

This is just a toy example to make sure things work. The issue is that is all 4 lines LINE1..4 are uncommented the output I see on the terminal is

PARENT1: -1
FD: 1 0    4 5    0 1
DEBUG1: 0
DEBUG2: 0

While if LINE 1 and LINE 3 are uncommented only I see a continuous stream of data. Same happens if if only LINE 2 and LINE 4 are uncommented. However, I want a full bidirectional communication. Also adding the commented SLEEP does not change the behavior.

What could be the issue here. I wonder why is there no bidirectional POPEN.

int pid;
int pipes[2][2];

pipe(pipes[0]);
pipe(pipes[1]);

pid=fork();

if(pid==0)
  {
  //usleep(1000000);
  close(pipes[0][0]);
  close(pipes[1][1]);

  int c1=dup2(pipes[0][1],STDOUT_FILENO);
  int c2=dup2(pipes[1][0],STDIN_FILENO);
  //int c2=dup2(STDIN_FILENO,pipes[1][0]);

  fprintf(stderr,"FD: %d %d    %d %d    %d %d\n",c1,c2,pipes[0][1],pipes[1][0],STDIN_FILENO,STDOUT_FILENO);

  //FILE*fout=fdopen(pipes[0][1],"w");
  //FILE*fin =fdopen(pipes[1][0],"r");
  while(1)
    {
    static int c1=0;
    fprintf(stderr,"DEBUG1: %d\n",c1);
    printf("%d\n",c1);                      // LINE 1
    fprintf(stderr,"DEBUG2: %d\n",c1);
    scanf("%d",&c1);                        // LINE 2
    fprintf(stderr,"DEBUG3: %d\n",c1);
    c1++;
    }
  //fclose(fout);
  //fclose(fin);
  return 0;
  }

close(pipes[0][1]);
close(pipes[1][0]);

char buffer[100];
FILE*fin=fdopen(pipes[0][0],"r");
FILE*fout=fdopen(pipes[1][1],"w");
while(1)
  {
  int c1=-1;
  printf("PARENT1: %d\n",c1);
  fscanf(fin,"%d",&c1);                         // LINE 3
  printf("Recv: %d\n",c1);

  fprintf(fout,"%d\n",c1+1);                    // LINE 4
  printf("PARENT3: %d\n",c1+1);
  }
fclose(fin);
fclose(fout);

Your code is quite long so I'm not sure that I have understand everything but why you don't use select ? Do you want to redirect the output of the child in a tird process or use it in your parent process ?

The following exemple is with cat in the child process.

#include <unistd.h>
#include <stdlib.h>

int     main()
{
  pid_t pid;
  int   p[2];


  pipe(p);
  pid = fork();
  if (pid == 0)
    {
      dup2(p[1], 1); // redirect the output (STDOUT to the pipe)
      close(p[0]);
      execlp("cat", "cat", NULL);
      exit(EXIT_FAILURE);
    }
  else
    {
      close(p[1]);
      fd_set rfds;
      char      buffer[10] = {0};

       while (1)
        {
          FD_ZERO(&rfds);
          FD_SET(p[0], &rfds); 
          select(p[0] + 1, &rfds, NULL, NULL, NULL); //wait for changes on p[0]
          if(FD_ISSET(p[0], &rfds))
            {
              int       ret = 0;
              while ((ret = read(p[0], buffer, 10)) > 0) //read on the pipe
                {
                  write(1, buffer, ret); //display the result
                  memset(buffer, 0, 10);
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM