Fork_wait_waitpid function and waitpid function for process control in Linux

Source: Internet
Author: User

Fork_wait_waitpid function and waitpid function for process control in Linux

Fork function:

# Include <unistd. h>
Pid_t fork (void );

Fork is used to create a child process;


Features:

After fork is called, it returns two times. The child process returns 0, and the parent process returns the child process ID. After fork returns, both the child process and the parent process start to execute from the next statement of the fork function;

Note::

After fork, the operating system will copy a child process that is exactly the same as the parent process. Although it is a parent-child relationship, in the operating system's view, they are more like siblings, the two processes share the code space, but the data space is independent of each other. The content in the data space of the child process is the complete copy of the parent process, and the instruction pointers are identical, the sub-process has the current running location of the parent process (the program counters of the two processes have the same pc value, that is, the sub-process is executed from the fork return), but it is a little different, if fork is successful, the return value of fork in the child process is 0, and the return value of fork in the parent process is the process number of the child process. If fork is not successful, the parent process returns an error.
As you can imagine, the two processes have been running at the same time, and the steps are consistent. After fork, they perform different jobs separately, that is, splitting. This is why fork is called fork. It is uncertain which sub-process and parent process should be executed first, depending on the operating system. If vfork is used, the parent process is running after the sub-process is completed.

We know that the content in the data space of the child process is a complete copy of the parent process, that is, the operations on data in the child process will not affect the parent process, the following example illustrates this feature:

#include <stdio.h>#include <unistd.h>int main(){    int i = 10;    pid_t pid;    printf("Father's pid:%d\n", getpid());    pid = fork();    if(pid < 0)    {        perror("fork failure!");        return -1;    }    else if(pid == 0)    {        while(1)        {            i++;            printf("Child's i = %d\n", i);            sleep(1);        }    }    else    {        printf("Child's pis:%d\n", pid);        while(1)        {            printf("Father's i = %d\n", i);            sleep(1);        }        sleep(1);    }    return 0;}
Running result:

Father's pid: 12148
Child's pis: 12149
Father's I = 10
Child's I = 11
Father's I = 10
Child's I = 12
Father's I = 10
Child's I = 13

........


Note that if a file is opened in the parent process, the kernel returns a file descriptor to the application, the file table items corresponding to the file descriptors of the child process and the parent process are shared, which means that the read and write operations on the files of the child process directly affect the file displacement of the parent process (likewise ).

The file table items pointed to by the new fd2 generated by calling fd2 = dup (fd1) in the process are the same as the file table items pointed to by fd1;



Wait and waitpid functions:

Wait and waitpid are used to wait for the child process to end;

If no sub-process exists, wait returns an error;

If a child process is running, it is blocked and waits until the child process ends;

If the child process has ended, get the information of the child process that has ended and return;

Why use the wait and waitpid functions?

If the parent process ends first, the child process becomes an orphan process. At this time, the init process (id 1) will become a new parent process of the child process;

If the child process ends first, the child process will become a zombie process! The zombie process does not occupy CPU resources, but it occupies the progress table. If there are many zombie processes, many normal processes cannot register in the progress table. Therefore, we must recycle dead processes and use wait and waitpid;

#include <stdio.h>#include <unistd.h>int main(){    int i = 10;    pid_t pid;    int status;    printf("Father's pid:%d\n", getpid());    pid = fork();    if(pid < 0)    {        perror("fork failure!");        return -1;    }    else if(pid == 0)    {        i++;        printf("Child's i = %d\n", i);        sleep(7);    }    else    {        printf("Child's pis:%d\n", pid);        printf("Father's i = %d\n", i);        sleep(2);    //    wait(&status);    }    return 0;}

If the above program does not use the wait function to recycle the child process, after the parent process ends normally for 2 seconds, the child process's parent process will change to the init process. You can run the ps-l command to view it, after the running time reaches 7 seconds, the child process ends normally. If wait is used, wait will cause the parent process to wait until the child process ends and then exit together after the child process ends, this avoids zombie processes.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.