1、exit函数
1 #include <stdlib.h>
2 void exit(int status);
3 功能:退出当前进程
4 参数:
5 status:退出状态,由父进程通过wait函数接收这个状态
6 一般失败退出设置为非0
7 一般成功退出设置为0
8 返回值:无
2、_exit函数
1 #include <unistd.h>
2 void _exit(int status);
3 功能:退出当前进程
4 参数:
5 status:退出状态,由父进程通过wait函数接收这个状态
6 一般失败退出设置为非0
7 一般成功退出设置为0
8 返回值:无
3、exit和_exit函数的区别:
exit为库函数,而exit为系统调用
exit会刷新缓冲区,但是_exit不会刷新缓冲区
一般会使用exit
案例:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
void myfun()
{
printf( "nihao beijing" );
/*
* 使用return
* return除了可以返回值以外,在主函数中使用可以退出进程,但是在子函数中使用只能退出当前函数
* return;
*/
/*
* 使用exit
* exit可以退出一个进程并且可以刷新缓冲区
* exit(0);
*/
/*
* 使用_exit
* _exit可以退出一个进程,但是不会刷新缓冲区
*/
_exit( 0 );
printf( "welcome to xxx\n" );
}
int main( int argc, char const *argv[] )
{
printf( "hello world\n" );
myfun();
printf( "hello kitty\n" );
return 0;
}
执行结果
使用return:
使用exit
使用_exit