当前位置:网站首页>Process knowledge generalization
Process knowledge generalization
2022-07-22 00:51:00 【amireux512】
Catalog
1. Create child process (fork)
2. Get your own process number
One , Process concept
A process is a program with independent functions about a running activity on a data set .
Three states of the process : be ready , perform , Blocking .
Foreground process : Interact with users , It will occupy the terminal . Used to achieve a large number of functions and occupy most of the resources
Background processes : Processes that do not occupy the terminal , Occupy a small amount of resources .
Zombie process , It means that the process has been executed , But its system resources are not fully released .
Orphan process : The child process that the parent process has exited . By grandpa process or 1 Process hosting .
Process group : The parent process and child process are the same process group by default .
pid_t getpgrp(void);// Get group ID
int setpgid(pid_t pid,pid_t pgid);// Used to handle pid The group of this process id Set to pgid.
conversation : The connection between the foreground process group and the background process group , Is a collection of multiple process groups
Realize background operation
a. Create a subprocess , Then exit the parent process . Then the subprocess becomes a background process .
b. ./hello & Background operation , Depend on the terminal
c. nohup ./hello & Background operation , It does not depend on the terminal
Two , Process API
1. Create child process (fork)
pid_t This type is used to represent the process number of the process (PID,PPID), Declared in the header file . Inside the operating system
Each process will be assigned a different ID Number , Used to identify the process .
pid_t fork(void);
Return value : Failure to return -1, At this time, only one process returns .
success ( Created a new child process ).
>0: Indicates that the parent process returns , And the return value at this time is the process number of the child process
=0: It means that the child process returns .
fork Internally it will call clone() The newly created Child process copies address space of parent process ( The child process copies most of the parent process
The data of , Including system data and other data , Except for the process number ).
fork A successful call will produce a child process , The code execution point of the child process is the same as that of the parent process (fork Return place ).
fork One call , There are two returns . Once, the parent process returns , Once, the subprocess returns .
fork It is uncertain who will execute first after success ( Determined by the task scheduling algorithm , Generally, the time slice rotation algorithm ).
vfork The function of fork similar , It just blocks the parent process , That is, after the execution of the subprocess , Before running the parent process .
pid_t vfork(void);
2. Get your own process number
getpid Used to return the process number of its own process
getppid Used to return the process number of the parent process
pid_t getpid(void);
pid_t getppid(void);
3. Process exit
There are three situations when a process exits
1) main The function is finished ==> Normal death
2) Be killed ==> Abnormal death
3) exit/_exit ==> commit suicide
void exit(int status); // Exit after cleaning void _exit(int status); // Exit directly without cleaning
4. wait/waitpid
wait/waitpid Used to wait for the state of a child process to change .
There are three situations when the waiting state changes :
1) Subprocess exit
2) Subprocess aborted by signal ==> Blocked state
3) The child process is awakened by the signal ==> Return to ready state
wait Used to wait for the state of a child process to change .
If the wait when , All child processes have exited , that wait Will return to . Other situations ,wait It will block
Calling process , Until the state of a child process changes . If a child process exits , Its parent process did not call wait, Then the subprocess will become a zombie (Zombie) process ,
wait/waitpid In addition to waiting for the sub process state to change , Another function is to recycle resources ( Mainly refers to PCB( Process control block )), and CPU And memory will be automatically released ).
pid_t wait(int *wstatus);
@wstatus: The pointer . Used to save the exit status of the child process that exits .
We can use the following macros to parse its information and meaning :
WIFEXITED(wstatus) It's true , Indicates that the subprocess exits normally .
WEXITSTATUS(wstatus) Return the return value of the subprocess ( Exit code ),8bits Unsigned integer
WIFSIGNALED(wstatus) It's true , Indicates that the subprocess is terminated by a signal
WTERMSIG(wstatus) Return the signal value of the signal that ends the subprocess
WIFSTOPPED(wstatus) It's true , Indicates that the subprocess is aborted by a signal (stop) Of
WSTOPSIG(wstatus) Return the signal value of the signal that terminates the subprocess
WIFCONTINUED(wstatus) It's true , Indicates that the subprocess is awakened by a signal
Return value :
success ( The state of at least one child process has changed ), Return to the process whose state changed ID
Failure to return -1,errno Set up
waitpid With the function of wait similar , It can only specify which or which child processes to wait for
pid_t waitpid(pid_t pid, int *wstatus, int options);
@pid: Used to specify which child process to wait for or which child process
pid > 0: Indicates waiting for the specified child process
pid == -1: Indicates waiting for any child process
pid < -1: Indicates waiting group ID be equal to pid Any subprocess of the absolute value of
@wstatus: The pointer . Used to save the exit status of the child process that exits .
@options: The way to wait , It's usually 0. When set to WNOHANG Indicates no blocking .
Non blocking mode , If there is no state change of the child process, it will return immediately
Return value :
The process that successfully returned the status change ID. If set WNOHANG Mode and the state of child processes
When it doesn't happen , Then immediately return to 0.
Failure to return -1,errno Set up .
边栏推荐
猜你喜欢
小程序如何使用自定義導航欄
TypeScript(二)
Solve the problem that pictures inserted into word documents become unclear
小程序基础知识
根据返回的图片和坐标,画出在图片上的对应的矩形框
Draw the corresponding rectangular box on the picture according to the returned picture and coordinates
OOM 分析工具 MAT
Use of forkjoin
Draw plate layer
[ar Foundation] ar Foundation Foundation Foundation Foundation
随机推荐
Successfully install pyinstaller (solve the problem of PIP install pyinstaller installation failure)
js获取服务端IP、端口、协议
7月13日直播预告:方舟低代码平台直播来袭
Cloud native and low code platforms make agile enterprises
Directly output JSON file from scratch
Queen n
面试官:你确定 Redis 是单线程的进程吗?
如何将.sql文件导入到mysql中
Applet to upload pictures
11种免费获取SSL证书的方式
06.Octave的介绍、安装与简单使用
Based on tensorflow2.0+ use Bert to obtain Chinese word and sentence vectors and conduct similarity analysis
Data and business: pursuit and achievement under the tide of digital transformation
牛客刷题——剑指offer
使用 uni-app 预览图片并且保存图片
从装配式建筑流行看云原生技术中台价值 (二)
手机app抓取工具手机版selenium—Appium,Mitmdump手机抓包
HCIP 第二天作业 7.17
小程序基础知识
BT.656 interface rate