Menu

#23 should rdbuf()->status() match exit() value?

xyzzy
closed-works-for-me
None
5
2016-06-07
2016-06-07
No

I wrote a custom program in C++ that looks liket this:

#include <cstdlib>
int main(){exit(1);}

It works in my shell, as I expect:

$ ./main
$ echo $?
1
$

However, the pstream library gices me -1

redi::pstream proc("./main");
auto return_code = proc.rdbuf()->status();
std::cout << return_code << '\n'; //prints -1

If I close the process, it gives me 36096

redi::pstream proc("./main");
proc.close();
auto return_code = proc.rdbuf()->status();
std::cout << return_code << '\n'; //prints 36096

If I read all the process streams, it gives me back 256:

redi::pstream proc("./main",redi::pstreams::psterr|redi::pstreams::pstdout);
while(std::Getline(proc.err(),temp)){}
proc.clear();
while(std::Getline(proc.out(),temp)){}
proc.close();
auto return_code = proc.rdbuf()->status();
std::cout << return_code << '\n'; //prints 256

Am I doing something wrong? Is a return value from 0 - 255 guaranteed to match the status()?

Discussion

  • Trevor Hickey

    Trevor Hickey - 2016-06-07

    like*

    getline*

    gives*

    I can't edit the ticket.

     

    Last edit: Trevor Hickey 2016-06-07
  • Trevor Hickey

    Trevor Hickey - 2016-06-07

    Returns: The exit status of the child process, or -1 if wait() has not yet been called to wait for the child to exit.

    Ignore the -1 then

     

    Last edit: Trevor Hickey 2016-06-07
  • Trevor Hickey

    Trevor Hickey - 2016-06-07

    Am I suppose to use WEXITSTATUS on wait()?

     
  • Trevor Hickey

    Trevor Hickey - 2016-06-07

    I figured it out. I have to call WEXITSTATUS() on the value returned back from status();

     

    Last edit: Trevor Hickey 2016-06-07
  • Trevor Hickey

    Trevor Hickey - 2016-06-07

    /close

     
  • Jonathan Wakely

    Jonathan Wakely - 2016-06-07

    Right, it returns the same value as wait or waitpid so you need to use WIFEXITED, WEXITSTATUS etc.

     

    Last edit: Jonathan Wakely 2016-06-07
  • Jonathan Wakely

    Jonathan Wakely - 2016-06-07
    • status: open --> closed-works-for-me
     

Log in to post a comment.

MongoDB Logo MongoDB