当前位置:网站首页>Process (below): process control, termination, waiting, replacement
Process (below): process control, termination, waiting, replacement
2022-08-02 05:00:00 【RNGWGzZs】

--------------“Good things always come as promised”
(1)进程控制:
① 如何理解fork有两个返回值?
父:子 ==1:N; The parent process does not need to be represented,子进程需要标识.
Multiple child processes perform different tasks,父进程需要通过id Distinguish subprocesses.

因此调用fork时候,The kernel will do:
1.分配新的内存块 和 内核数据 给子进程.
2.The code for the parent process Data is copied to the child process.
3.and add the child process to In the list of system process calls.
4.fork返回,and perform scheduling.
②写时拷贝:
通常情况下,父子代码共享,数据也是(Physical memory is the same).但是when one party tries to write,In order to ensure the independence of each process,此时会发生写时拷贝.


为什么要写时拷贝?
①保证进程的独立性.
Why not separate when created?
Because the child process does not necessarily need all the data of the parent process.
The child process does not necessarily use it immediately(按需分配);因此进行延时分配,本质是Efficient use of memory space.
fork调用失败原因?
1.Too many programs in the system
2.User process exceeded limit(OS进行的限制).
(2)进程终止:
①进程退出码:
mainTo whom the function returns the value?操作系统
main函数也是函数,So it must be called.The return value will be returned to操作系统.
为什么要有返回值? ------------运行加载,完成工作,Need to know the degree of completion.
途径:
echo $?
退出码0 、!0:
#include<string.h>
strerror(size_t i); //错误码

each error code,have corresponding error meanings.But it's all man-made.
②进程退出:
1.exit:进程终止;and release the buffer's data
_exit:进程终止,Do not do the rest of the work


2.异常退出:At this point the exit code has no meaning anymore
3.进程终止,what the operating system will do? Must go to management!
释放内存 数据结构 Dispatch queue removal.
(3)进程等待:父进程等待
①Waiting for necessity:回收子进程、Get its exit information.
②进程等待方法:
1.wait:Waiting for the received child process to returnpid
#include<sys/wait.h>
pid_t wait(int* status)

小结:父进程通过wait Read child process information,The zombie state will also change automatically.
在子进程运行期间(wait),父进程在做什么? 什么也没做------> 阻塞等待!!!
2.waitpid:Specifies the waiting process
//Record the status of the child process
waitpid(pid_id pid,int* status,int options)
//List of monitoring scripts
while :; do ps ajx|head -1 && ps ajx | grep myproc |grep -v 'grep';echo "#########################" ; sleep 1 ;done;等待成功!=子进程运行成功.
status:退出结果 ---------->查看子进程 运行状态



注:进程异常终止,Essentially because of the received signal.Its exit code also has no value.

An exception occurred in this process,收到信号,此时Exit codes don't make any sense either.
实际中一般用 Macro detection:
WIFEXITED(status) //进程信号 0 与 !0
WEXITSTATUS(status) //进程退出码Create and wait for multiple processes:


补充:阻塞与非阻塞
//若pid没有返回 That is, the child process is not executed,No more waiting for child processes 返回0 不予等待
// 若正常退出 则返回子进程pid
pid_t ret=waitpid(id,&status,WNOHANG);

So for the parent process,You need to keep waiting for the child process to finish executing.

(4)进程替换:
The child process executes the new process.
进程替换函数:
| 函数 | .... |
int execl(const char *path, const char *arg, ...); | 列表 |
int execlp(const char *file, const char *arg, ...); | 列表+默认路径 |
int execle(const char *path, const char *arg, ...,char *const envp[]); | 列表+自定义路径 |
int execv(const char *path, char *const argv[]); | 数组 |
int execvp(const char *file, char *const argv[]); | 数组+默认路径 |
int execve(const char *path, char *const argv[], char *const envp[]); | 数组+自定义路径 |

execl Its essence is to let the program load to disk.
1.当前进程 再进行 程序替换,Whether a new process was created?
这里根本没有 创建新进程.Just replace the disk code. 进程!=可执行程序.
2.被替换后,后续的代码,can be read?
答案是不能.

3. 函数调用失败,Programs will not be affected.But if the call succeeds, it will not return!

4.Operation with independence between parent and child will not be affected by program replacement functions.
Understanding of functional interfaces:

只需要理解 数组、列表 The difference in the parameters of the execution program:
execv为例:

理解默认路径、自定义路径:
默认路径:

自定义路径(execle):

打印出null;

It will not print out at this time This is undefined(MAVAL)的环境变量.


The related content of the process is also finally completed:Hope it is useful to you reading.
祝你好运~
边栏推荐
- Case | industrial iot solutions, steel mills high-performance security for wisdom
- 【LeetCode】设计链表
- Flame sensor connected with Arduino
- USB2.0一致性测试方法_高速示波器
- 【操作系统】线程安全保护机制
- 【科普贴】I2C接口详解——偏硬件解析
- GM8284DD,GM8285C,GM8913,GM8914,GM8905C,GM8906C,国腾振芯LVDS类芯片
- rosdep update失败解决办法(亲测有效)
- 将ORCAD原理图导入allegro中进行PCB设计
- 引擎开发日志:OpenGL资源多线程加载
猜你喜欢
随机推荐
最第k大的数的一般性问题
哈希表解题方法
MAC安装Mysql超详细完整教程
振芯GM7123C:功能RGB转VGA芯片方案简介
Industry where edge gateway strong?
【TCS3200 color sensor and Arduino realize color recognition】
LL(1)文法 :解决 if-else/if-else 产生式二义性问题
工业边缘网关究竟强大在哪里?
OneNET Studio与IoT Studio对比分析
开源代码交叉编译操作流程及遇到的问题解决(lightdm)
汇编语言跳转指令总结
USB2.0一致性测试方法_高速示波器
LT8918L LVDS转MIPI芯片技术支持资料
同时求最大值与最小值(看似简单却值得思考~)
IoT solution
所有子字符串中的元音 —— LeetCode - 2063
【Popular Science Post】UART Interface Communication Protocol
与TI的lvds芯片兼容-GM8284DD,GM8285C,GM8913,GM8914,GM8905C,GM8906C,国腾振芯LVDS类芯片,
【科普贴】MDIO接口详解
云服务器web项目部署详解




![[Arduino connected to GP2Y1014AU0F dust sensor]](/img/b4/c32dcd32bf5b9e8596af406c9177a2.png)




