Why do processes I fork get systemd as their parent?

前端 未结 3 1394
死守一世寂寞
死守一世寂寞 2021-01-13 05:15

I am learning fork() in Linux, and here is my program:

  1 #include 
  2 #include 
  3 #include 
  4 int main(         


        
相关标签:
3条回答
  • 2021-01-13 05:36

    The "inconsistency" you observed is because sometimes, the parent process(es) exited before their child process(es) terminated. So, these child processes become "orphans" as their parent processes are not waiting for them. As a result, they are "re-parented" to the init process. While traditionally the process id of the "init" process is 1, it's not always true. POSIX leaves it as implementation-defined:

    The parent process ID of all of the existing child processes and zombie processes of the calling process shall be set to the process ID of an implementation-defined system process. That is, these processes shall be inherited by a special system process.

    Thus you see a particular PID as the parent (1599 in your example), which happens to be "init" process equivalent on your Linux. The systemd is an init variant used in Debian Linux distributions - which follows a slightly more complicated implementation.

    In essense, what you observed is pretty normal. Ideally, you should reap all the processes in order to avoid zombie processes.

    0 讨论(0)
  • 2021-01-13 05:45

    It is because the parent process terminates first. In Linux there are no ophan processes. They are assigned to the init process.

    If you want to control your processes so that child process terminates first, make parent process wait. Using wait() sys_call.

    Example:
    
       #include <stdio.h>
       #include <stdlib.h>
       #include <unistd.h>
       int main(void)
       {
            int pid;
            int pid2;
            pid = fork();
            if(pid < 0){
                     exit(1);
            }
            if(pid == 0){ // child process
                pid2 = fork()
                if (pid2 < 0)
                    exit(1);
                if (pid2 == 0)
                {
                    printf("pid:%dppid:%d\n",getpid(),getppid());
                    exit(0);
                }
                wait();
                printf("pid:%d ppid:%d\n",getpid(),getppid());
                exit(0);
            }
            else{         // parent process
               wait();
               printf("parent pid:%d ppid:%d\n",getpid(),getppid());
               exit(0);
            }       
            return 0;            
      }
    

    systemd is an init system used in Linux distributions to bootstrap the user space and manage all processes subsequently

    0 讨论(0)
  • 2021-01-13 06:03

    I suppose that, sometimes, a race condition happens, and the parent dies before the child. Hence, the child becomes children from init process. In your case, that must be systemd.

    Anyway, be advised that running fork(); fork(); will produce 4 processes, which is (probably) not what you intend. Use control structure as you did with the first one to have fine control on the behaviour of your program.

    0 讨论(0)
提交回复
热议问题