The functions used are mainly to get process ID, create process, process exit, process wait, execute program.
Get process Id:getpid (), Getppid ()
Create process: fork (), Vfork ()
Process exit: Exit ()
Process wait: Wait ()
Execute program: EXEC (), System ()
Getpid function
原型:pid_t getpid(void)头文件:<unistd.h><sys/types.h>功能:返回调用该函数的进程id,且该函数总是运行成功
Getppid function
原型:pid_t getppid(void)头文件:<unistd.h><sys/types.h>功能:返回调用该函数的进程的父进程id,且该函数总是运行成功
Fork function
prototype: pid_t fork (void ) header file:< unistd.h> function: Create a child process. Success returns the child process PID in the parent process, returns 0 in the child process, and fails back-1.
#include <stdio.h> #include <unistd.h> #include <stdlib.h> void Main () {pid_t pid; Pid=fork (); if (Pid==0 ) {printf ( "This was child program,the return is%d\n" ,PID); } else {printf (" This was Father Program,the return is%d\n ", PID); } exit (0 );}
With the fork function, the parent process produces a child process, and both the parent and child processes execute the code after the fork function until the process exits. Because the parent process returns the child process PID, the child process returns 0, so the return value can be used to allow the parent and child processes to perform different functions. The child process prints "This is a child program,the return is 0" at the shell prompt because the operating system pops up the shell prompt after the parent process executes, and then executes the subprocess.
vfork function
原型:pid_t vfork(void)头文件:<unistd.h><sys/types.h>功能:创建一个子进程,并阻塞父进程。成功则在父进程返回子进程PID,在子进程返回0;失败返回-1.
#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <stdlib.h> void Main () {pid_t pid; Pid=vfork (); if (Pid==0 ) {printf ( "This was child program,the return is%d\n" ,PID); } else {printf (" This was Father Program,the return is%d\n ", PID); } exit (0 );}
Executes the child process before the parent process executes.
Both the fork function and the Vfork function are used to create child processes, but the difference is two points:
①fork: Child processes have separate data segments, stacks.
Vfork: The child process shares the data segment with the parent process, the stack.
②fork: The order of execution of the parent and child processes is indeterminate
Vfork: Child processes run first, run after parent process
The parent process can use the return or exit () function to exit the process;
The child process can only exit the process with the exit () function, where exit (0) indicates a graceful exit, exit (1) indicates an exception exit
Wait function
wait(int*status)头文件:<sys/wait.h><sys/types.h>功能:挂起调用它的进程,直到该进程的一个子进程结束。成功则返回结束的那个子进程的ID,失败返回-1参数:status若不为NULL,则用于记录子进程的退出状态
The function that executes the process is exec,exec is a function family, a total of 6: EXECL,EXECV,EXECLE,EXECVE,EXECLP,EXECVP, take execl as an example to explain
execl function
原型:int execl(const char* pathname,const char* arg,...)头文件:<unistd.h>功能:运行可执行文件,并且原进程的代码不再执行,只执行新的代码。成功时不返回,失败时返回-1.参数:pathname为要运行的可执行文件的路径 arg及后面的参数作为可执行文件的参数,并以一个NULL作为参数的结束标志。
#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <sys/types.h>#include <sys/wait.h>voidMain () {pid_t pid; Pid=fork ();if(pid==0) {Execl ("/bin/ls","ls","Home", NULL);printf("This is a child program,the return is%d\n", PID); }Else{Wait (NULL);printf("This was Father Program,the return is%d\n", PID); }Exit(0);}
As can be seen, after using the EXECL function, the original process code is no longer executed, and the new code is executed.
Fork creates a new process that produces a new PID.
exec retains the original process and executes the new code.
System functions
systemcharcommand)头文件:<stdlib.h>功能:执行一条shell指令,该函数会产生子进程
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Linux Multi-process programming