Create a process
Function Name: Fork
Function prototype: pid_t fork (void);
function function: Create a child process
Header file;<unistd.h>
Return value: Success: The PID of the child process returned in the parent process, 0 failed in the child process: 1
#include <unistd.h>void main () {fork ();p rintf ("program is end\n"); exit (0);} Run Result: Program is end The result analysis: Once the fork function is called, a new process is generated in memory, and the new process is the same as the parent process code, which starts from the statement after the fork.
#include <unistd.h>void main () {pid_t pid;pid = fork ();p rintf ("pid is%d\n", PID); exit (0);} Operating result: PID is 3003 pid is 0
#include <unistd.h>void main () {pid_t pid;pid = fork (), if (PID > 0) {printf ("This is Father process\n"); exit (0);} Else{printf ("This is a child process\n"); exit (0);}} Run Result: This is Father process
Create a process
Function Name: vfork
Function prototype: pid_t vfork (void);
function function: Create a child process and block the parent process
Header file:<sys/types.h> <unistd.h>
Return value: Success: The PID of the child process returned in the parent process, 0 failed in the child process: 1
#include <sys/types.h> #include <unistd.h>void main () {pid_t pid;pid = vfork (); if (pid > 0) {printf ("This is Father Process\n "); exit (0);} Else{printf ("This is a child process\n"); exit (0);}} Run Result: This is the child process, this is father process
Consider the results of the following programs:
#include <stdio.h> #include <unistd.h>void main () {pid_t Pid;int count = 0;pid = Fork () count++;p rintf ("Count =%d\n ", count); exit (0);} Run Result: Count = 1 count = 1
#include <stdio.h> #include <unistd.h>void main () {pid_t Pid;int count = 0;pid = Vfork () count++;p rintf (" Count =%d\n ", count); exit (0);} Run Result: Count = 1 count = 2
Fork Contrast Vfork
1. Fork: Child processes have separate data segments and stacks
Vfork: Child processes share data segments and stacks with parent processes
2. Fork: The order of execution of the parent-child process is indeterminate
Vfork: Child processes run first, run after parent process
Process exit
When you exit normally, the parent process can use return 0, and exit (0), and the child process can only use exit (0);
When exiting abnormally, exit (1) is used;
Process wait
Function Name: Wait
Function prototype: pid_t wait (int *status);
function function: Suspends the process that invokes it until its child process ends
Header file:<sys/types.h> <sys/wait.h>
Return value: Success: Returns the ID of the terminated child process failed: 1
Parameter description: Status record the exit status of the child process
#include <unistd.h> #include <sys/types.h> #include <sys/wait.h>void main () { pid_t pid; PID = fork (); if (PID > 0) { wait (NULL);p rintf ("This is Father process\n"); exit (0); } else {printf ("This is a child process\n"); exit (0);} } Run Result: This is the child process, this is father process
Execute the program
Function name: Execl and System
function prototypes: int execl (const char *pathname,const char *arg ...);
function function: Run executable file
Header file:<unistd.h>
Return value: Success does not return, failure returns
Parameter description: Pathname: The path to the executable file to run ARG: The executable file runs the required parameters, the first one is the file name and ends with NULL
#include <unistd.h> #include <sys/types.h> #include <sys/wait.h>void main () {pid_t pid;pid = fork (); if (PID > 0) {Wait (NULL);p rintf ("This is Father process\n"); exit (0);} Else{execl ("/bin/ls", "ls", "/home/", NULL);p rintf ("This is a child process\n"); exit (0);}} Operating result: Wind The is father process
Attention:
Fork creates a new process that produces a new PID
exec retains the original process, but the code is replaced with new code to execute the new code
Multi-process programming