加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-13 19:04:24 所属栏目:Linux 来源:网络整理
导读:我正试图从子进程运行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

我正试图从子进程运行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);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读