473,386 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

signal trapping in C++

Hey guys --

I have an idea on how to make my program a bit better -- but I'm not
sure if I can do it.

Do any of you know if there is a way to have a non-static function trap
a signal from the OS? The most interesting data from my program is only
output at the very end of the program, but sometimes I realize that I
need to stop it sooner (e.g. i set a parameter such that it is running
longer than I anticipated OR it crashes OR PBS wants to kill it) and it
would be nice if before quitting I could get it to print the best
results that it has so far (from all processors when run in parallel if
possible).

It seems that you have to use a static void function for both atexit
and signal (from signal.h) callbacks, but this would require making
everything that function uses static (e.g. the classes containing all
the data), right?

is there another way?

thanks!

Mar 30 '06 #1
9 1856
ni*******@gmail.com wrote:
Hey guys --

I have an idea on how to make my program a bit better -- but I'm not
sure if I can do it.

Do any of you know if there is a way to have a non-static function trap
a signal from the OS? The most interesting data from my program is only
output at the very end of the program, but sometimes I realize that I
need to stop it sooner (e.g. i set a parameter such that it is running
longer than I anticipated OR it crashes OR PBS wants to kill it) and it
would be nice if before quitting I could get it to print the best
results that it has so far (from all processors when run in parallel if
possible).

It seems that you have to use a static void function for both atexit
and signal (from signal.h) callbacks, but this would require making
everything that function uses static (e.g. the classes containing all
the data), right?

is there another way?

No. While signals are OT here, the general concept of passing C++
method to C libraries isn't.

When passing callbacks to C, the function must have extern "C" linkage.
Some compilers will let you get away with passing a static member, but
this isn't strictly correct.

I recommend the using a plain old extern "C" function, which can, if
required, be made a friend of the class.

--
Ian Collins.
Mar 30 '06 #2
Hi,

ni*******@gmail.com writes:
It seems that you have to use a static void function for both atexit
and signal (from signal.h) callbacks, but this would require making
everything that function uses static (e.g. the classes containing all
the data), right?


Strictly speaking, you have to use a global function here. But with quite
a few compiler you can get away with static member functions, as they tend
to have the same calling convention.

Also, it is quite limited what you may done in a signal handler. E.g.,
calling most library function is off limit. The usual practice is to
create a global variable with type sig_atomic_t, set it from the signal
handler, and query it now-and-then from your main loop.

ImRe
Mar 31 '06 #3
Ian Collins <ia******@hotmail.com> writes:
No. While signals are OT here, the general concept of passing C++
method to C libraries isn't.


??? I thought this newsgroup is about standard C++.
And what other parts of the C++ standard are off topic here?

ImRe
Mar 31 '06 #4
thanks guys!

Mar 31 '06 #5
Imre Palik wrote:
Ian Collins <ia******@hotmail.com> writes:

No. While signals are OT here, the general concept of passing C++
method to C libraries isn't.

??? I thought this newsgroup is about standard C++.
And what other parts of the C++ standard are off topic here?

Standard C++ knows nothing about signals.

--
Ian Collins.
Mar 31 '06 #6
Ian Collins wrote:
Imre Palik wrote:
Ian Collins <ia******@hotmail.com> writes:
No. While signals are OT here, the general concept of passing C++
method to C libraries isn't.

??? I thought this newsgroup is about standard C++.
And what other parts of the C++ standard are off topic here?


Standard C++ knows nothing about signals.


See 18.7.1, Table 22. C++ knows about:

Macros: SIGABRT SIGILL SIGSEGV SIG_DFL SIG_IGN SIGFPE SIGINT SIGTERM SIG_ERR
Type: sig_atomic_t
Functions: raise signal
Alan
Mar 31 '06 #7
ni*******@gmail.com wrote:
Hey guys --

I have an idea on how to make my program a bit better -- but I'm not
sure if I can do it.

Do any of you know if there is a way to have a non-static function trap
a signal from the OS? The most interesting data from my program is only
output at the very end of the program, but sometimes I realize that I
need to stop it sooner (e.g. i set a parameter such that it is running
longer than I anticipated OR it crashes OR PBS wants to kill it) and it
would be nice if before quitting I could get it to print the best
results that it has so far (from all processors when run in parallel if
possible).


Why not log the "best result so far" periodically? If your
job is killed by the scheduler or crashes or etc. you are
left with the last version of the file containing the most
recent results.

For that nasty case that your job is killed during the
actual writing of this log, you might want to produce a
backup each time and write to a copy.

HTH,
- J.
Mar 31 '06 #8
Alan Johnson wrote:

Standard C++ knows nothing about signals.


See 18.7.1, Table 22. C++ knows about:

Macros: SIGABRT SIGILL SIGSEGV SIG_DFL SIG_IGN SIGFPE SIGINT SIGTERM
SIG_ERR
Type: sig_atomic_t
Functions: raise signal

Thanks for the reference, my lesson for the day.

--
Ian Collins.
Mar 31 '06 #9
Ian Collins wrote:

Standard C++ knows nothing about signals.


It knows about signal and raise, and a handful of default signals. Along
with Standard C, it doesn't know about asynchronous signals (i.e.
signals raised other than by calling raise).

--

Pete Becker
Roundhouse Consulting, Ltd.
Apr 1 '06 #10

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

Similar topics

4
by: Wugi | last post by:
I'm trying to find an equivalent of key-event trapping which is easy in QBasic. Several of my programs build up a geometric figure with two parameter line families, eg with two nested for..next...
2
by: Ishwar Rattan | last post by:
Here is a piece code (according to blurb on os.wait, the lower order 7 bits of exit status of process should contain the signal number of signal that terminated the process..) and signal number...
5
by: Klaus Neuner | last post by:
Hello, consider the following two programs: # (1) import sys, signal def alarm_handler(signum, frame): raise
13
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
3
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.*/...
3
by: LeTubs | last post by:
Hi I'm not sure if this is correct ...place to post but here goes This is what i'm trying to do, I want to write a signal / alarm handler ( I don't know which hence the posting here, as once I...
11
by: Jackie | last post by:
Hi everyone, I'd like to know when and how signals are used (e.g. SIGFPE, SIGABRT, SIGTERM, SIGSEGV, SIGINT)? Thank you so much.
2
by: None | last post by:
Hello, 1. The prototype of function signal in signal.h: void (*signal(int sig, void (*func)(int)))(int); is some complex to me. Would you please explain it to me in detail with the C...
2
by: Captain Nemo | last post by:
I'm still using Office 2000 myself, but some of my clients have Office 2003. I've recently added a piece of code to create an instance of Word, open a document, fill in the blanks and become...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...

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.