zoukankan      html  css  js  c++  java
  • 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将会正常输出。

  • 相关阅读:
    php 基本连接mysql数据库和查询数据
    HTTP/2 简介
    MySQL数据库 utf-8与utf8mb4
    AJAX请求中出现OPTIONS请求
    精力充沛的管理者,要不要“闲”下来?
    【转】微信开发出现“该公众号暂时无法提供服务,请稍后再试”的坑
    Nginx笔记(一):安装
    Tomcat分析
    Redis集群搭建与使用
    微信JS-SDK实现上传图片功能
  • 原文地址:https://www.cnblogs.com/scu-cjx/p/7723366.html
Copyright © 2011-2022 走看看