The return value of a call to the system () function in C ++, used to run a Python program

I am working on Linux with code that calls a call system()to run a python program. I am interested in the value returned by this function call in order to understand how the python program was executed.

So far I have found 3 results:

  • When the python process succeeds, the value returned by system () is 0

  • When a python process is killed in the middle of execution (using kill -9 pid), the value returned by system () is 9

  • When a python process crashes due to incorrect parameters, the value returned by system () is 512

This is not what I read about the system () function .

In addition, the python program's startup code shows that it exits sys.exit(2)when any error occurs and sys.exit(0)when execution completes successfully.

Can anyone link these two? Am I misinterpreting the return value? Is there some kind of Linux processing that takes an argument to sys.exit()a python program function and returns a value system()based on it?

+5
source share
1 answer

The exit code of the program you are calling can be selected using the WEXITSTATUS(status)on the manual page . See also the manual page wait.

int status = system("/path/to/my/program");
if (status < 0)
    std::cout << "Error: " << strerror(errno) << '\n';
else
{
    if (WIFEXITED(status))
        std::cout << "Program returned normally, exit code " << WEXITSTATUS(status) << '\n';
    else
        std::cout << "Program exited abnormaly\n";
}
+9
source

All Articles