• exit()与_exit()区别


    exit()与_exit()都是用来终止进程的函数,当程序执行到两者函数时,系统将会无条件停止剩下操作,清除进程结构体相应信息,并终止进程运行。

    二者的主要区别在于:exit()函数在执行时,系统会检测进程打开文件情况,并将处于文件缓冲区的内容写入到文件当中再退出。而_exit()则直接退出,不会将缓冲区中内容写入文件。在Linux内核实现中,exit()函数封装了_exit()的实现。

    代码测试:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <unistd.h>
     4 
     5 int main(void)
     6 {
     7     printf("test begin
    ");
     8     printf("this is content buffer");
     9     exit(0);
    10 }

    执行结果为:

    root@ubuntu:/home/test# ./test
    test begin
    this is content buffer

    其中printf函数使用缓存I/O的方式,该函数在遇到“ ”时自动从缓冲区中将记录读出。故上述执行exit()后,第二行printf函数的内容被显示。

    代码测试:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <unistd.h>
     4 
     5 int main(void)
     6 {
     7     printf("test begin
    ");
     8     printf("this is content buffer");
     9     _exit(0);
    10 }

     执行结果为:

    root@ubuntu:/home/test# ./test
    test begin

    位于第二个printf函数中的内容未被打印,其存在于缓冲区中,由于_exit(0)被清空。如果上述在_exit(0)前添加一行代码:fflush(NULL),fflush是一个计算机函数,功能是冲洗流中的信息,该函数通常用于处理磁盘文件。fflush()会强迫将缓冲区内的数据写回参数stream 指定的文件中,第二行printf将会正常输出。

  • 相关阅读:
    VIE模式和IP
    背景色改为豆绿色
    Semantic Logging
    mysql 安装配置相关
    高德API相关
    vmware workstation 虚拟机安装vwmare tools
    sql server2012光盘中有management studio,安装时选择客户端。
    zz微软企业库
    zz flag attribute for enum
    zz 还要用存储过程吗
  • 原文地址:https://www.cnblogs.com/scu-cjx/p/7723366.html
Copyright © 2020-2023  润新知