进程通信:信号通信中的SIGABRT的验证测试

/******************************************************************************************************************

参考:

说明:主要是在程序中的合适地址加上abort函数它就会向调用它的进程发SIGABRT信号

******************************************************************************************************************/ 

           为了验证这个SIGABRT,也就是abort函数,想起来把前两天学习的东西都结合起来了有:fork();有wait();有execl();有abort()。主体思想就是运行一个根本就不存在的程序,让其运行abort()。从下边的运行结果来看它没有运行abort()以下的代码,因为这时子进程已经退出了,并在它生命的最后一刻它告诉了它我父进程一些信息--就是一个SIGABRT信号。这个信号有什么用? 其实也没有什么用,父进程也可以不鸟这个信号。这只是一方面,主要说的是如果父进程想知道这个子进程怎么了,怎么就死了,想破案的话就可以找这个类似飞机上的黑匣子一样。用这个信号来传递。还有验证结果是abort()只干掉了当前进程,从实验结果来看有干掉父进程。

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>

void my_func(int sign_no)
{
	if(sign_no == SIGABRT)
	{
		printf("I have get SIGABRT\n");
	}
}
int main(void)
{
	pid_t child;
	
	signal(SIGABRT, my_func);
	
	if((child=fork())==-1)
	{
		printf("Fork Error: %s\n", strerror(errno));
		exit(1);
	}
	else
	{
		if(child == 0)
		{
			printf("the child process is run\n");
			sleep(1);
			
			if((execl("fuck", "fuck",NULL)) == -1);
				abort();
			
			printf("I am the child:%d\n", getpid());
			exit(0);
		}
		else
		{
			wait(NULL);
			printf("the father process is run\n");
			printf("I am the father: %d\n", getpid());
			return 0;
		}
	}
}

运行结果:

[root@localhost gcc]# ./a.out
the child process is run
I have get SIGABRT
the father process is run
I am the father: 30540
[root@localhost gcc]#


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

袁保康

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值