c – linux fork – execl,执行的进程变成了僵尸

前端之家收集整理的这篇文章主要介绍了c – linux fork – execl,执行的进程变成了僵尸前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正试图从子进程运行twinkle命令行.
例如这样:

int hangup() {
write_on_display("line3","            ");
write_on_display("hide_icon","DIALTONE");
write_on_display("hide_icon","BACKLIGHT");

int pid = fork();
if (pid == 0) {
    int res = execl("/usr/bin/twinkle"," ","--immediate","--cmd","answerbye",(char *) NULL);
    _exit(0);
} else {
    perror("hangup");
    return 0;
}
return 1;
}

但闪烁变成僵尸:

10020 pts/1    Z+     0:00 [twinkle] 

我试着设定
信号(SIGCHLD,SIG_IGN);
但没有成功.
实际上我认为儿童过程在闪烁结束之前就已经死了.

从命令行运行闪烁如:

twinkle --immediate --call 100

不会使僵尸 – 闪烁正确关闭.
那里我想念的是什么?

最佳答案
父进程需要使用子进程ID调用waitpid().从链接的参考页面

All of these system calls are used to wait for state changes in a child of the calling process,and obtain information about the child whose state has changed. A state change is considered to be: the child terminated; the child was stopped by a signal; or the child was resumed by a signal. In the case of a terminated child,performing a wait allows the system to release the resources associated with the child; if a wait is not performed,then the terminated child remains in a “zombie” state (see NOTES below).

例如:

pid_t pid = fork();
if (0 == pid)
{
    /* Child process. */
}
else
{
    /* Parent process,wait for child to complete. */
    int status;
    waitpid(pid,&status,0);
}
原文链接:https://www.f2er.com/linux/440290.html

猜你在找的Linux相关文章