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 start from the next statement of the fork function;

Note :

fork, The two processes share the code space, but the data space is independent of each other, and the content in the child process data space is the full copy of the parent process. The instruction pointer is exactly the same. The child process owns the location where the parent process is currently executing (two process of program counter PC value same, i.e., The child process starts at the fork return, but a little different. , assuming fork succeeds, 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, assuming the fork is unsuccessful, the parent process returns an error.
    can imagine that 2 processes have been executed 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 not deterministic, depending on the operating system. assuming that vfork is used, it is possible to ensure that the child process executes before the stepfather process executes.

We know from the above note. The content in the child process data space is the full copy of the parent process. That is, the operation of the data in the child process does not affect the parent process, the following sample can illustrate 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;}
Execution 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

........


Another point to note is that assuming the parent process opens the file, that is, the kernel returns a file descriptor to the application, The corresponding 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;

Assume there are no child processes. The wait error returns;

There are child processes. The child process is executing and is blocked. Wait for the child process to end;

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

Why use the wait and WAITPID functions?

Assume that the parent process ends first. The child process becomes the orphan process, at which point the init process (ID 1) becomes the new parent process of the child process;

Assuming the child process ends first, the child process becomes a zombie process!

The zombie process itself does not occupy CPU resources. However, it occupies the process table, assuming that there are very many zombie processes, so many normal processes will not be able to register into the process table; We will need to recycle the zombie process, using wait and waitpid;

   Waitpid () Temporarily stops the process from running until a signal arrives or the child process ends.

assuming that the child process has ended when you call Wait (), wait () returns the child process end status value immediately. the end state value of the child process is returned by the parameter status, and the process identifier of the child process returns quickly.

Assuming the end-of-state value is not an intention, the parameter status can be set to NULL. the parameter PID is the child process identifier to wait for, other numeric meanings such as the following: PID <-1 wait for the process group identifier to be the PID absolute value regardless of the sub-process. pid =-1 waits for whatever subprocess, equivalent to wait ().

pid = 0 waits for the process group identifier to be the same as the current process, regardless of the sub-process.

pid > 0 Waits no matter what the sub-process identification code is for the child process of the PID.


The parameter option can be combined for 0 or less: Wnohang assumes that no matter what has ended, the child process returns immediately, not waiting. Wuntraced assumes that the child process enters a paused operation and returns immediately, but the end state is ignored.


the end state of the child process is returned and stored in status, with a few macros at the bottom to determine the end condition: wifexited (status) assumes that the child process normally ends with a value other than 0.

Wexitstatus (status) Gets the end code returned by the child process exit (), usually using wifexited to infer whether the ability to use this macro ends properly.

wifsignaled (status) assumes that this macro value is true when the child process is terminated by a signal wtermsig (status) Gets the signal code that the child process has aborted because of the signal, and usually uses wifsignaled to infer it before using the macro.

wifstopped (status) assumes that the child process is in a paused state when the macro value is true.

This is usually only the case when using wuntraced.

Wstopsig (status) obtains the signal code that causes the child process to pause, usually using wifstopped to infer the macro before use. Assuming a successful run, the child process identifier (PID) is returned, assuming that an error occurs and returns a return value of 1. The reason for failure is stored in errno.


Examples:

#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;}

The above program assumes that the wait function is not used to reclaim the child process, and the parent process is 2 seconds after the normal end. The parent process of the child process becomes the init process. Can be viewed with the ps-l command, after 7 seconds of execution time, the child process ends normally; If wait is used, wait causes the parent process to wait for the child process to end, and the child process exits together, avoiding the generation of the zombie process.




Fork_wait_waitpid function for Linux system programming _8_ Process Control

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.