Linux C Learning notes----process creation (FORK,WAIT,WAITPID)

Source: Internet
Author: User

1.pid_t Fork ();

(1) When a process calls fork, the system creates a child process. This child process differs from the parent process in that it has only his process ID and parent process ID, and the others are the same. It's like a process clone (clone) itself.

(2) in order to distinguish between parent and child processes, we must track the return value of the fork. Fork returns a value of 1 when the fork is lost (out of memory or the maximum number of processes the user has reached), otherwise the return values of the forked are important. For the parent process Fork returns the ID of the child process, and 0 for the fork child process. I

They are based on the return value to differentiate the parent-child process.

(3) Once a child process is created, the parent-child process continues to execute from the fork, competing for the resources of the system. Sometimes we want the child process to continue, and the parent process blocks straight

To the child process to complete the task. We can call wait or waitpid system call at this time.

Vfork (Building a new process)

Related functions Wait,execve
Table header File #include <unistd.h>
Defining functions pid_t vfork (void);
Function description Vfork () produces a new subprocess whose child processes replicate the data and stack space of the parent process and inherit the parent process's user code, group code, environment variables, open file code, working directory, resource limits, and so on. Linux uses Copy-on-write (COW) technology to make a real copy action only if one of the processes attempts to modify the space to be copied, because the inherited information is copied, not the same memory space, so the child process's modifications to these variables are not synchronized with 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 child processes will be executed or executed later than the parent process, so be aware when writing programs
The occurrence of deadlock or race conditions.
return value If Vfork () succeeds, the parent process returns the newly established child Process Code (PID), and 0 is returned in the newly created child process. If vfork fails, it returns 1 directly, and the reason for failure is in errno.
Error code Eagain Insufficient memory. Enomem There is not enough memory to configure the data structure space required by the core.
Example #include <unistd.h>
Main ()
{
if (vfork () = =0)
{
printf ("This was the child process\n");
}else{
printf ("This is the parent process\n");
}
}
Perform This is the parent process
The child process


Wait (waits for the child process to break or end)
Table header File #include <sys/types.h>
#include <sys/wait.h>
Defining functions pid_t Wait (int * status);
Function description Wait () temporarily stops execution of the current process until a signal arrives or the child process ends. If the child process has ended when you call Wait (), then 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 identification code of the child process is returned quickly. If the end state value is not
Parameters Status can be set to null. Please refer to Waitpid () for the end status value of the child process.
return value If the execution succeeds, it returns the child process identifier (PID), and returns 1 if an error occurs. The reason for failure is stored in errno.

Waitpid (Waiting for the child process to break or end)

Table header File #include <sys/types.h>
#include <sys/wait.h>
Defining functions pid_t waitpid (pid_t pid,int * status,int options);
Function description Waitpid () 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 you call Wait (), then 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 identification code of the child process is returned quickly. The parameter status can be set to NULL if the end state value is not the intention. Parameter PID is to wait for the child process identification code, the other numerical significance is as follows:
Pid<-1 waits for the process group identifier to be the PID absolute value of any child processes.
Pid=-1 waits for any child process, equivalent to wait ().
Pid=0 waits for the process group identifier to be the same as the current process for any child processes.
Pid>0 waits for any child process identification code to be a child of PID.
Parameter option can be combination of 0 or below
Wnohang If there are no completed sub-processes then return immediately, not to wait.
wuntraced If a child process enters a paused execution, it returns immediately, but the end state is ignored.
The end state of the child process is returned and stored in status, with several macros at the bottom to determine the end condition
Wifexited (status) is a non-0 value if the child process ends normally.
Wexitstatus (status) Gets the end code returned by the child process exit (), typically using wifexited to determine whether the macro ends properly before it can be used.
wifsignaled (status) This macro value is true if the child process ends because of a signal
Wtermsig (status) Gets the signal code that the child process aborts because of the signal, generally uses 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) Gets the signal code that causes the child process to pause, usually using wifstopped to judge before the macro is used.
return value If the execution succeeds, it returns the child process identifier (PID), and returns 1 if an error occurs. The reason for failure is stored in errno.

Example: #include <unistd.h>

C code  
    1. <span style="Font-size:small;" > #include <sys/types.h>
    2. #include <sys/wait.h>
    3. #include <stdio.h>
    4. #include <stdlib.h>
    5. #include <errno.h>
    6. #include <math.h>
    7. int main (void) {
    8. pid_t child,child2;
    9. int status;
    10. printf ("This would demostrate how toget Child status\n");
    11. if ((Child = fork ()) = =-1) {
    12. printf ("Fork Error:%s\n", Strerror (errno));
    13. Exit (1);
    14. } Else if (child = = 0) {
    15. Child2 = fork ();
    16. if (child2 = = 0)
    17. printf ("I am The child2:%ld\n", Getpid ());
    18. int i;
    19. printf ("I am The child:%ld\n", Getpid ());
    20. For (i = 0; i < 1000000; i++) sin (i);
    21. i = 5;
    22. printf ("I exit with%d\n", I);
    23. Exit (i);
    24. }
    25. while (((Child = Waitpid (Getpid (), &status,0) = =-1) & (errno = = eintr));
    26. if (Child = =-1)
    27. printf ("Wait error:%s\n", Strerror (errno));
    28. Else if (!status)
    29. printf ("child%ld terminated normally return to status is zero\n",
    30. Child);
    31. Else if (wifexited (status))
    32. printf ("child%ld terminated normally return to status is%d\n",
    33. Child, Wexitstatus (status));
    34. Else if (wifsignaled (status));
    35. printf ("Child%ld terminated due to signal%d Znot caught\n",
    36. Child, Wtermsig (status));
    37. GetChar ();
    38. return (exit_success);
    39. }
    40. </span>

Linux C Learning notes----process creation (FORK,WAIT,WAITPID)

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.