exit与wait

ericming200409 2010-06-21 11:16:59
exit(int status) 的参数是怎么传递给父进程的,我在子进程中exit(2); 为什么在父进程用wait(&eixt_status);为什么得出的结果是:exit_status的值为512
...全文
130 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yzx714 2010-06-21
  • 打赏
  • 举报
回复
wait的值并不是exit的参数,详情请man或者APUE
mymtom 2010-06-21
  • 打赏
  • 举报
回复
man waitpid
可以查看WIFEXITED, WEXITSTATUS等宏定义的说明。
WIFEXITED(stat_val)
Evaluates to a non-zero value if status was returned for a child process that terminated normally.
WEXITSTATUS(stat_val)
If the value of WIFEXITED(stat_val) is non-zero, this macro evaluates to the low-order 8 bits of the status argument that the child process passed to _exit() or exit(), or the value the child process returned from main().
mymtom 2010-06-21
  • 打赏
  • 举报
回复

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

#include <unistd.h>

int
main(int argc, char *argv[])
{
int status;
pid_t pid;

pid = fork();

if (0 == pid) {
exit(2);
} else {
wait(&status);
printf("status = %d\n", status);
if (WIFEXITED(status))
printf("WEXITSTATUS(status)=%d\n", WEXITSTATUS(status));
}

return 0;

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