C++ linux fork-execl,执行的进程变成僵尸
我正在尝试从子进程运行闪烁命令行。 例如:C++ linux fork-execl,执行的进程变成僵尸,c++,linux,fork,zombie-process,C++,Linux,Fork,Zombie Process,我正在尝试从子进程运行闪烁命令行。 例如: 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", " ", "--
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;
}
void catch_child(int sig_num)
{
/* when we get here, we know there's a zombie child waiting */
int child_status;
wait(&child_status);
}
但是闪烁变成了僵尸:
10020 pts/1 Z+ 0:00 [twinkle] <defunct>
10040 pts/1 Z+ 0:00 [twinkle] <defunct>
10053 pts/1 Z+ 0:00 [twinkle] <defunct>
10064 pts/1 Z+ 0:00 [twinkle] <defunct>
10097 pts/1 Z+ 0:00 [twinkle] <defunct>
10108 pts/1 Z+ 0:00 [twinkle] <defunct>
10130 pts/1 Z+ 0:00 [twinkle] <defunct>
不会使僵尸-闪烁正确关闭。
我缺少什么?父进程需要使用子进程的进程id调用。从链接的参考页: 所有这些系统调用都用于等待调用进程的子进程中的状态更改,并获取有关其状态已更改的子进程的信息。状态更改被视为:子级终止;孩子被一个信号拦住了;或者孩子被一个信号打断了对于终止的子级,执行等待允许系统释放与该子级相关的资源;如果未执行等待,则终止的子级仍处于“僵尸”状态(请参阅下面的注释) 例如:
pid_t pid = fork();
if (0 == pid)
{
/* Child process. */
}
else
{
/* Parent process, wait for child to complete. */
int status;
waitpid(pid, &status, 0);
}
是的,但我需要父母和孩子异步工作 事实上我发现了我的错误。 因此,如果有人有类似的问题,使用这样的信号处理函数:
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;
}
void catch_child(int sig_num)
{
/* when we get here, we know there's a zombie child waiting */
int child_status;
wait(&child_status);
}
及
信号(信号,捕捉儿童)
在main()函数中
一切正常
聚丙烯
在这里:
只是旁注:IIRC,除非
execl
产生错误,execl
之后的代码将不会执行。@Jite,更正。退出(0)如果execl()
成功,则code>将消失。最好在父进程中捕获“子进程结束”信号,然后在那里等待它。真的有用吗?