473,569 Members | 2,782 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Throwing exceptions in a unix signal handler. Good idea?

Is the behaviour of throwing exceptions in a unix signal handler
defined?

eg:

void sighandler(int sig)
{
... do something
throw myobj;
}

Under gcc on linux it seems to work as I'd expect - ie the exception
handler wrapping the code running when the signal was caught catches
the exception, but no one I've asked seems sure if this is how it
should work or its just the gcc way of doing it.

Can anyone shed any light on this?

Thanks

B2003
Jun 27 '08 #1
9 5800
th********@goog lemail.com writes:
Is the behaviour of throwing exceptions in a unix signal handler
defined?

eg:

void sighandler(int sig)
{
... do something
throw myobj;
}

Under gcc on linux it seems to work as I'd expect - ie the exception
handler wrapping the code running when the signal was caught catches
the exception, but no one I've asked seems sure if this is how it
should work or its just the gcc way of doing it.

Can anyone shed any light on this?
It's not a good idea.

First, if it works with your compiler on your kernel, it may very well
(and most probably) not work with another compiler or on another
kernel.

And even with your compiler on your kernel, if you add multithreads,
there's no guarantee in which thread the signals are processed.

Also, some syscalls may be interrupted by signals, with some exit code
indicating it, to let the program retry the syscall. If you throw an
exception from the signal handler, you may leave an unstable state.
It means that this exception can be thrown from any place, not only to
the very localized places where you call a function that throws it.

So I would definitely not do that.
--
__Pascal Bourguignon__
Jun 27 '08 #2
On 2008-06-09 16:54:58 +0200, th********@goog lemail.com said:
Is the behaviour of throwing exceptions in a unix signal handler
defined?

eg:

void sighandler(int sig)
{
... do something
throw myobj;
}

Under gcc on linux it seems to work as I'd expect - ie the exception
handler wrapping the code running when the signal was caught catches
the exception, but no one I've asked seems sure if this is how it
should work or its just the gcc way of doing it.

Can anyone shed any light on this?
From the C99 standard: "If the signal occurs other than as the result
of calling the abort or raise function, the behavior is undefined if the
signal handler refers to any object with static storage duration other
than by assigning a value to an object declared asvolatile
sig_atomic_t, or the signal handler calls any function in the standard
library other than the abort function, the _Exit function, or the
signal function with the first argument equal to the signal number
corresponding to the signal that caused the invocation of the handler.
Furthermore, if such a call to the signal function results in a SIG_ERR
return, the value of errno is indeterminate."

So, unless the signal came from a call to abort or raise, the behavior
is undefined. If your compiler tells you what it does, fine. But that's
a discussion for a different newsgroup.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #3
On Jun 9, 4:26 pm, Pete Becker <p...@versatile coding.comwrote :
From the C99 standard: "If the signal occurs other than as the result
That C, not C++.
So, unless the signal came from a call to abort or raise, the behavior
is undefined.
It may be undefined from the point of view of the C standard , but not
from the unix standard otherwise there'd be no point using signals -
every time a handler was called you'd have to hold your breath and
hope the program didn't crash!

I'm only interested in the specific combination of C++ exceptions and
signal handlers since once stores the stack and one forces an unwind.
Are compilers smart enough to know to pop the signal handler stack
first and return to where the signal occured before unwinding the
stack for the exception is the question.

B2003
Jun 27 '08 #4
th********@goog lemail.com wrote:
On Jun 9, 4:26 pm, Pete Becker <p...@versatile coding.comwrote :
>From the C99 standard: "If the signal occurs other than as the result

That C, not C++.
>So, unless the signal came from a call to abort or raise, the behavior
is undefined.

It may be undefined from the point of view of the C standard , but not
from the unix standard otherwise there'd be no point using signals -
every time a handler was called you'd have to hold your breath and
hope the program didn't crash!
You still need to ask in the OS/compiler specific newsgroup that can
tell you the mechanics of that system. C++ says nothing about it and it
works differently on the different systems.
Jun 27 '08 #5
th********@goog lemail.com wrote:
On Jun 9, 4:26 pm, Pete Becker <p...@versatile coding.comwrote :
>From the C99 standard: "If the signal occurs other than as the result

