LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-20-2008, 12:11 PM   #1
lali.p
Member
 
Registered: Jan 2007
Distribution: Slackware 11.0
Posts: 141

Rep: Reputation: 16
Unhappy How to reinstall a signal handler after exec ( Strange behaviour in signal handling )


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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pass input to a signal handler ? vasanth2001 Programming 8 08-29-2008 08:50 AM
Is it valid to do semop in signal handler ? pravinsd Linux - Kernel 3 07-16-2008 08:49 AM
signal SIGINT handler SOMETIMES doesn't exit Peterius Programming 3 05-25-2008 11:55 PM
signal handler routine vdx Programming 7 09-24-2007 12:43 AM
howto know whether a function is called in signal handler snowing Programming 1 11-07-2005 09:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration