exit 与 _exit的区别

来源:互联网 发布:仿真吉他软件 编辑:程序博客网 时间:2024/04/28 07:14

转载出处:http://blog.csdn.net/goingarm/article/details/46747287


        

       从图中可以看出,_exit 函数的作用是:直接使进程停止运行,清除其使用的内存空间,并清除其在内核的各种数据结构;exit 函数则在这些基础上做了一些小动作,在执行退出之前还加了若干道工序。exit() 函数与 _exit() 函数的最大区别在于exit()函数在调用exit  系统调用前要检查文件的打开情况,把文件缓冲区中的内容写回文件。也就是图中的“清理I/O缓冲”。

 

所需头文件: exit:   #include<stdlib.h>

                       _exit: #include<unistd.h>

函数原型:exit:  void exit(int  status)

                _exit:  void  _exit(int  status)

函数传入值:status 是一个整型的参数,可以利用这个参数传递进程结束时的状态。一般来说,0表示正常结束;其他的数值表示出现了错误,进程非正常结束。在实际编程时,父进程可以利用wait 系统调用接收子进程的返回值,从而针对不同的情况进行不同的处理。

 

exit()与_exit() 实例分析

     printf(const char *fmt,...)函数使用的是缓冲I/O方式,该函数在遇到 "\n" 换行符时自动从缓冲区中将记录读出。

 

<代码示例>

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>


int main()
{

     pid_t result;
     result = fork();
     if(result<0)
     perror("fork");
     if(result == 0)
     {
             printf("This is _exit test\n");
             printf("This is the content in the buffer000");
            _exit(0);
     }
     else
     {
             printf("This is exit test\n");
             printf("This is the content in the buffer");
             exit(0);
     }
     return 0;
}

下面是运行结果:

结果分析:子进程中运行_exit(0)并未将Thisis the content in the buffer000 打印出来,而父进程中运行的exit(0)Thisis the content in the buffer打印出来了。说明,exit(0)会在终止进程前,将缓冲I/O内容清理掉,所以即使printf里面没有 \n也会被打印出来,而_exit(0)是直接终止进程,并未将缓冲I/O内容清理掉,所以不会被打印出来。


我们提倡通过main函数return的方式退出进程。不到万不得已,不使用exit的方式退出。这是为什么?

我们先来看一下下面这些代码:

classTest

{

public:

Test()

{

printf("Test::Test()\n");

}

~Test()

{

printf("Test::~Test()\n");

}

};

intmain()

{

Test testme;

exit(0);

return0;

}

如果去掉上述代码中的exit(0),运行结果是:

Test::Test()

Test::~Test()

如果加上exit,运行结果是否还是一样呢?我们看看实际运行效果:

[zbc@localhost temp]$ g++ exit_test.cpp && ./a.out

Test::Test()

[zbc@localhost temp]$

从上面的实际代码运行结果可以看出:调用exit之后,局部对象的析构函数得不到调用。如果这个局部对象的析构函数还负责把数据回写到磁盘,清除全局同步标识等关键事务,那后果------这个我不说,哥你也懂的。

exit归根到底是操作系统提供的机制,不是C++语言提供的机制,所以它无视C++对象模型的要求,直接终止进程。为安全起见,建议谨慎使用exit(哦,或者是C++)。

exit是这样,pthread_exit/ExitThread也是一样的,请看具体测试代码和结果:

void*thread_func(void*)

{

Test testme;

pthread_exit((void*)1);

returnNULL;

}

intmain()

{

pthread_t th =0;

intret =pthread_create(&th,NULL,thread_func,0);

if(ret !=0){

printf("pthread_create failed, ret=%d\n",ret);

return-1;

}

void*rval =0;

ret =pthread_join(th,&rval);

if(ret !=0){

printf("pthread_join failed, ret=%d\n",ret);

return-1;

}

printf("pthread_join: %p\n",rval);

return0;

}

以上代码的运行结果是:

[zbc@localhost temp]$ g++ -lpthread pthread_exit.cpp

[zbc@localhost temp]$ ./a.out

Test::Test()

pthread_join: 0x1

[zbc@localhost temp]$

另外提一下,上面提到C++对象时特别说明了是局部对象,因为,对于全局对象来说,结果是不同的。C++语言利用类似atexit一类的机制,注册了全局对象的析构函数作为钩子,保证调用exit时,所有全局对象都能得到析构。可惜的是,局部对象没法用这种机制,所以局部对象就悲剧了。由此可见,当操作系统不给面子时,C++语言的处境其实是很尴尬的。


0 0