我正在使用 fork() 在 C 中创建后台进程。
当我创建其中一个进程时,我将它的 pid 添加到一个数组中,这样我就可以跟踪后台进程。
pid = fork();
if(pid == -1)
{
printf("error: fork()\n");
}
else if(pid == 0)
{
execvp(*args, args);
exit(0);
}
else
{
// add process to tracking array
addBGroundProcess(pid, args[0]);
}
我有一个收割僵尸的处理程序
void childHandler(int signum)
{
pid_t pid;
int status;
/* loop as long as there are children to process */
while (1) {
/* get zombie pids */
pid = waitpid(-1, &status, WNOHANG);
if (pid == -1)
{
if (errno == EINTR)
{
continue;
}
break;
}
else if (pid == 0)
{
break;
}
/* Remove this child from tracking array */
if (pid != mainPid)
cleanUpChild(pid);
}
}
当我创建后台进程时,处理程序正在执行并尝试清理子进程,然后我什至可以调用 addBGroundProcess。
我正在使用像 emacs& 这样不应该立即退出的命令。
我错过了什么?
谢谢。