Linux Learning-process creation (FORK,WAIT,WAITPID) __linux

Source: Internet
Author: User

1.pid_t Fork ();
(1) When a process calls fork, a child process is created. This subprocess differs from the parent process only by his process ID and the parent process ID, and the rest are the same. Just like the character process clone (clone) itself.
(2) to differentiate between parent and child processes, we must track the return value of fork. The fork return value plays an important role when the fork fails (not enough memory or the user's maximum number of processes has arrived) Fork returns 1. For the parent process Fork returns the ID of the subprocess and returns 0 for the fork subprocess. I
is based on this return value to distinguish between the parent-child process.
(3) Once the subprocess is created, the parent-child process continues to execute from the fork, competing with the resources of the system. Sometimes we want the child process to continue, and the parent process to block until the child process completes the task. At this time we can call wait or waitpid system call.

Vfork (to create a new process)
Related functions: Wait,execve
Table header file: #include <unistd.h>
Definition function: pid_t vfork (void);

Function Description: Vfork () produces a new subprocess that replicates the parent process's data and stack space and inherits the parent process's user code, group code, environment variables, open file code, working directory, and resource limits. Linux uses Copy-on-write (COW) technology, and only if one of the processes tries to modify the space to replicate does it do a real copy, because the inherited information is replicated and does not mean the same memory space, so the child processes are not synchronized with the changes to these variables and the parent process. In addition, child processes do not inherit file locks and unhandled signals from the parent process. Note that Linux does not guarantee that a child process executes or executes later than the parent process, so it is necessary to be aware of deadlocks or competitive conditions when writing programs.

return value: If Vfork () succeeds, the parent process returns the newly established subprocess Code (PID) and returns 0 in the newly established subprocess. If Vfork fails, return 1 directly, and the reason for failure is in errno.

Error code: Eagain not enough memory. Enomem not enough memory to configure the core required data structure space.

Example:

#include <stdio.h>  
#include <unistd.h>  

void Main ()  
{  
    int i;  
    printf ("Hello,%d\n", Getpid ());  
    i=2;  
    Fork ();  
    printf ("var%d in%d\n", I, Getpid ());  
}  

Execution results:
Hello, 2808
var 2 in 2808
var 2 in 2809

Wait (waits for child process to break or end)

Table header File

#include <sys/types.h>
#include <sys/wait.h>

Definition function: pid_t wait (int * status);

Function Description: Wait () will temporarily stop the execution of the current process until a signal arrives or the child process ends. If the child process has ended when the wait () is invoked, wait () returns the child process end state value immediately. The end state value of the subprocess is returned by the parameter status, and the process identifier for the child process is returned as soon as

Parameter: status can be set to null. The end status value of the child process is referred to Waitpid ().

Return value: Returns the child process identification Number (PID) If the execution succeeds, or returns 1 if an error occurs. The reason for the failure exists in errno.

waitpid (wait for child process to break or end)

Table header file:

#include <sys/types.h>
#include <sys/wait.h>

Definition function: pid_t waitpid (pid_t pid,int * status,int options);

Function Description:
Waitpid () Temporarily stops execution of the current process until a signal arrives or the child process ends. If the child process has ended when the wait () is invoked, wait () returns the child process end state value immediately. The end state value of the subprocess is returned by the parameter status, and the process identifier for the child process is returned as soon as it is. If you do not want to end the state value, the parameter status can be set to null. Parameter PID is the child process identifier to wait for, and other numerical meanings are as follows:
Pid<-1 waits for the process group identifier to be any child process of the PID absolute value.
Pid=-1 waits for any child process, equivalent to wait ().
Pid=0 waits for the process group identifier to be any child process that is the same as the current process.
Pid>0 waits for any child process to recognize the code as PID.
Parameter option can be a combination of 0 or below
Wnohang returns immediately if there are no child processes that have ended, and does not wait.
Wuntraced returns immediately if the child process enters a suspend execution, but the end state is ignored.
The end state of the subprocess is returned and stored in status, with several macros below to determine the end condition
Wifexited (status) is a value other than 0 if the child process ends normally.
Wexitstatus (status) Gets the end code returned by the subprocess exit (), typically using the wifexited to determine whether the macro will be used before it ends normally.
wifsignaled (status) This macro value is true if the child process ends because of a signal
Wtermsig (status) to get the child process because the signal to abort the signal code, the general will use the wifsignaled to judge before using this macro.
wifstopped (status) This macro value is true if the child process is in a paused execution condition. This is generally the case only if you are using wuntraced.
Wstopsig (status) obtains the signal code that raises the child process pause, usually uses wifstopped to judge before using this macro.

Return value: Returns the child process identification Number (PID) If the execution succeeds, or returns 1 if an error occurs. The reason for failure is in errno

Example: #include <unistd.h>

<span style= "Font-size:small;" > #include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #i  
    Nclude <errno.h> #include <math.h> int main (void) {pid_t child,child2;  
    int status;  
    printf ("This'll demostrate how-to-get-child status\n");  
        if (Child = fork ()) = = 1) {printf ("Fork Error:%s\n", Strerror (errno));  
    Exit (1);  
        else if (child = = 0) {child2 = fork ();  
        if (child2 = = 0) printf ("I am The child2:%ld\n", Getpid ());  
        int i;  
        printf ("I am The child:%ld\n", Getpid ());  
        for (i = 0; i < 1000000; i++) sin (i);  
        i = 5;  
        printf ("I exit with%d\n", I);  
    Exit (i);  
    while ((Child = Waitpid (Getpid (), &status,0)) = = 1) & (errno = = eintr));  
    if (child = = 1) printf ("Wait error:%s\n", Strerror (errno)); else if (!status) printf ("Child%LD terminated normally return status are zero\n ", child); else if (wifexited (status) printf ("Child%ld terminated normally return to status is%d\n", child, W  
    Exitstatus (status));  
    else if (wifsignaled (status));  
    printf ("Child%ld terminated due to signal%d znot caught\n", Child, Wtermsig (status));  
    GetChar ();  
return (exit_success);   } </span>

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.