/******************************************************************************************************************
参考:
说明:主要是在程序中的合适地址加上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]#