Linux get to the bottom of the fork function call example

Here to explain the use of several fork system call on the Linux operating system. (Regularly updated)

A, atexit function

void cleanup(void) {
    printf("Cleaning up\n");
}
void fork6()
{
    atexit(cleanup);
   printf("L0");
   fork();
   printf("L1"); exit(
0); }

operation result:

 

 

    int (void (* func) ( void)) atexit   function, when the process ends normally would call the atexit func method, whether you write the function func where. 

Second, the recovery of the child

  When a process terminates for any reason, the kernel will not remove it from the system immediately. But it will remain in a state of waiting for the termination of the parent process recycling, which is called "zombie processes." When the parent process recycling process has been terminated, the kernel will exit status of the child process passed to the parent process, and then abandon the process has been terminated. At this point, the process does not exist.

  If a parent process terminates, the kernel will arrange the init process is the father of orphans in its processes.

  In the 64-bit system, the adoptive father of the process is generally not a number , but a number of random process, but this process is cmd "/ lib / systemd / systemd --user   " /

  (Using ps -ef command to view)

 

   1. The end of the first child process, the parent process hangs cycle. Son normal recovery process.

void fork7()
{
    if (fork() == 0) {
    /* Child */
    printf("Terminating Child, PID = %d\n", getpid());
    exit(0);
    } else {
    printf("Running Parent, PID = %d\n", getpid());
    while (1)
        ; /* Infinite loop */
    }
}

   operation result:

 

 

   2. The parent process to end the child process into a loop to hang.

void fork8()
{
    if (fork() == 0) {
    sleep(1);
    /* Child */
    printf("Running Child, PID = %d\n", getpid());
    printf("Running Child, PPID = %d\n",getppid());
    while (1)
        ; /* Infinite loop */
    } else {
    printf("Terminating Parent, PID = %d\n", getpid());
    exit(0);
    }
}

  operation result:

 

   The second code sample, the parent than the child process before the end of the run, this time the child becomes an orphan process, was adopted. Adoptive father for the process described earlier.

  3. Here there is a problem there is some reference to the recovery of the child.

int main () { 

    int PID1, PID2, the PID3, PID4 are, pid5; 

    the printf ( " No. 1 starts the process \ n- " );
     // get a first process PID 
    PID1 = getpid (); 
    the printf ( " the PID1% = D \ n- " , PID1); 

    // Create a second process 
    PID2 = the fork ();
     IF (PID2 == 0 ) 
    { 

        the printf ( " 2 process begins \ n- " ); 
        the printf ( " PID2% = D, the PID the Parent % D = \ n- " , getpid (), getppid ()); 
        the printf ( " end process No. 2 \ n- "); 

        // Exit Exit 
        Exit ( 0 ); 
    } 

    // create a third process 
    PID3 = fork ();
     IF (PID3 == 0 ) 
    { 
        printf ( " No. 3 to start the process of \ the n- " ); 
        printf ( " PID 3 D% =, the Parent D the PID =% \ n- " , getpid (), getppid ()); 

        // Create a fourth process in which the process No. 3 
        PID4 are = the fork ();
         IF (PID4 are == 0 ) 
        { 
            the printf ( " No. 4 to start the process of \ the n- " ); 
            printf ( "4 =% d PID, the Parent PID =% d \ the n- " , getpid (), getppid ()); 

            // SLEEP 3 3 SECOND, waiting for the end of the process he became orphaned 
            SLEEP ( 3 ); 
            printf ( " No. 4 the process ends \ n- " ); 
            Exit ( 0 ); 
        } 

        // inside process 3, the process of creating the first five 
        pid5 = the fork ();
         IF (pid5 == 0 ) 
        { 
            the printf ( " the PID. 5% = D, the Parent =% d PID \ the n- " , getpid (), getppid ()); 
            printf ( " No. 5 process began \ the n- " ); 

            //sleep 3 second wait for the end of the process 3, process their orphaned 
            SLEEP ( 3 ); 
            the printf ( " No. 5 The process ends \ n- " ); 
            Exit ( 0 ); 
        } 
        // wait between the end of the child process that facilitates the observation pid relations 
        printf ( " No. 3 end of the process \ the n- " );                 
        Exit ( 0 ); 
    } 


    // wait for the end of child threads 
    (printf " end process No. 1 \ the n- " );
     return  0 ; 
} 
-------- -------- 
Disclaimer: this article is the original article CSDN bloggers "Zhang Gu Gu", the follow CC 4.0 BY- SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: HTTPS: // blog.csdn.net/Paulhappy/article/details/50984888

  Compile the code using the following command

$ gcc -o process.out processes.c -lpthread

  Results are as follows:

 

   The program's process is as follows:

 

   

Guess you like

Origin www.cnblogs.com/lyw-hunnu/p/11828768.html