Hi
The following program is born totally out of curiosity and as far as i can think has no real use ? (Kindly point out if i am wrong anywhere)
I am trying to install signal handler for SIGSEGV.
Code:
#include<iostream>
#include<unistd.h>
#include<signal.h>
using namespace std;
void signal_handler(int n)
{
cout<<"\nSEGMENTATION FAULT OCCURRED I AM LOADING THE PROCESS AGAIN"<<endl;
execv("a.out",0);
}
int main()
{
struct sigaction s;
s.sa_handler=signal_handler;
cout<<"\nTHIS IS THE MAIN OF THE PROCESS"<<endl;
if(sigaction(SIGSEGV,&s,0))
{
cout<<"\nINSTALLATION OF SIGNAL HANDLER FAILED"<<endl;
}
else
{
cout<<"\nSIGNAL HANDLER SUCCESSFULLY INSTALLED"<<endl;
}
int* p=0;
*p=6;
cout<<"\nBefore return"<<endl;
return 0;
}
The thing is that i want to handle the signal SIGSEGV and then exec the same process(executable a.out).
exec i guess clears all the signal dispositions and reinstalls the default signal dispositions. But since the new process starts from the main, i can again reinstall the signal handler for SIGSEGV( and infact the output verifies it but still i get the following behaviour which i don't expect)
Quote:
me@battleaXe:~/programs$ g++ signal_handling.cpp
me@battleaXe:~/programs$ ./a.out
THIS IS THE MAIN OF THE PROCESS
SIGNAL HANDLER SUCCESSFULLY INSTALLED
SEGMENTATION FAULT OCCURRED I AM LOADING THE PROCESS AGAIN
THIS IS THE MAIN OF THE PROCESS
SIGNAL HANDLER SUCCESSFULLY INSTALLED
Segmentation fault
me@battleaXe:~/programs$
|
My expected behaviour is that the process should keep on segfaulting and keep reloading itself infinitely. I does happen for the first time but from next time onwards it doesn't( as you can clearly see in the output )
I have googled for more than 2 hours without any satisfactory answer.
I also learned from somewhere that when a signal handler is being executed, that signal is masked i.e SIGSEGV in my case would be masked and when i do exec in the signal handler, the signal SIGSEGV would remain masked in the execed process. But if that is the case then i should never get "Segmentation fault" because if SIGSEGV gets masked, it won't be delivered and in that case my program should execute completely, isn't it ? But infact the statement "Before return" never gets printed as you can see from the output.
May be i am missing something and there could be some flaw in the way i have understood things to be.
Waiting eagerly for your response.
Thanks & Regards
lali.cpp