arun11299 / cpp-subprocess

Subprocessing with modern C++
Other
457 stars 90 forks source link

Blocking when using read_all with socket stream #69

Open thanhtungtvg95 opened 3 years ago

thanhtungtvg95 commented 3 years ago

I am using function read_all() but process is blocking. I find this happen here: int read_bytes = read(fd, buf + rbytes, read_upto - rbytes);

Solution:

      fd_set fds;
      struct timeval timeout;

      timeout.tv_sec = 3; /* timeout in secs */
      timeout.tv_usec = 10000;
      FD_ZERO(&fds);
      FD_SET(fd, &fds);
      if (select(fd + 1, &fds, NULL, NULL, &timeout) > 0) {
        read_bytes = read(fd, buf + rbytes, read_upto - rbytes);
      } else {
        std::cout << "Time out" << std::endl;
      }