【C】C语言中的_exit()与exit()

_exit()和exit()主要区别是一个退出进程会清理I/O缓冲区,一个直接结束进程进入到内核中

举例说明:

 1 #include <stdio.h>
 2 
 3 
 4 /*demo01   程序只输出 hello world*/
 5 /*
 6 int main()
 7 {
 8     printf("hello world\n");
 9     printf("hello world");
10     _exit(0);
11 }
12 */
13 
14 /*demo02   程序输出  hello world
15                       hello world */
16 /*
17 int main()
18 {
19     printf("hello world\n");
20     printf("hello world\n");
21     _exit(0);
22 }
23 */
24 
25 /*demo03  程序只输出 hello world*/
26 /*
27 int main()
28 {
29     printf("hello world\n");
30     printf("hello world");
31     exit(0);
32 }
33 */
34 
35 /*demo04   程序输出  hello world
36                       hello world */
37 /*
38 
39 int main()
40 {
41     printf("hello world\n");
42     printf("hello world\n");
43     exit(0);
44 }

解释:

printf函数就是使用的是缓冲I/O的方式,该函数在遇到“\n“换行符时自动的从缓冲区中将记录读出。所以exit()将缓冲区的数据写完后才能退出来,所以调用exit()函数后程序并不会马上退出,这就是有些出现的僵尸程序,而_exit是直接退出进入到内核中去。

return是语言级别的,它表示了调用堆栈的返回;而exit是系统调用级别的,它表示了一个进程的结束。

return是返回函数调用,如果返回的是main函数,则为退出程序。   
exit是在调用处强行退出程序,运行一次程序就结束   

exit(1)表示异常退出.这个1是返回给操作系统的不过在DOS好像不需要这个返回值。   
exit(0)表示正常退出   

无论写在那里,都是程序退出,dos和windows中没有什么不一样,最多是系统处理的不一样。   
数字0,1,-1会被写入环境变量ERRORLEVEL,其它程序可以由此判断程序结束状态。   
一般0为正常退出,其它数字为异常,其对应的错误可以自己指定。   


返回给操作系统的,0是正常退出,其他值是异常退出,在退出前可以给出一些提示信息,或在调试程序中察看出错原因.

参考:https://blog.csdn.net/yyfwd/article/details/50548359

猜你喜欢

转载自www.cnblogs.com/xuelisheng/p/10171650.html