惠州网站建设怎么样,公众号怎么做网站,小程序开发教程个人,自然堂网站建设平台分析目录 一、进程终止#xff0c;OS做了什么#xff1f;
二、进程终止的常见方式
1、代码跑完#xff0c;结果正确
2、代码跑完#xff0c;结果不正确
补充
(1)、main函数的返回值的意义是什么#xff1f;
(2)、return 0的含义是什么#xff1f;
(3)、退出码是什么和…目录 一、进程终止OS做了什么
二、进程终止的常见方式
1、代码跑完结果正确
2、代码跑完结果不正确
补充
(1)、main函数的返回值的意义是什么
(2)、return 0的含义是什么
(3)、退出码是什么和sterror认识
(4)、如何获取退出码
3、代码没有跑完程序崩溃
三、如何用代码终止一个进程
1、return语句
2、exit()函数
四、知识补充
1、return和exit()区别
2、exit()和_exit()区别
3、区别示意图
五、缓冲区相关知识
1、库函数和系统调用接口 一、进程终止OS做了什么
创建进程不管是fork命令行./或者双击都会变成进程OS要管理这些进程要创建进程对应的内核数据结构task_struct还要为该进程创建对应的地址空间mm_struct还要为该进程创建页表构建映射关系并且还要将该进程对应的代码和数据加载到内存。
因此进程终止时OS需要释放进程申请的相关内核数据结构和对应的数据和代码本质就是释放系统资源。
二、进程终止的常见方式
1、代码跑完结果正确
#includestdio.h
#includeunistd.h26 int main()27 {28 printf(pid: %d,ppid: %d\n,getpid(),getppid());29 return 0;30 }
2、代码跑完结果不正确 1 #includestdio.h2 #includeunistd.h3 4 5 6 int sum(int top)7 {8 int s0;9 for (int i0;itop;i) 10 {11 si;12 }13 return s;14 }15 int main()16 {17 int ret0;18 int ressum(100);19 if(res!5050)20 {21 //如果运行的代码不正确 return 122 ret1;23 }24 return ret;25 }
[hxVM-24-7-centos 20231203-进程终止]$ make
gcc -stdc99 -o myproc myproc.c
[hxVM-24-7-centos 20231203-进程终止]$ ./myproc
[hxVM-24-7-centos 20231203-进程终止]$ echo $?
1补充 (1)、main函数的返回值的意义是什么
返回给上一级进程父进程或者bash用来评判该进程执行结果用的
(2)、return 0的含义是什么
0是退出码的一种代表运行成功代码对或不对用退出码判定。
非0标识的是运行结果不正确非0值有无数个不同的非0值可以标识不同的错误原因方便在进程运行结束后结果不正确时方便定位错误的原因。
(3)、退出码是什么和sterror认识
退出码是计算机为了方便返回结果设定的我们并不清楚返回的1、2、3、4是什么意思所以需要做一个将对应错误码或退出码转化为字符串描述的方案 .
strerror(number)将状态码或退出码转换成字符串描述。 1 #includestdio.h2 #includeunistd.h3 #includestring.h4 //strerror(number)将状态码或退出码转换成字符串描述。5 int main()6 {7 for(int number0;number150;number)8 {9 //查看number对应的错误原因 10 printf(%d: %s\n,number,strerror(number));11 }12 return 0;13 }14
[hxVM-24-7-centos 20231203-进程终止]$ ls abcdef
ls: cannot access abcdef: No such file or directory
[hxVM-24-7-centos 20231203-进程终止]$ echo $?
2
[hxVM-24-7-centos 20231203-进程终止]$ kill -9 11111
-bash: kill: (11111) - Operation not permitted
[hxVM-24-7-centos 20231203-进程终止]$ echo $?
1
[hxVM-24-7-centos 20231203-进程终止]$ ./myproc
0: Success //成功
1: Operation not permitted //权限不被运行
2: No such file or directory //没有此文件或目录
3: No such process //没有次进程
4: Interrupted system call
5: Input/output error
6: No such device or address
7: Argument list too long
8: Exec format error
9: Bad file descriptor
10: No child processes
11: Resource temporarily unavailable
12: Cannot allocate memory
13: Permission denied
14: Bad address
15: Block device required
16: Device or resource busy
17: File exists
18: Invalid cross-device link
19: No such device
.................................
129: Key was rejected by service
130: Owner died
131: State not recoverable
132: Operation not possible due to RF-kill
133: Memory page has hardware error
134: Unknown error 134
.....................
146: Unknown error 146
147: Unknown error 147
148: Unknown error 148
149: Unknown error 149(4)、如何获取退出码
如果想在命令行中获取最近一次进程退出的退出码 通过
[hxVM-24-7-centos 20231203-进程终止]$ echo $?
0
[hxVM-24-7-centos 20231203-进程终止]$ 3、代码没有跑完程序崩溃
当遇到程序崩溃的时候例如遇到野指针除0操作退出码无意义。一般而言退出码对应的return语句没有被执行。 1 #includestdio.h2 #includeunistd.h3 #includestring.h4 5 //程序崩溃6 int main()7 {8 int *pNULL;9 *p1234;//野指针 10 return 0;11 }[hxVM-24-7-centos 20231203-进程终止]$ ./myproc
Segmentation fault1 #includestdio.h2 #includeunistd.h3 #includestring.h4 5 //程序崩溃6 int main()7 {8 //int *pNULL;9 //*p1234;//野指针10 11 12 int a10;
W 13 a/0;//除0操作 14 return 0;15 }[hxVM-24-7-centos 20231203-进程终止]$ make
gcc -stdc99 -o myproc myproc.c
myproc.c: In function ‘main’:
myproc.c:13:6: warning: division by zero [-Wdiv-by-zero]a/0;//除0操作^
[hxVM-24-7-centos 20231203-进程终止]$ ./myproc
Floating point exception
三、如何用代码终止一个进程
1、return语句
return语句就是用来终止进程的
main函数里执行 return语句是用来终止进程
其它函数内部执行return 语句代表函数返回。
2、exit()函数 1 #includestdio.h2 #includeunistd.h3 #includestring.h4 #includestdlib.h5 6 //exit()7 int main()8 {9 printf(hello world\n);10 printf(hello world\n);11 printf(hello world\n);12 exit(11); 13 printf(hello world\n);14 printf(hello world\n);15 printf(hello world\n);16 return 0;17 }[hxVM-24-7-centos 20231203-进程终止]$ ./myproc
hello world
hello world
hello world
[hxVM-24-7-centos 20231203-进程终止]$ echo $?
11
[hxVM-24-7-centos 20231203-进程终止]$
四、知识补充
1、return和exit()区别
return是一个语句return在普通函数里通常代表函数调用结束在main函数里代表进程退出
exit是一个函数代表在任何地点终止进程
2、exit()和_exit()区别
1、exit是C语言提供的进程终止方案进程终止时会把缓冲区中的内容刷新到显示屏然后再进行进程退出 1 #includestdio.h2 #includeunistd.h3 #includestring.h4 #includestdlib.h5 6 7 8 int main()9 {10 printf(you can see me?\n);11 sleep(3);12 exit(11); 13 }
//先打印结果 再sleep三秒
[hxVM-24-7-centos 20231203-进程终止]$ ./myproc
you can see me?
[hxVM-24-7-centos 20231203-进程终止]$ 1 #includestdio.h2 #includeunistd.h3 #includestring.h4 #includestdlib.h5 6 7 //exit()和_exit()对比8 int main()9 {10 printf(you can see me?); 11 sleep(3);12 exit(11);13 }
//先sleep三秒 再打印结果
//去掉\n 因为数据没有\n所以数据没有立即刷新说明这个数据当前一定在缓冲区里最终程序退出时会刷新
//最终看到结果
[hxVM-24-7-centos 20231203-进程终止]$ ./myproc
you can see me?[hxVM-24-7-centos 20231203-进程终止]$ echo $?
11
2、_exit()是系统调用接口(系统层面上想终止进程用的是_exit接口)直接终止进程进程退出不会刷新缓冲区内的内容 1 #includestdio.h2 #includeunistd.h3 #includestring.h4 #includestdlib.h5 6 //exit()和_exit()对比7 int main()8 {9 printf(you can see me?);10 sleep(3);11 _exit(11);12 }
//缓冲区里的内容并没有被刷新出来
//先sleep3秒再进程退出
[hxVM-24-7-centos 20231203-进程终止]$ ./myproc
[hxVM-24-7-centos 20231203-进程终止]$ echo $?
113、区别示意图
exit()函数最后也会调用_exit()函数,但是再调用之前还做了其它工作:
1、执行用户定义的清理函数
2、关闭所有打开的流所有的缓存数据均被写入
3、调用_exit()函数 五、缓冲区相关知识
1、库函数和系统调用接口
os给我们提供接口是因为OS本身不相信我们只是提供接口的方式交互而我们对系统接口并不了解就有人把系统接口做了封装因此语言也就有了自己的库。 exit()底层调用的是_exit(),只不过直接调用_exit()数据没有立即刷新出来而调用exit数据刷新出来了那么缓冲区是谁在维护在那一层维护呢
这个缓冲区一定不在操作系统内部如果是OS维护䣌缓冲区的内容_exit()也能刷新出来但是_exit()不能刷新缓冲区数据而exit()可以又因为exit()是C语言提供的函数因此缓冲区应该由C语言提供的C标准库给我们维护的。