I had perfectly correct and working program around 5 years ago. At that point I stopped using it, I upgraded the OS, time passed, dust covered the code, and finally I dug it up just to discover it does not communicate with subprocess anymore.
Here is the code (simplified, but it shows the problem):
#include <stdio.h> #include <signal.h> #include <fcntl.h> #include <errno.h> #include <sys/types.h> #include <string.h> #include <sys/time.h> #include <sys/stat.h> #include <iostream> #include <string> #include <cassert> int main() { std::cout << "Creating the pipe for reading from the process" << std::endl; const std::string pipe_name = "/tmp/proc_comm"; { int res = mkfifo(pipe_name.c_str(),0777); assert(res==0); } std::cout << "Launching subprocess" << std::endl; FILE *cmd_handle = popen(("espeak -x -q -z 1> "+pipe_name+" 2> /dev/null").c_str(), "w"); assert(cmd_handle!=0); std::cout << "Opening the pipe" << std::endl; int pipe_id = open(pipe_name.c_str(),O_RDONLY); assert(pipe_id!=-1); const std::string message = "hello\n"; std::cout << "Sending the message" << std::endl; if (!fwrite(message.c_str(),sizeof(char),message.length(),cmd_handle)) assert(0); if (ferror(cmd_handle)) assert(0); if (fflush(cmd_handle)!=0) assert(0); fd_set output_set; FD_ZERO(&output_set); FD_SET(pipe_id,&output_set); static timeval timeout; timeout.tv_sec = 10; timeout.tv_usec = 0; std::cout << "Selecting the pipe for reading" << std::endl; const int inputs = select(pipe_id+1, // max of pipe ids + 1 &output_set,0,0,&timeout); if (inputs==-1) // error assert(0); else if (inputs==0) // nothing to read assert(0); // HERE (*) else { // we can only read from our pipe assert(inputs==1); assert(FD_ISSET(pipe_id,&output_set)); const int bufsize = 20000; char char_buf[bufsize]; memset(char_buf,0,sizeof(char_buf)); std::cout << "Reading from the pipe" << std::endl; const int count = read(pipe_id,char_buf,bufsize-1); if (count==-1) assert(0); std::cout << "Read " << count << std::endl; } return 0; } It compiles, it runs, the pipe for reading from the process is created and opened, the subprocess is launched, the message is sent but there is nothing to read from the process (the line HERE (*)).
So how do you read from the process nowadays? If possible I would like to keep the general workflow, i.e. using pipe for reading, and a process handle for writting to process.