C语言 我用了wait(&status),status的值为256,为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24130990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:09:11  来源:igfitidea点击:

I used wait(&status) and the value of status is 256, why?

cwait

提问by Simon

I have this line in my code :

我的代码中有这一行:

t = wait(&status); 

When the child process works, the value of status is 0, well.

当子进程工作时,status的值为0,好吧。

But why does it return 256 when it doesn't work? And why changing the value of the argument given to exit in the child process when there is an error doesn't change anything (exit(2) instead of exit(1) for example)?

但是为什么它不起作用时返回256?为什么在出现错误时更改给子进程中退出的参数值不会改变任何东西(例如,exit(2) 而不是 exit(1))?

Thank you

谢谢

Edit : I'm on linux, and I compiled with GCC.

编辑:我在 linux 上,我用 GCC 编译。

I defined status like this

我这样定义状态

int status;
t = wait(&status); 

回答by larsks

Given code like this...

给定这样的代码......

int main(int argc, char **argv) {
    pid_t pid;
    int res;

    pid = fork();
    if (pid == 0) {
        printf("child\n");
        exit(1);
    }

    pid = wait(&res);
    printf("raw res=%d\n", res);

    return 0;
}

...the value of reswill be 256. This is because the return value from waitencodes both the exit status of the process as well as the reason the process exited. In general, you should not attempt to interpret non-zero return values from waitdirectly; you should use of the WIF...macros. For example, to see if a process exited normally:

... reswill的值是256。这是因为 from 的返回值wait编码了进程的退出状态以及进程退出的原因。通常,您不应尝试直接解释非零返回值wait;你应该使用WIF...宏。例如,查看进程是否正常退出:

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

And then to get the exit status:

然后获取退出状态:

 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.

For example:

例如:

int main(int argc, char **argv) {
    pid_t pid;
    int res;

    pid = fork();
    if (pid == 0) {
        printf("child\n");
        exit(1);
    }

    pid = wait(&res);
    printf("raw res=%d\n", res);

    if (WIFEXITED(res))
        printf("exit status = %d\n", WEXITSTATUS(res));
    return 0;
}

You can read more details in the wait(2)man page.

您可以在wait(2)手册页中阅读更多详细信息。

回答by Thomas Shaw

The status code contains various information about how the child process exited. Macros are provided to get information from the status code.

状态代码包含有关子进程如何退出的各种信息。提供了宏以从状态代码中获取信息。

From wait(2)on linux:

wait(2)linux 上:

If status is not NULL, wait() and waitpid() store status information in the int to which it points.  This integer can be inspected with the following macros (which take the integer itself as an argu-
   ment, not a pointer to it, as is done in wait() and waitpid()!):

   WIFEXITED(status)
          returns true if the child terminated normally, that is, by calling exit(3) or _exit(2), or by returning from main().

   WEXITSTATUS(status)
          returns  the  exit status of the child.  This consists of the least significant 8 bits of the status argument that the child specified in a call to exit(3) or _exit(2) or as the argument for a
          return statement in main().  This macro should be employed only if WIFEXITED returned true.

   WIFSIGNALED(status)
          returns true if the child process was terminated by a signal.

   WTERMSIG(status)
          returns the number of the signal that caused the child process to terminate.  This macro should be employed only if WIFSIGNALED returned true.

   WCOREDUMP(status)
          returns true if the child produced a core dump.  This macro should be employed only if WIFSIGNALED returned true.  This macro is not specified in POSIX.1-2001 and is not available on some UNIX
          implementations (e.g., AIX, SunOS).  Only use this enclosed in #ifdef WCOREDUMP ... #endif.

   WIFSTOPPED(status)
          returns true if the child process was stopped by delivery of a signal; this is possible only if the call was done using WUNTRACED or when the child is being traced (see ptrace(2)).

   WSTOPSIG(status)
          returns the number of the signal which caused the child to stop.  This macro should be employed only if WIFSTOPPED returned true.

   WIFCONTINUED(status)
          (since Linux 2.6.10) returns true if the child process was resumed by delivery of SIGCONT.

回答by FoggyDay

SUGGESTION: try one of the following "Process Completion Status" macros:

建议:尝试以下“进程完成状态”宏之一:

http://www.gnu.org/software/libc/manual/html_node/Process-Completion-Status.html

http://www.gnu.org/software/libc/manual/html_node/Process-Completion-Status.html

EXAMPLE:

例子:

  int status = 0;
  ..
  int retval = wait (&status);
  if (WIFEXITED(status)) 
    printf("OK: Child exited with exit status %d.\n", WEXITSTATUS(status));
  else
    printf("ERROR: Child has not terminated correctly.\n");