调用system()返回256 [英] call system() return 256

查看:134
本文介绍了调用system()返回256的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用system()在我的应用程序中打印pstack信息,但是它失败并返回256.但是我将下面的代码收集到一个新的主函数中,没关系.为什么?

I want to system() to print pstack info in my application, but it failed and return 256.But I pick up these codes as below to one new main function,that's ok. why?

char cmd[256] ;
string pstackCmd;
struct timeval tv;
struct tm lt;(&tv, NULL);

localtime_r(&tv.tv_sec, &lt);
sprintf(cmd, "pstack `pgrep app` > pstack_%04d%02d%02d%02d%02d%02d%03d",
                            lt.tm_year+1900,
                            lt.tm_mon+1,
                            lt.tm_mday,
                            lt.tm_hour,
                            lt.tm_min,
                            lt.tm_sec,
                            (int)(tv.tv_usec/1000));
pstackCmd = string(cmd);
int retValue = system(pstackCmd.c_str());
if ( retValue != 0)
{
    printf("pstack `pgrep app`,retValue:%d",retValue);
}

推荐答案

代替此:

if (retValue != 0)

您需要这个:

if (retValue == -1 || WEXITSTATUS(retValue) != 0)

也就是说,您首先通过查看 system()是否返回-1来检查是否存在故障;如果没有,则该命令的退出状态(传统上为零表示成功).

That is, you check for failure first by seeing if system() returned -1; if it did not then the exit status of the command (with zero meaning success, traditionally).

这篇关于调用system()返回256的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