Linux 进程终止5种形式

1、main函数的自然返回,return
2、调用exit函数
3、调用_exit函数
4、调用abort函数
5、接受能导致进程终止的信号:

    ctrl+c (^C)

    SIGINT(SIGINT中断进程) 

    SIGQUIT (进程在因收到SIGQUIT退出时会产生core文件, 在这个意义上类似于一个程序错误信号)

     ctrl + \ (^\Quit)

其中1、2、3为正常终止,4、5异常终止


exit和_exit函数都是用来终止进程的。当程序执行到exit和_exit时,进程会无条件的停止剩下的所有操作,清除包括PCB在内的各种数据结构,并终止本程序的运行。

exit函数和_exit函数的最大区别在于exit函数在退出之前会检查文件的打开情况,把文件缓冲区中的内容写回文件,也就是清理I/O缓冲。

exit可输出缓冲区数据

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("Using exit\n");
    printf("This is the content in buffer");
    exit(0);
}

//运行结果:
Using exit
This is the content in buffer

_exit无法输出缓冲区数据

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
        printf("Using exit\n");
        printf("This is the content in buffer");
        _exit(0);
}
//运行结果:
Using exit

abort()是使异常程序终止,同时发送SIGABRT信号给调用进程。
使用实例:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *stream;
    if((stream = fopen("nofilehere", "r")) == NULL)
    {
         perror("Can not open");
         abort();
   }
   else
   {
        fclose(stream);
   }
   return 0;
}

运行结果:

Can not open: No such file or directory
Aborted (core dumped)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值