进程退出时,有相应的exit_code,可用于判断进程退出的原因。
比如,waitpid()接口用于等待进程退出,此时被等待退出的进程的返回值比较重要,需要用其来判断进程退出的相应状态,而这就是通过进程退出时的exit_code来获取的。waitpid()库函数接口定义如下:
pid_t waitpid(pid_t pid, int *
status, int options);
其中status用于保存被wait进程的退出状态值,其实就是exit_code,相关实现代码如下:
-
SYSCALL_DEFINE3(waitpid, pid_t, pid, int __user *, stat_addr, int, options)
-
{
-
return sys_wait4(pid, stat_addr, options, NULL);
-
}
-
/*sys_waitpid()和sys_wait4系统调用的实现*/
-
SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr,
-
int, options, struct rusage __user *, ru)
-
{
-
struct wait_opts wo;
-
struct pid *pid = NULL;
-
enum pid_type type;
-
long ret;
-
-
if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
-
__WNOTHREAD|__WCLONE|__WALL))
-
return -EINVAL;
-
-
if (upid == -1)
-
type = PIDTYPE_MAX;
-
else if (upid < 0) {
-
type = PIDTYPE_PGID;
-
pid = find_get_pid(-upid);
-
} else if (upid == 0) {
-
type = PIDTYPE_PGID;
-
pid = get_task_pid(current, PIDTYPE_PGID);
-
} else /* upid > 0 */ {
-
type = PIDTYPE_PID;
-
pid = find_get_pid(upid);
-
}
-
-
wo.wo_type = type;
-
wo.wo_pid = pid;
-
wo.wo_flags = options | WEXITED;
-
wo.wo_info = NULL;
-
wo.wo_stat = stat_addr;
-
wo.wo_rusage = ru;
-
ret = do_wait(&wo);
-
put_pid(pid);
-
-
return ret;
-
}
do_wait()->
do_wait_thread()->
wait_consider_task->
wait_task_zombie
wait_task_stopped
wait_task_continued
-
/*sys_wait4和sys_waitpid的实现for task in state EXIT_ZOMBIE*/
-
static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
-
{
-
...
-
status = (p->signal->flags & SIGNAL_GROUP_EXIT)
-
? p->signal->group_exit_code : p->exit_code;
-
if (!retval && wo->wo_stat)
-
retval = put_user(status, wo->wo_stat);
-
...
-
}
其中,p->exit_code,即被wait进程的exit_code,其值有两种情况:
1、由用户态exit()接口传入,此时的exit_code=用户态传入的error_code<<8,相应代码如下:
-
SYSCALL_DEFINE1(exit, int, error_code)
-
{
-
do_exit((error_code&0xff)<<8);
-
}
2、内核中直接调用do_exit()时作为入参传入的错误码,此时的错误应该都是小于255(低8位表示)的。
阅读(4279) | 评论(0) | 转发(2) |