Multi-process programming
First, function learning
1.1 Creating a Process fork
1.1.1 Function prototype
pid_t fork (void);
1.1.2 Function function
Create a child process
1.1.3 Owning header file
<unistd.h>
1.1.4 return value
Success: The parent process returned a successfully created child process PID subprocess returned 0
Failed: Parent Process returned
1.1.5 parameter Description
No
1.2 Creating a process vfork
1.2.1 Function prototype
pid_t vfork ();
1.2.2 Function function
Create a child process and block the parent process
1.2.3 Owning header file
<sys/types.h> <unistd.h>
1.2.4 return value
Success: The parent process returned a successfully created child process PID subprocess returned 0
Failed: Parent process returned-1
1.2.5 Parameter Description
No
Fork and vfork Contrast:
Both fork and vfork can be used to create child processes, but there are also the following differences:
1. Fork: The child process has separate data segments, stacks. Vfork: The child process shares the data segment with the parent process, the stack.
2. Fork: The order of execution of the parent and child processes is indeterminate vfork: Child processes run first, run after the parent process
1.3 Process waiting wait
1.3.1 Function prototype
pid_t Wait (int* status);
1.3.2 Function function
Suspends the process that invokes it until its child process ends
1.3.3 Owning header file
<sys/types.h> <sys/wait.h>
1.3.4 return value
Success: Returns the end of the child process ID
Failed:-1
1.3.5 parameter Description
Status: Record child process exit status
1.4 Execution Program function name execl
1.4.1 Function prototype
int execl (const char* pathname,const char*arg,...)
1.4.2 function function
Run an executable file
1.4.3 Owning header file
<unsitd.h>
1.4.4 return value
Success: Do not return
Failed: Return
1.4.5 parameter Description
Pathname: The path to the executable file to run
ARG: The parameters required to run the executable file
1.5 terminating the process Exit equals _exit
1.5.1 function prototype
void _exit (int status)
1.5.2 function function
Terminates the process being called
1.5.3 Owning header file
<unistd.h>
1.5.4 return value
No
1.5.5 parameter Description
Status is used as a city exit status returned to the parent process
Parent Process exit: can be return 0 and exit
Child process exit: only exit
Multi-process programming, Wang Ming Learning Learn