美文网首页
exit()与_exit()函数的区别(Linux系统中)

exit()与_exit()函数的区别(Linux系统中)

作者: EamonXia | 来源:发表于2019-03-29 10:03 被阅读0次

exit()与_exit()函数的区别(Linux系统中)

/*体现exit和_exit的区别*/
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main(void)
{
    pid_t pid;
    if ( (pid = fork() )==-1 )   //如果创建子进程失败
     {
        perror ("创建子进程失败\n");  //创建子进程出错信息
        exit(0);
     }
    else if(pid==0)  //子进程
     {
        printf("1:this is childthread\n");
        printf("2:this is childthread and now we are in the buf"); 
        //这个地方没有换行符,所以不写出数据
        exit(0);   //退出,强制清空,会输出上面未完成数据
     }
    else  //父进程
     {
        sleep(1);   //休眠一秒以确定先后顺序
        printf("3:this is father");
        printf("4:this is father we are now in the buffer\n");  //同样没有换行符
        _exit(0);  //_exit函数会直接丢弃相应的数据
     }
     return 0;
}

结果显示如下:

[root@host-192-0-11-183 csapp]# g++ exit_exit.cpp -o a.exe   
[root@host-192-0-11-183 csapp]# ./a.exe
1:this is childthread
2:this is childthread and now we are in the buf3:this is father4:this is father we are now in the buffer

Linux学习之进程fork()、exec、exit()/_exit()、wait()/waitpid
深入浅出 C++:与程序终止相关的函数 PART 1 - exit()、atexit()

相关文章

网友评论

      本文标题:exit()与_exit()函数的区别(Linux系统中)

      本文链接:https://www.haomeiwen.com/subject/zccdpqtx.html