我的代码中有这一行:
t = wait(&status);
Run Code Online (Sandbox Code Playgroud)
当子进程工作时,status的值为0.
但是为什么它不起作用时会返回256?为什么改变子进程给予退出参数值时存在错误不会改变任何东西(出口(2)而不是exit(1)例如)?
谢谢
编辑:我在linux上,我用GCC编译.
我定义了这样的状态
int status;
t = wait(&status);
Run Code Online (Sandbox Code Playgroud)
lar*_*sks 12
给出这样的代码......
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;
}
Run Code Online (Sandbox Code Playgroud)
...... res将是的价值256.这是因为返回值来自wait进程的退出状态以及进程退出的原因.通常,您不应尝试直接解释非零返回值wait; 你应该使用WIF...宏.例如,要查看进程是否正常退出:
WIFEXITED(status)
True if the process terminated normally by a call to _exit(2) or
exit(3).
Run Code Online (Sandbox Code Playgroud)
然后获得退出状态:
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.
Run Code Online (Sandbox Code Playgroud)
例如:
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;
}
Run Code Online (Sandbox Code Playgroud)
您可以在wait(2)手册页中阅读更多详细信息.
小智 5
状态码包含有关子进程如何退出的各种信息。提供宏来从状态代码获取信息。
来自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.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26770 次 |
| 最近记录: |