简体   繁体   中英

Increasing the size of a pipe output in c++

I wrote a c++ code to implement a bi-directional pipe between a parent and a child processor. Everything is working perfectly I just need to increase the size of the buffer (output of the pipe). I know that there are a lot of posts that tackled the size of char using malloc but I think my question is a bit specific since it requires the following:

  1. determining the size of the buffer(char array) returned from the pipe.
  2. increasing the size of the buffer to something larger than 1 MB.

Here's a snippet of my code:

#define PARENT_READ readpipe[0]
#define CHILD_WRITE readpipe[1]
#define CHILD_READ writepipe[0]
#define PARENT_WRITE writepipe[1]

 int writepipe[2] = {-1,-1};// parent -> child                                                                                                                                                          
 int readpipe[2] = {-1,-1};//child -> parent                                                                                                                                                            
 pid_t childpid;                                                                                                                                                                              
 string pipeInput;
 const char * c;
 **char buffer [1000000]; //1 MB for now ..**  

if((childpid=fork())<0)
   {
      //cannot fork child                                                                                                                                                                                
      printf("cannot fork child");
      exit(-1);
   }
  else if (childpid==0)
    {
      //child process                                                                                                                                                                                    
      close(PARENT_WRITE);
      close(PARENT_READ);
      dup2(CHILD_READ,0); //read data from pipe instead of stdin                                                                                                                                         
      dup2(CHILD_WRITE , 1);//write data to pipe instead of stdout                                                                                                                                       
      system("python import_modify_graph.py");
      close(CHILD_READ);
      close(CHILD_WRITE);
    }
  else
    {
     close(CHILD_READ);
     close(CHILD_WRITE);
     pipeInput="SOME INPUT";
     c=pipeInput.c_str();
     write(PARENT_WRITE,c,strlen(c));
     close(PARENT_WRITE);
     **read(PARENT_READ,buffer,1000000);**

Assuming you are under Linux, you can do that using:

fcntl(pipe_fd, F_SETPIPE_SZ, size);

http://man7.org/linux/man-pages/man2/fcntl.2.html

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