简体   繁体   English

为什么在调用 exit(0) 后,子进程仍然存在?

[英]why after the exit(0) is called, but the child process still remains?

in the parent process I want to start a daemon to do a long time work, so I use the double fork() to create the grandchild process to start the daemon.在父进程中我想启动一个守护进程做长时间的工作,所以我使用双 fork() 创建孙进程来启动守护进程。 The question is every once in a while, the child process does not exit successfully, I am sure that the printf() before the exit(0) is called, but when I use the "ps" I can see the child process pid listed, and it never exit.问题是每隔一段时间,子进程没有成功退出,我确定exit(0)之前的printf()被调用,但是当我使用“ps”时我可以看到列出的子进程pid ,它永远不会退出。 Do you know why this happened, should I call _exit(0) instead of exit(0) to make the child process exit?你知道为什么会这样吗,我应该调用 _exit(0) 而不是 exit(0) 来让子进程退出吗?

startProcess(const char *pidfile, const char *command)
    pid_t   pid;
    char    cmd[1024];
    char    *head = cmd;
    char    *end;
    char    *argv[128] = {0};
    int     argc = 0;

    if( 0 == (pid=fork()) ) { //fork a child 
        if( 0 > setpriority(PRIO_PROCESS, 0, 0) ) {
            perror();
            return;
        }
        pid = fork(); //fork a grandchild
        if(-1 == pid){
            exit(-1);
        }else if(0 == pid){ //grandchild process
            if (snprintf(cmd, sizeof(cmd), "%s", command) <= sizeof(cmd) - 1) {
                /* change command line to argc/argv format */
                do {
                    if (argc >= sizeof(argv)/sizeof(argv[0]) - 1)
                        break;
                    for (; (*head == ' ' || *head == '\t'); head++); //remove space or TAB in the header
                    if (*head == '\0')
                        break;
                    if (NULL != (end = strpbrk(head, " \t"))) {
                        *end = '\0';
                        argv[argc++] = head;
                        head = end + 1;
                    } else {
                        argv[argc++] = head;
                        break;
                    }
                } while (1);
                argv[argc] = NULL; // Maximal value of argc is ARRAY_SIZE(argv) - 1.

                /* execute command to start a daemon*/
                execvp(argv[0], argv);
           }
           //should not enter here
           exit(-1);
        }else{ //still in child process
            printf("child process exit\n");
            exit(0); //child process exit, but sometimes this call seems fail
        }
    }else if(-1 == pid){
        return;
    }else{ //parent process
        int     status = 0;
        int     rv
        pid_t   r_pid = waitpid(pid, &status, WNOHANG);
        if (pid == r_pid) {
            if (WIFEXITED(status) && (0 == WEXITSTATUS(status))) {
                rv = 0;
            } else {
                rv = -1;
            }
        }
        return rv;
    }

Using WNOHANG instead of 0 is surely wrong.使用WNOHANG而不是0肯定是错误的。 If the parent calls waitpid (with WNOHANG ) before the child exits, the child will remain in existence as a zombie waiting to be reaped until the parent exits too.如果父级在子级退出之前调用waitpid (使用WNOHANG ),则子级将作为僵尸继续存在,等待被收割,直到父级退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM