473,785 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

signal handling in C

Consider the following code: segment violation is deliberately
generated to catch the SIGSEGV signal. The handler for this signal,
namely SIGSEGV_handler , is called. However after the handler is
finished, control does not return to the printf statement in main()
which is present after the strcpy statement. Instead Segmentation
violation error message is printed in Redhat Linux and the program is
aborted. In VC++, the SIGSEGV_handler is called and then the program
crashes. Is this the expected behaviour ?

void SIGSEGV_handler (int num);

int main(void)
{
char *str = NULL;

signal(SIGSEGV, SIGSEGV_handler );
strcpy(str, "TEST");
printf("After signal handling\n");

return 0;
}

void SIGSEGV_handler (int num)
{
printf("SIGSEGV _handler called with the argument %d\n", num);

return;
}

Dec 15 '06 #1
10 4035
subramanian said:
Consider the following code: segment violation is deliberately
generated to catch the SIGSEGV signal. The handler for this signal,
namely SIGSEGV_handler , is called. However after the handler is
finished, control does not return to the printf statement in main()
which is present after the strcpy statement. Instead Segmentation
violation error message is printed in Redhat Linux and the program is
aborted. In VC++, the SIGSEGV_handler is called and then the program
crashes. Is this the expected behaviour ?

void SIGSEGV_handler (int num);

int main(void)
{
char *str = NULL;

signal(SIGSEGV, SIGSEGV_handler );
strcpy(str, "TEST");
printf("After signal handling\n");

return 0;
}

void SIGSEGV_handler (int num)
{
printf("SIGSEGV _handler called with the argument %d\n", num);

return;
}
foo.c: In function `main':
foo.c:5: `NULL' undeclared (first use in this function)
foo.c:5: (Each undeclared identifier is reported only once
foo.c:5: for each function it appears in.)
foo.c:7: warning: implicit declaration of function `signal'
foo.c:7: `SIGSEGV' undeclared (first use in this function)
foo.c:9: warning: implicit declaration of function `printf'
make: *** [foo.o] Error 1

If I fix those, I get the behaviour you report. This is not entirely
unexpected from a C language perspective, since the behaviour of the
program is undefined:

"If the signal occurs other than as the result of calling the abort
or raise function, the behavior is undefined if the signal handler
calls any function in the standard library other than the signal
function itself..."

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 15 '06 #2
What should be done to catch signals and proceed with execution from
the next statement onwards in the program

On Dec 15, 1:55 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
subramanian said:
Consider the following code: segment violation is deliberately
generated to catch the SIGSEGV signal. The handler for this signal,
namely SIGSEGV_handler , is called. However after the handler is
finished, control does not return to the printf statement in main()
which is present after the strcpy statement. Instead Segmentation
violation error message is printed in Redhat Linux and the program is
aborted. In VC++, the SIGSEGV_handler is called and then the program
crashes. Is this the expected behaviour ?
void SIGSEGV_handler (int num);
int main(void)
{
char *str = NULL;
signal(SIGSEGV, SIGSEGV_handler );
strcpy(str, "TEST");
printf("After signal handling\n");
return 0;
}
void SIGSEGV_handler (int num)
{
printf("SIGSEGV _handler called with the argument %d\n", num);
return;
}foo.c: In function `main':
foo.c:5: `NULL' undeclared (first use in this function)
foo.c:5: (Each undeclared identifier is reported only once
foo.c:5: for each function it appears in.)
foo.c:7: warning: implicit declaration of function `signal'
foo.c:7: `SIGSEGV' undeclared (first use in this function)
foo.c:9: warning: implicit declaration of function `printf'
make: *** [foo.o] Error 1

If I fix those, I get the behaviour you report. This is not entirely
unexpected from a C language perspective, since the behaviour of the
program is undefined:

"If the signal occurs other than as the result of calling the abort
or raise function, the behavior is undefined if the signal handler
calls any function in the standard library other than the signal
function itself..."

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 15 '06 #3
In article <11************ **********@80g2 000cwy.googlegr oups.com>
subramanian <su************ **@yahoo.comwro te:
>void SIGSEGV_handler (int num);

int main(void)
{
char *str = NULL;

signal(SIGSEGV, SIGSEGV_handler );
strcpy(str, "TEST");
printf("After signal handling\n");

return 0;
}

void SIGSEGV_handler (int num)
{
printf("SIGSEGV _handler called with the argument %d\n", num);

return;
}
This code is full of simple mechanical errors (does not compile
because, e.g., SIGSEGV is not defined at all, since you did not
"#include" the appropriate headers). If you fix those, however,
the code remains undefined by the C Standards, and full of more
important errors even on those systems where the behavior *is*
predictable.

In particular, those systems that (a) somehow catch the invalid
first value in the strcpy() call and (b) actually enter your
SIGSEGV handler in a useful way will generally either:

- reset the SIGSEGV handler to the default ("terminate program
with core dump" behavior), or
- temporarily defer additional SIGSEGVs, keeping the handler
installed and re-enabling it upon return from the handler.

They will also provide -- in a manner that cannot be obtained using
the simple Standard C handler shown above -- additional arguments
pinpointing (or perhaps just "generally getting close to", depending
on the hardware) the instruction pointer at the time the signal
occurred. To get this information you should[%] use a different
call, such as the POSIX sigaction(), to install the handler. Then,
inside the handler, you may be required to modify the provided
machine state -- which is rather naturally machine-dependent, i.e.,
will differ on an Intel vs a PowerPC vs a SPARC vs a MIPS -- if
you intend for the program to do anything useful upon returning
from the handler. If you leave the state unchanged, there is a
significant possibility that returning from the handler will take
you back to the faulting instruction, which will just immediately
fault again, calling the handler again (assuming you have set the
correct options for sigaction() or whatever the correct call may
be), and leading to an endless loop that, aside from the complexities
of signal handling, is effectively identical to:

for (;;)
;

Hence, the code you really need is OS- and machine-dependent and
cannot be written in portable C. You therefore need some other
newsgroup(s), such as comp.unix.progr ammer, comp.os.linux.* ,
comp.os.ms-windows.*, etc., or (probably better) the documentation
for your hardware, compiler, and/or operating system.

[% It may or may not be possible to get the extra information when
using the Standard C "signal" interface, but it is probably unwise
to attempt this unless the documentation for your system specifically
says to do so. In some cases, the extra information is available
more or less "by accident", and a future version of the system may
move it elsewhere without warning.]
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Dec 15 '06 #4
Please see the man page "signal" ( manual 7, man 7 signal ).

There is an action associated with each signal handler. And it
specifies the action to be taken. When SIGSEGV is received, the action
taken is "Core" i.e, terminate the process and dump core. Apart from
calling the user defined signal handler. And that is the reason for not
printing the subsequent print() statement.

Thanks & Regards
--vijayck

subramanian wrote:
Consider the following code: segment violation is deliberately
generated to catch the SIGSEGV signal. The handler for this signal,
namely SIGSEGV_handler , is called. However after the handler is
finished, control does not return to the printf statement in main()
which is present after the strcpy statement. Instead Segmentation
violation error message is printed in Redhat Linux and the program is
aborted. In VC++, the SIGSEGV_handler is called and then the program
crashes. Is this the expected behaviour ?

void SIGSEGV_handler (int num);

int main(void)
{
char *str = NULL;

signal(SIGSEGV, SIGSEGV_handler );
strcpy(str, "TEST");
printf("After signal handling\n");

return 0;
}

void SIGSEGV_handler (int num)
{
printf("SIGSEGV _handler called with the argument %d\n", num);

return;
}
Dec 15 '06 #5


On Fri, 14 Dec 2006, subramanian wrote:
What should be done to catch signals and proceed with execution from
the next statement onwards in the program

A possible option can be using the longjmp() and setjmp()
functions/macros, as follows. If your implementation allows calling
printf() from within signal handlers, you can uncomment the printf()
inside the signal handler.

Emil
#include <signal.h>
#include <stdio.h>
#include <setjmp.h>

jmp_buf jbuf;

void SIGSEGV_handler (int num);

int main(void)
{
char *str = NULL;

signal(SIGSEGV, SIGSEGV_handler );
if (!setjmp(jbuf))
strcpy(str, "TEST");
printf("After signal handling\n");

return 0;
}

void SIGSEGV_handler (int num)
{
/*printf("SIGSEG V_handler called with the argument %d\n", num);*/
longjmp(jbuf,1) ;
}
Dec 15 '06 #6
Hello,
Consider the following code: segment violation is deliberately
generated to catch the SIGSEGV signal. The handler for this signal,
namely SIGSEGV_handler , is called. However after the handler is
finished, control does not return to the printf statement in main()
which is present after the strcpy statement. Instead Segmentation
violation error message is printed in Redhat Linux and the program is
aborted. In VC++, the SIGSEGV_handler is called and then the program
crashes. Is this the expected behaviour ?
The problem you are facing can't be answered within the C standard.
This is OS dependent. For instance, on UNIX/Linux, the following apply:

<quote SUSv3>
If and when the function [signal handler] returns, if the value of sig
was SIGFPE, SIGILL, or SIGSEGV or or any other implementation-defined
value corresponding to a computational exception, the behavior is
undefined.
</quote SUSv3>

That means, everything can happen... On some UNIX, the message
"SIGSEGV_handle r called with the argument 11" will be repeated forever,
because the PC or the
effective address that took the SIGSEGV isn't modified, causing the
SIGSEGV to be repeated... This makes a really nice infinite loop,
actually.

HTH,
Loic.

Dec 15 '06 #7
Kohn Emil Dan wrote:
On Fri, 14 Dec 2006, subramanian wrote:
>What should be done to catch signals and proceed with execution from
the next statement onwards in the program

A possible option can be using the longjmp() and setjmp()
functions/macros, as follows. If your implementation allows calling
printf() from within signal handlers, you can uncomment the printf()
inside the signal handler.
Using `longjmp` inside a signal handler in Standard C gives you
undefined behaviour (unless the signal was provoked by `abort`
or `raise`).

The OP will need implementation-dependant signal handling code.
(Maybe he can rely on some other standard to make it less
implementation-dependant that it could be.)

--
Chris "to heavy to even shortjmp" Dollin
"We did not have time to find out everything we wanted to know."
- James Blish, /A Clash of Cymbals/

Dec 15 '06 #8
vijay kalkoti wrote:
Please see the man page "signal" ( manual 7, man 7 signal ).

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html >
Dec 15 '06 #9


On Fri, 15 Dec 2006, Chris Dollin wrote:
Kohn Emil Dan wrote:
<snipped>
>>
<snipped>
Using `longjmp` inside a signal handler in Standard C gives you
undefined behaviour (unless the signal was provoked by `abort`
or `raise`).

Can you point me where the standard mentions this?
>
The OP will need implementation-dependant signal handling code.
(Maybe he can rely on some other standard to make it less
implementation-dependant that it could be.)

In my opinion any non-trivial C program needs some implementation
dependent features, so I agree with you.
Emil
--
Chris "to heavy to even shortjmp" Dollin
"We did not have time to find out everything we wanted to know."
- James Blish, /A Clash of Cymbals/

Dec 16 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3936
by: Lionel van den Berg | last post by:
Hi all, I'm trying to do some signal handling using the csignal library but I can't find and specific examples for C++. The signal function as defined in C takes a parameter that is the signal we are registering for, and a second parameter that is the function to call. What I want to do is specify the function to call as a function of an instance of "this" class e.g.
2
2212
by: lpw | last post by:
I have dilligently reviewed FAQ-lite Section 3.2, "How do I pass a pointer-to-member-function to a signal handler, X event callback, system call that starts a thread/task, etc." The only suggestion on how to deliver a signal to an object is to do it via a global variable and a wrapper function, a technique that is generally a Bad Idea (due to the usage of a global variable). I understand that this ng is dedicated to the discussion of...
3
3526
by: Martin McCormick | last post by:
A C program contains several signal statements to remove a lock file if the program gets killed: /*Set up interrupt handler to catch ctrl-C so that lock file can be removed.*/ signal(SIGINT,crash); signal(SIGBUS,crash); signal(SIGSEGV,crash); This part works. Those signals cause the "crash.c" module to run and get rid of the lock. Is there a standard way to also print
4
2018
by: Eric Boutin | last post by:
Hi ! currently reading C docs, I think I'm reading docs about the stdc lib, but I'm not shure.. it talks about signals, does *all* OS implement the signal function, and use it well ? I mean.. I know Unixes do it, but what about windows ? does it implements all SIG* signal promptly ? Thanks ! -Eric Boutin
7
2214
by: Stanley S | last post by:
Hi, Are Signal Handling part of ANSI C? I am not able to find any reference of Sig Handling in Stephen Prata's "C Primer Plus". The usage of signals is to trap errors I guess. (It looks similiar to the concept of try-catch to me.) It seems to relate more to nix OS. Are signals handling part of Windows too?
2
1488
by: hg | last post by:
Hi, I posted an equivalent question earlier ... but am still not sure: I currently (under Linux) have a program that uses Queue.put (raw_input('')) in a signal handler and Queue.get() in the main/only thread. It works but I want to know if it is legal / what type of synchro API I have the right to use in a signal handler.
3
6900
by: vijay.db | last post by:
Hi Group, Running DB2 V8.2 Fxpack 9 in AIX 5.2, I get the following error frequently and my instance is stopped...collected some info like: the signal received is 11 which is SEGMENTATION VIOLATION ERROR...after that I'm getting the entry as DIA0505I Execution of a component signal handling function has begun, so my query is what does the DIA05051 actally mean.. My diag log entry is as follows:
1
4819
by: 32Alpha | last post by:
Hi, first post here. First off, this IS a homework assignment for an operating systems class, but the question isn't "how do i do the assignment" but "why is my particular implementation not working". I've searched the forums here, but could not find anything particular to my problem, Googling and the linux manual pages haven't been much help either. What the program is supposed to do is create a very simple shell (that sits atop the...
0
1132
by: Marcin Krol | last post by:
Right, I didn't realize before that Python interpreter has its own signal handling routines. Now I am able to handle signals in Python code, but it still barfs on exit: import time import signal import sys def userBreak(sigNum, execFrame):
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10350
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8983
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7505
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.