arun11299 / cpp-subprocess

Subprocessing with modern C++
Other
458 stars 91 forks source link

Using poll() changes the return code #10

Closed maged closed 6 years ago

maged commented 6 years ago
#include "subprocess.hpp"

using namespace subprocess;

int main() {
  std::vector<std::string> flags = {"ls", "nosuchfile"};
  std::string command = subprocess::util::join(flags);
  auto p = subprocess::Popen(command);
  // while (p.poll() == -1) {
  //   usleep(100 * 1000);
  // }

  p.wait();

  std::cout << "result " << p.retcode() << std::endl;
}

Outputs result: -1

While the below outputs result: 0.

#include "subprocess.hpp"

using namespace subprocess;

int main() {
  std::vector<std::string> flags = {"ls", "nosuchfile"};
  std::string command = subprocess::util::join(flags);
  auto p = subprocess::Popen(command);
  while (p.poll() == -1) {
    usleep(100 * 1000);
  }

  // p.wait();

  std::cout << "result " << p.retcode() << std::endl;
}
maged commented 6 years ago

wait() doesn't update retcode.

arun11299 commented 6 years ago

Thanks. The pull request has been merged.