Fork_wait_waitpid function of _8_ Process Control for Linux system programming

Source: Internet
Author: User

Fork function:

#include <unistd.h>
pid_t fork (void);

Fork is used to create a sub-process;


features :

The fork call returns two times, the child process returns 0, the parent process returns the process ID of the child process,and when the fork returns, both the child process and the parent process begin execution from the next statement of the fork function;

Note :

fork, These two processes share code space, However, the data space is independent of each other, the content in the child process data space is the full copy of the parent process, the instruction pointer is identical, and the child process has the location where the parent process is currently running (the program counter PC value of the two process is the same, that is, the child process starts at the fork return), But a little different, if Fork succeeds, the return value of fork in the subprocess is 0, The return value of the fork in the parent process is the process number of the child process, and if the fork is unsuccessful, the parent process returns an error.
    can imagine that 2 processes have been running at the same time, and Unison, after the fork, they do different jobs, that is, bifurcation. This is the reason why fork is called fork. As for the child process and the parent process which first executes, this is indeterminate, depending on the operating system. If you use vfork, you can guarantee that the child process runs before the stepfather process runs.

As we know in the above note, the content in the child process data space is a full copy of the parent process, meaning that the operation of the data in the child process does not affect the parent process, and the following example illustrates this feature:

#include <stdio.h> #include <unistd.h>int main () {    int i = ten;    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;}
Operation 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

........


It is also important to note that if a file is opened in the parent process, the kernel returns a file descriptor to the application, The File table entry for the child process and the parent process's file descriptor is shared, which means that the child process's read and write to the file directly affects the parent process's file offset (and vice versa).

The process called Fd2 = DUP (FD1) produces a new fd2 that points to the file table entry and FD1 points to the file table entry that is the same;



Wait and Waitpid functions:

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

If there are no child processes, the wait error is returned;

There is a child process, the child process is running, then block, waiting for the child process to end;

If the child process has ended, it gets the information of the end child process and returns;

Why use the wait and WAITPID functions?

If the parent process ends first, the child process becomes an orphan process, at which point the init process (ID 1) becomes the new parent process of the child process;

If the child process ends first, the child process becomes a zombie process! The zombie process itself does not occupy CPU resources, but it occupies the process table entries, if there are many zombie processes, so many normal processes can not be registered into the process table, so we have to recycle the zombie process, using wait and waitpid;

#include <stdio.h> #include <unistd.h>int main () {    int i = ten;    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, the parent process 2 seconds after the normal end, the child process of the parent process will become the INIT process, can be viewed with the Ps-l command, reached the running time of 7 seconds, the child process normal end, if you use Wait, Then wait causes the parent process to wait for the child process to end and the child process to exit together, avoiding the generation of the zombie process.




Fork_wait_waitpid function for Linux system programming _8_ Process Control

Related Article

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.