c调用python获取返回值,在C ++中的system()函数调用的返回值,用于运行Python程序...

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

So far, I have found 3 results:

When the python process completes successfully, value returned by system() is 0

When the python process is killed mid-execution (using kill -9 pid), value returned by system() is 9

When the python process fails on its own due to incorrect parameters, value returned by system() is 512

This does not fit with what I've read about system() function.

Furthermore, the code for the python program being invoked shows that it exits with sys.exit(2) when any error is encountered, and sys.exit(0) when execution completes successfully.

Could anyone relate these two? Am I interpreting the return value in a wrong manner? Is there some Linux processing involved that takes the argument of the sys.exit() function of the python program and returns value of system() based on it?

解决方案

The exit code of the program you call can be fetched with WEXITSTATUS(status) as per the manual page. Also see the manual page for 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";

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值