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

Home Posts Topics Members FAQ

Best Way to Print Nature of Signal


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,c rash);
signal(SIGBUS,c rash);
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
the error the user of the program would have seen so that whoever
runs the program knows that something bad happened? With the
signal handler, the program silently finishes which might make
someone think it was successful when in reality, it just did nothing
and abended. Thank you.
--

Martin McCormick WB5AGZ Stillwater, OK
Information Technology Division Network Operations Group
Nov 13 '05 #1
3 3525
In article <bn**********@h ydrogen.cis.oks tate.edu>,
Martin McCormick <ma****@okstate .edu> wrote:

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
the error the user of the program would have seen so that whoever
runs the program knows that something bad happened? With the
signal handler, the program silently finishes which might make
someone think it was successful when in reality, it just did nothing
and abended. Thank you.


Be aware (I have to say this or people will jump on me and start
stuffing socks down my throat) that it's impossible (or at least
Extremely Difficult) to do this without invoking undefined behavior,
because the set of functions you're allowed to call from a signal handler
is extremely limited[1]. There's a good chance, however, that your
implementation defines the behavior of doing something like this to be
"what you would expect", and you're almost certainly already relying on
this to be able to clean up the lock before the program terminates.

For more details on what your implementation (as opposed to the language)
allows you to do in a signal handler, a newsgroup for your platform may
be more useful.

The signal handler gets the signal as its parameter, so a crash() that
looks something like this will probably come close to doing what you want:
--------
void crash(int sig)
{
do_cleanup();
#if USE_ALTERNATIVE _1
/*Alternative 1: Now that we've done the cleanup, let the default
signal handler dump core or whatever
*/
if(signal(sig,S IG_DFL)==SIG_ER R);
{
fputs("This is odd, can't revert to default signal handler\n",stde rr);
abort();
}
raise(sig);
/*We shouldn't get here, but to be sure:*/
return;
#else
/*Alternative 2: After doing cleanup, inform user that we terminated
abnormally, but bypass the default signal handler
*/
fprintf(stderr, "Terminatin g abnormally (signal %d received)\n",si g);
exit();
#endif
}
--------
The boring standardese details:
Alternative #2 isn't allowed for asynchronous signals (which the ones
you're handling probably are), because you're not allowed to call most
standard library functions[1]. You're probably not doing anything worse
than you're already doing in do_cleanup(), though.
Alternative #1 is slightly more problematic, since you're not allowed
to call raise() in a signal handler invoked by calling raise() either.
In practice, though, it (like alternative #2) isn't likely to be any
worse than do_cleanup() is already doing.
dave

[1] To be precise, signal() with a first argument equal to the signal
that resulted in the handler being called or abort() when handling
an asynchronous signal, (rather more generously) anything but raise()
when handling a signal raised by raise().

--
Dave Vandervies dj******@csclub .uwaterloo.ca
If he'd got it wrong (and it happens to us all), several people would
have jumped on him and started stuffing socks down his throat.
--Richard Heathfield in comp.lang.c
Nov 13 '05 #2
ma****@okstate. edu (Martin McCormick) writes:
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,c rash);
signal(SIGBUS,c rash);
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
the error the user of the program would have seen so that whoever
runs the program knows that something bad happened? With the
signal handler, the program silently finishes which might make
someone think it was successful when in reality, it just did nothing
and abended. Thank you.


