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
- <span style="Font-size:small;" > #include <sys/types.h>
- #include <sys/wait.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <math.h>
- int main (void) {
- pid_t child,child2;
- int status;
- printf ("This would demostrate how toget 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 to status is zero\n",
- Child);
- Else if (wifexited (status))
- printf ("child%ld terminated normally return to status is%d\n",
- Child, Wexitstatus (status));
- Else if (wifsignaled (status));
- printf ("Child%ld terminated due to signal%d Znot caught\n",
- Child, Wtermsig (status));
- GetChar ();
- return (exit_success);
- }
- </span>
Linux C Learning notes----process creation (FORK,WAIT,WAITPID)