That C, not C++.
>So, unless the signal came from a call to abort or raise, the behavior
is undefined.

It may be undefined from the point of view of the C standard , but not
from the unix standard otherwise there'd be no point using signals -
every time a handler was called you'd have to hold your breath and
hope the program didn't crash!
It is also undefined in the C++ standard, which refers to the C standard.
I'm only interested in the specific combination of C++ exceptions and
signal handlers since once stores the stack and one forces an unwind.
Are compilers smart enough to know to pop the signal handler stack
first and return to where the signal occured before unwinding the
stack for the exception is the question.
Only a platform specific group could answer that. There isn't a
portable standard C++ answer (other than "it's undefined").
--
Ian Collins.
Jun 27 '08 #6
On Jun 9, 12:47 pm, Ian Collins <ian-n...@hotmail.co mwrote:
thagor2...@goog lemail.com wrote:
On Jun 9, 4:26 pm, Pete Becker <p...@versatile coding.comwrote :
From the C99 standard: "If the signal occurs other than as the result
That C, not C++.
So, unless the signal came from a call to abort or raise, the behavior
is undefined.
It may be undefined from the point of view of the C standard , but not
from the unix standard otherwise there'd be no point using signals -
every time a handler was called you'd have to hold your breath and
hope the program didn't crash!

It is also undefined in the C++ standard, which refers to the C standard.
I'm only interested in the specific combination of C++ exceptions and
signal handlers since once stores the stack and one forces an unwind.
Are compilers smart enough to know to pop the signal handler stack
first and return to where the signal occured before unwinding the
stack for the exception is the question.

Only a platform specific group could answer that. There isn't a
portable standard C++ answer (other than "it's undefined").
Ian is right. The Linux g++ docs flat out say it's undefined.
Jun 27 '08 #7
On Mon, 9 Jun 2008 17:26:29 +0200, Pete Becker
<pe**@versatile coding.comwrote in comp.lang.c++:
On 2008-06-09 16:54:58 +0200, th********@goog lemail.com said:
Is the behaviour of throwing exceptions in a unix signal handler
defined?

eg:

void sighandler(int sig)
{
... do something
throw myobj;
}

Under gcc on linux it seems to work as I'd expect - ie the exception
handler wrapping the code running when the signal was caught catches
the exception, but no one I've asked seems sure if this is how it
should work or its just the gcc way of doing it.

Can anyone shed any light on this?

From the C99 standard: "If the signal occurs other than as the result
of calling the abort or raise function, the behavior is unde?ned if the
signal handler refers to any object with static storage duration other
than by assigning a value to an object declared asvolatile
sig_atomic_t, or the signal handler calls any function in the standard
library other than the abort function, the _Exit function, or the
signal function with the ?rst argument equal to the signal number
corresponding to the signal that caused the invocation of the handler.
Furthermore, if such a call to the signal function results in a SIG_ERR
return, the value of errno is indeterminate."

So, unless the signal came from a call to abort or raise, the behavior
is undefined. If your compiler tells you what it does, fine. But that's
a discussion for a different newsgroup.
Even more significant, from C++98:

"The common subset of the C and C++ languages consists of all
declarations, definitions, and expressions that may appear in a well
formed C++ program and also in a conforming C program. A POF (plain
old function) is a function that uses only features from this common
subset, and that does not directly or indirectly use any function that
is not a POF. All signal handlers shall have C linkage. A POF that
could be used as a signal handler in a conforming C program does not
produce undefined behavior when used as a signal handler in a C++
program. The behavior of any other function used as a signal handler
in a C++ program is implementation defined."

The last sentence points to footnote 213 on the same page, which adds:

"In particular, a signal handler using exception handling is very
likely to have problems"

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jun 27 '08 #8
On 2008-06-09 17:45:04 +0200, th********@goog lemail.com said:
On Jun 9, 4:26 pm, Pete Becker <p...@versatile coding.comwrote :
>From the C99 standard: "If the signal occurs other than as the result

That C, not C++.
It is both. C++ gets its specification for signal and raise from the C
standard.
>
>So, unless the signal came from a call to abort or raise, the behavior
is undefined.

