The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Special Forums > UNIX and Linux Applications
.
google unix.com



UNIX and Linux Applications Discuss UNIX and Linux software applications. This includes SQL, Databases, Middleware, MOM, SOA, EDA, CEP, BI, BPM and similar topics.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
problem in SIGSEGV signal handling pavan6754 High Level Programming 4 07-10-2009 06:33 AM
Signal handling #moveon High Level Programming 1 05-27-2009 07:29 AM
signal handling question fox_hound_33 High Level Programming 7 05-31-2008 01:53 PM
Program received signal SIGSEGV, Segmentation fault. napapanbkk High Level Programming 5 06-30-2007 07:56 AM
Signal Handling themezzaman High Level Programming 3 07-12-2006 10:05 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-07-2009
mustus mustus is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 2
SIGSEGV Signal handling

Hello,
Can anybody tell me how can i handle segmentation fault signal, in C code?
  #2 (permalink)  
Old 08-07-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
First off, do not run code in production that gets a SIGSEGV signal. It is really bad practice.

The simplest way, and definitely not the best is to block the signal with a call to signal()
Code:
#include <signal.h>
#include <stdio.h>

void my_signal_handler(int sig)
{
    write(STDERR_FILENO, "SEGFAULT!\n", 10); 
    signal(SIGSEGV, my_signal_handler); // reset the signal handler for SIGSEGV
    abort();
}

int main()
{
   signal(SIGSEGV, my_signal_handler); // initial set of signal handler
   /* do stuff here  */
   signal(SIGSEGV, SIG_DFL);  // restore default behavior

}
This is an example not meant to be used for production - it has potential race conditions, but you can use it to debug a SIGSEGV problem. In the my_signal_handler function be aware that you need to call only async safe functions like write() not std C lib funcions like printf()
  #3 (permalink)  
Old 08-07-2009
matrixmadhan matrixmadhan is online now Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,952
Quote:
In the my_signal_handler function be aware that you need to call only async safe functions like write() not std C lib funcions like printf()
Hello Jim,

Can you please explain this?

Is it because of the reason, that kernel might possibly deliver SIGSEGV when using printf, but I guess that's possible even with write() - I think am wrong.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:36 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0