Unfortuately, there is no way to do what you want in standard
C. Any of the standard facilities for writing text to a file
(including the terminal) may not be called from a signal handler
that was called due to an exception. If the handlers were
expected to return normally, then I would have recommended
writing to an object declared with "volatile sig_atomic_t" type
and check for it elsewhere; but as they probably don't
(/shouldn't) return, you can't do that.

<OT>
However, as you seem to be using non-standard signals to begin
with (SIGBUS), and apparently expect ctrl-C to generate SIGINT,
I'll hazard an educated guess that you're on a UNIX-style system
with access to POSIX extensions. So I'd recommend that you post
this question on comp.unix.progr ammer, where they will very
likely be able to help you take advantage of non-"Standard C"
POSIX extensions that may aid you. I've taken the liberty of
cross-posting this there: but please follow-up to there *only*, if
you will not be discussing ISO C.

POSIX allows you to call such functions as write(), which would
allow you to write a message to the terminal; but there may still
be unflushed data that is in the stdout/stderr buffer, but still
not yet actually written out: you'll need to take care of that.

You might also consider adding whether or not this handler would
be appropriate for other signals such as SIGABRT, SIGHUP, SIGTERM...
</OT>

HTH, HAND.
--
Micah J. Cowan
mi***@cowan.nam e
Nov 13 '05 #3
Hi Martin!
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,c rash);
signal(SIGBUS,c rash);
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
the error the user of the program would have seen so that whoever
runs the program knows that something bad happened? With the
signal handler, the program silently finishes which might make
someone think it was successful when in reality, it just did nothing
and abended. Thank you.


If you have a POSIX conform implementation, you can use write() (but not
the printf() family!) in your signal handler. The function write() is
async-signal-safe, which means that you can use it without any problem
in signal handler. The standard file descriptor for stdout resp. stderr are
STDOUT_FILENO, resp STDERR_FILENO.

BTW, use sigaction() instead of signal() (though I believe that modern
Un*x implement signal() using sigaction()). This will make your program
more "reliable" wrt. signals catching.

HTH,
Loic.
--
Article posté via l'accès Usenet http://www.mes-news.com
Accès par Nnrp ou Web
Nov 13 '05 #4

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

Similar topics

2
2725
by: vincent delft | last post by:
I want to write a script that will monitore the performance of a web application delivering HTTP and HTTPS pages. I would like to know the best way to do it... By reading Python Doc. I've found the following: For HTTP: def http_monitor(host,url): starttts=time.time() conn=httplib.HTTPConnection(host)
14
2919
by: Marcin Ciura | last post by:
Here is a pre-PEP about print that I wrote recently. Please let me know what is the community's opinion on it. Cheers, Marcin PEP: XXX Title: Print Without Intervening Space Version: $Revision: 0.0 $
20
10867
by: Joel Hedlund | last post by:
Hi all! I use python for writing terminal applications and I have been bothered by how hard it seems to be to determine the terminal size. What is the best way of doing this? At the end I've included a code snippet from Chuck Blake 'ls' app in python. It seems to do the job just fine on my comp, but regrettably, I'm not sassy enough to wrap my head around the fine grain details on this one. How cross-platform is this? Is there a more...
6
2868
by: seb | last post by:
Hi, I am using pygtk for the first times. I am wondering what would be the best "pattern" to interface pygtk with a thread. The thread is collecting informations (over the network for example) or is doing some long calculations.
3
2690
by: Memfis | last post by:
What is the best practice for using boost::signal? Should the signal be a public field? Should an accessor method be used? Should there be some special connection methods for every signal, like the following: private: signal<void()x; public: void connectX(const signal<void()>::slot_type& slot); void disconnectX(const signal<void()>::slot_type& slot);
8
11370
by: sam_cit | last post by:
Hi, I hear many say that it is better to convert address, i mean, type cast into (void*) and print using %p identifier rather than %u, could you let me know the specific reason behind it?
43
7587
by: dev_cool | last post by:
Hello friends, I'm a beginner in C programming. One of my friends asked me to write a program in C.The purpose of the program is print 1 to n without any conditional statement, loop or jump. How is it possible? Please help me. Thanks in advance.
21
8095
by: Owen Zhang | last post by:
What is the best way to implement "tail -f" in C or C++ and higher performance compared to either unix shell command "tail -f" or perl File::Tail ? Any suggestion appreciated. Thanks.
4
10276
by: bukzor | last post by:
Does anyone have a pythonic way to check if a process is dead, given the pid? This is the function I'm using is quite OS dependent. A good candidate might be "try: kill(pid)", since it throws an exception if the pid is dead, but that sends a signal which might interfere with the process. Thanks. --Buck
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
9483
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10096
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8982
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...
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
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.