It may be undefined from the point of view of the C standard , but not
from the unix standard otherwise there'd be no point using signals -
every time a handler was called you'd have to hold your breath and
hope the program didn't crash!
Yes, that's correct.
>
I'm only interested in the specific combination of C++ exceptions and
signal handlers since once stores the stack and one forces an unwind.
Are compilers smart enough to know to pop the signal handler stack
first and return to where the signal occured before unwinding the
stack for the exception is the question.

If you're interested in platform-specific behavior, you need to ask on
a newsgroup that discusses your platform. In standard C++, the behavior
is undefined.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #9
On Jun 9, 5:45 pm, thagor2...@goog lemail.com wrote:
On Jun 9, 4:26 pm, Pete Becker <p...@versatile coding.comwrote :
From the C99 standard: "If the signal occurs other than as
the result
That C, not C++.
Included in the C++ standard by reference.
So, unless the signal came from a call to abort or raise,
the behavior is undefined.
It may be undefined from the point of view of the C standard ,
but not from the unix standard otherwise there'd be no point
using signals - every time a handler was called you'd have to
hold your breath and hope the program didn't crash!
Not if you only do things which are allowed in the signal
handler. Posix allows you to call a certain number of system or
library functions, in addition to what the C standard allows,
but it's still very limited. (Of course, if you're only under
Posix, you won't be using signal() anyway.)
I'm only interested in the specific combination of C++
exceptions and signal handlers since once stores the stack and
one forces an unwind. Are compilers smart enough to know to
pop the signal handler stack first and return to where the
signal occured before unwinding the stack for the exception is
the question.
The problem is that signals can arrive asynchronously. At a
moment, for example, when the stack frame isn't set up
correctly, and stack walkback isn't possible. In practice, it
is very, very difficult to implement it so that it can work
reliably, and I know of no system which does so.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique oriente objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
Jun 27 '08 #10

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

Similar topics

2
4569
by: Jakub Moscicki | last post by:
Hello, A small problem: I get a signal during a system call (from xmlrpclib -> httplib) and an exception "IOError: Interrupted system call" is raised (this is system dependant, on other machine it does not raise this exception). I have my own signal handler so I want to simply ignore this exception if it occures. But for a reason...
1
1587
by: Dominic | last post by:
Just in case someone is interested. I came up with this solution since my previous posting. Maybe you've got some other ideas or critcism. I'll listen ;-) Ciao, Dominic P.S. Python is really powerful!
21
7230
by: Stephan | last post by:
why does the following code not work???? after compiling and running it will just say killed after all my memory filled up any suggestions? #include <iostream> using namespace std; void out_of_mem() {
13
4828
by: Siemel Naran | last post by:
Hi. I posted this question to comp.lang.c++, but am rephrasing it a bit from what I learned and posting to comp.lang.c++.moderated for more insight. So how do I solve my problem? I want it so that when the user presses Ctrl-C or eds the task from task manager (or the kill command in UNIX) then the system should shutdown gracefully. This...
4
6900
by: maxmagna | last post by:
Hello, I am looking for material that describes how *precisely* UNIX-based signals interact with the normal C++ execution flow of control and guidelines on how to treat signals in an "object-oriented style". So far I have not been able to locate much on the subject. Can anyone provide any pointer/information or suggestion? Thanks, max
40
13487
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
18
2959
by: Denis Petronenko | last post by:
Hello, in the following code i have segmentaion fault instead of exception. Why? What i must to do to catch exceptions in such situation? Used compiler: gcc version 3.3.6 (Debian 1:3.3.6-13) int main() { try{ int* p = NULL; *p = 4;
7
5104
by: Sek | last post by:
Hi Folks! I was pondering over a code and noticed that exception handlers were present in the private, protected as well as public methods. And, ofcourse, public methods were calling priv/prot methods internally. My thought was, the exception is being rethrown and propagated by the non-public methods to the public methods, causing...
0
7697
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, well explore What is ONU, What Is Router, ONU & Routers main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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. ...
0
8120
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6283
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5512
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...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
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
0
937
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...

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.