c++ - Interactive two-way communication (using pipes) between child and parent processes -


i trying create child process, redirect stdin , stdout parent process, , interact child process. is, parent process should able receive input child, process it, , provide output child, , cycle repeats (like user interaction shell, end goal: simulate user interaction).

thus far have been able create , redirect pipes (the code below echoes input on parent process, user, through child). problem can once, child terminates after first i/o cycle, , sigpipe. how can keep child alive, , have read pipe when parent writes it?

p.s. know except can used simulate user interaction, need access returned data custom analysis, , i'd go way academic purposes.

#include <iostream> #include <string> #include <sstream> #include <unistd.h> #include <stdio.h> #include <sys/wait.h> #include <limits.h> #include <sys/types.h> #include <cstdlib>   #define max_cmd_size 256 #define max_buff_size 512  #define num_pipes 2 #define parent_read_pipe pipes[0] #define parent_write_pipe pipes[1]  #define read_end 0 #define write_end 1  #define parent_read_fd parent_read_pipe[read_end] #define parent_write_fd parent_write_pipe[write_end]  #define child_read_fd parent_write_pipe[read_end] #define child_write_fd parent_read_pipe[write_end]  int pipes[num_pipes][2];   int main() {     pipe(parent_read_pipe);     pipe(parent_write_pipe);      pid_t process_id = fork();      if (process_id<0) //throw diag::diagerror("error: error during fork()");     {std::cerr<<"error: error during fork()"; exit(1);}     else if (process_id==0)     {//child process         dup2(child_read_fd, stdin_fileno);         dup2(child_write_fd, stdout_fileno);          close(child_read_fd);         close(child_write_fd);         close(parent_read_fd);         close(parent_write_fd);          char buffer[max_buff_size];         int count = read(stdin_fileno, buffer, max_buff_size-1);         if (count >= 0)          {             buffer[count] = 0;             printf("%s", buffer);         }          else //{throw diag::diagerror("io error");}         {std::cerr<<"error: error during fork()"; exit(1);}         exit(0);     }     else     {//parent process        close(child_read_fd);         close(child_write_fd);          char buffer[max_buff_size];          std::string temp="";          while(std::getline(std::cin,temp))         {             write(parent_write_fd, temp.c_str(), temp.size());              //wait(&status);             int count = read(parent_read_fd, buffer, max_buff_size-1);             if (count >= 0)              {                 buffer[count] = 0;                 printf("%s", buffer);             }              else //{throw diag::diagerror("io error");}             {std::cerr<<"error: error during fork()"; exit(1);}         }       } } 

your child process exits after 1 read:

    if (process_id<0) throw diag::diagerror("error: error during fork()");     else if (process_id==0)     {//child process             .....             exit(0); /* <-- here */     } 

this of course causes pipe(s) closed , parent receives sigpipe while trying write pipe nobody reads anymore. parent code uses loop read/write to/from child - simmilar thing in child code - run in loop instead of exiting. can use special string of characters cause child process exit loop , terminate.

besides - if want read/write using stdin , stdout - invoke exec() after duplicated descriptors dup2 , closed right pipe ends. in child process use normal c++ streams std::cin , std::cout. of course requires refactoring child code separate program.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -