我正在尝试创建一个守护程序,该守护程序派生两个将无限运行的进程,以检查时间并在进程之间接收消息。当我运行程序时没有创建守护程序的步骤时,一切正常。但是,一旦我尝试创建一个守护程序,似乎我派生的子进程就变成了僵尸。
这是ps -ajx的输出
2678 3628 3628 2678 pts/2 3628 S+ 1000 0:00 ./assignment_daemon
3628 3629 3629 3629 ? -1 Zs 1000 0:00 [assignment_daem] <defunct>
这是C代码,
int main()
{
int pid = fork();
if(pid > 0)
{
printf("Parent process..\n");
sleep(10);
exit(EXIT_SUCCESS);
}
else if (pid == 0)
{
printf("Child process..\n");
if(setsid() < 0)
{
exit(EXIT_FAILURE);
}
umask(0);
if(chdir("/") < 0)
{
exit(EXIT_FAILURE);
}
int x;
for(x = sysconf(_SC_OPEN_MAX); x>=0;x--)
{
close(x);
}
openlog("assignment_daemon", LOG_PID|LOG_CONS,LOG_USER);
syslog(LOG_INFO,"assignment_daemon started...");
int SIZE = 2;
pid_t child_pids[SIZE];
int num_of_children = SIZE;
for(int i = 0; i < num_of_children; i++)
{
if((child_pids[i] = fork()) < 0)
{
perror("Error: failed to fork!\n");
syslog(LOG_ERR,"failed to fork...");
exit(EXIT_FAILURE);
}
else if(child_pids[i] == 0){
if(i == 0)
{
syslog(LOG_INFO,"Forked message_server...");
//run message server
char * argv_list[] = {"message_queue_server",NULL};
execv("./assignment_queue_server",argv_list);
}
if(i == 1)
{
syslog(LOG_INFO,"Forked time_check...");
//run time check
char * argv_list[] = {"time_check_process",NULL};
execv("./time_check_process",argv_list);
}
}
}
int status;
pid_t pid;
while(num_of_children > 0)
{
pid = wait(&status);
printf("Child %d exited with status 0x%x\n",pid,status);
syslog(LOG_INFO,"child has exited...");
num_of_children--;
}
syslog(LOG_INFO,"assignment_daemon ended...");
}
return 0;
}
任何帮助将不胜感激。 谢谢。