系统的返回值()不返回执行程序的价值 [英] Return value of system() is not return value of executed program

查看:151
本文介绍了系统的返回值()不返回执行程序的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行的可执行文件,其主要()使用系统将返回2()
这是我做的。

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;INT主(INT ARGC,CHAR * agrv [])
{
    字符命令[7];
    的strcpy(指挥,./test1);
    的printf(返回值数:%d \\ n,系统(指挥));    返回0;
}

测试1

 的#include<&stdio.h中GT;INT主要(无效)
{
    的printf(测试1已经执行,它的返回值是2 \\ n);    返回2;
}

这是我收到

  test1的已执行和返回值是2
返回值:512

我的问题是,为什么我得到 512


解决方案

系统的返回值实际上是POSIX下waitpid函数()的返回值。

状态其实有很多嵌在它的信息:

从系统(3)手册页​​:


  

下面的宏可用于测试的出口的方式
  处理。一前三个宏将计算为一个非零
  (真)值:


  
  

WIFEXITED(状态)


  
  

如果通过调用正常终止进程_exit(2)或exit(3)。


  
  

WIFSIGNALED(状态)


  
  

如果进程终止,由于收到信号。


  
  

WIFSTOPPED(状态)


  
  

如果进程尚未终止,但已经停止并且可以重新开始。结果
  这个宏可真只有等待调用中指定的
   WUNTRACED 选项,或者如果子进程正被跟踪(见的ptrace(2))。


  
  

根据这些宏的值,下面的宏产生
  关于孩子过程中的剩余状态的信息:


  
  

WEXITSTATUS(状态)


  
  

如果 WIFEXITED(状态)真正,计算结果为低阶参数的8位传递给_exit(2)或exit(3)孩子。


  
  

WTERMSIG(状态)


  
  

如果 WIFSIGNALED(状态)真正,计算结果为导致信号的数量
  终止进程。


  
  

WCOREDUMP(状态)


  
  

如果 WIFSIGNALED(状态)真正,计算结果为真,如果该进程的终止是伴随着创建包含当接收到信号的过程中的图像的核心文件。


  
  

WSTOPSIG(状态)


  
  

如果 WIFSTOPPED(状态)真正,计算结果为导致进程停止的信号的数


解决方案

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&limits.h中GT;INT主(INT ARGC,CHAR *的argv [])
{
    INT状态;
    字符命令[PATH_MAX]; / * PATH_MAX将sys / syslimits.h定义,通过limits.h中包括* /
    的strcpy(指挥,./test1);
    状态=系统(指挥);
    如果(WIFEXITED(状态)){
          的printf(返回值数:%d \\ n,WEXITSTATUS(状态));
    }
    否则如果(WIFSIGNALED(状态)){
          的printf(该计划退出,因为信号(信号没有数:%d)\\ n,WTERMSIG(状态));
    }
    返回0;
}

I would like to execute an executable file whose main() returns 2 using system(). This is what I did

#include <stdio.h>
#include <string.h>

int main(int argc, char *agrv[])
{
    char command[7];
    strcpy(command, "./test1");
    printf("The return value: %d\n", system(command));

    return 0;
}

and test1 is

#include <stdio.h>

int main(void)
{
    printf("test1 has been executed and its return value is 2\n");

    return 2;
}

This is what I'm getting

test1 has been executed and its return value is 2
The return value: 512

My question is why I'm getting 512.

解决方案

The return value of system is actually the return value of waitpid() under POSIX.

status actually has a lot of information embedded in it:

From the system(3) manpage:

The following macros may be used to test the manner of exit of the process. One of the first three macros will evaluate to a non-zero (true) value:

WIFEXITED(status)

True if the process terminated normally by a call to _exit(2) or exit(3).

WIFSIGNALED(status)

True if the process terminated due to receipt of a signal.

WIFSTOPPED(status)

True if the process has not terminated, but has stopped and can be restarted.
This macro can be true only if the wait call specified the WUNTRACED option or if the child process is being traced (see ptrace(2)).

Depending on the values of those macros, the following macros produce the remaining status information about the child process:

WEXITSTATUS(status)

If WIFEXITED(status) is true, evaluates to the low-order 8 bits of the argument passed to _exit(2) or exit(3) by the child.

WTERMSIG(status)

If WIFSIGNALED(status) is true, evaluates to the number of the signal that caused the termination of the process.

WCOREDUMP(status)

If WIFSIGNALED(status) is true, evaluates as true if the termination of the process was accompanied by the creation of a core file containing an image of the process when the signal was received.

WSTOPSIG(status)

If WIFSTOPPED(status) is true, evaluates to the number of the signal that caused the process to stop.

Solution

#include <stdio.h>
#include <string.h>
#include <limits.h>

int main(int argc, char *argv[])
{
    int status;
    char command[PATH_MAX]; /* PATH_MAX is defined in sys/syslimits.h, included by limits.h */
    strcpy(command, "./test1");
    status = system(command);
    if ( WIFEXITED(status) ) {
          printf("The return value: %d\n", WEXITSTATUS(status));
    }
    else if (WIFSIGNALED(status)) {
          printf("The program exited because of signal (signal no:%d)\n", WTERMSIG(status));
    } 
    return 0;
}

这篇关于系统的返回值()不返回执行程序的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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