473,241 Members | 1,465 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,241 software developers and data experts.

Please explain signal handler behaviour

Why would a signal handler cannot in general call standard library
functions.

I have read whats is said in Standard but needs an explaination on that
as i couldn't uunderstand it.

Thanx in advance

Mar 15 '06 #1
4 2063

<ma***********@gmail.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
Why would a signal handler cannot in general call standard library
functions.

I have read whats is said in Standard but needs an explaination on that
as i couldn't uunderstand it.


From 7.1.2
"4 The functions in the standard library are not guaranteed to be reentrant
and may modify
objects with static storage duration.158)"

"158) Thus, a signal handler cannot, in general, call standard library
functions."

This has to do with the design of the OS. Many OS's only use non-reentrant
functions. That is, the function can't be called again until it completes
it's first call. Since most OS's use non-reentrant functions, the signal
handler would cause some form of undefined behavior: lockup, crash,
segmentation fault, if the signal handler called the same function that was
interrupted. For example, if the signal handler is called and interrupts a
call to printf() and then the signal handler calls printf(), undefined
behavior occurs. This means that most signal handlers are implemented
without using the standard library.
Rod Pemberton


Mar 15 '06 #2
In article <11*********************@u72g2000cwu.googlegroups. com>,
ma***********@gmail.com <ma***********@gmail.com> wrote:
Why would a signal handler cannot in general call standard library
functions. I have read whats is said in Standard but needs an explaination on that
as i couldn't uunderstand it.


A signal handler can interrupt existing operations (including
library calls) between sequence points, possibly even
terminating an time-consuming instruction (such as division)
{requiring that the instruction be restarted when the handler
completes.}

Library calls are allowed to have internal persistant state
that might get overwritten if the same library call were invoked
in the signal handler. Or possibly even a different library call,
as there are tight ties between some of them.

I/O is the obvious example: if a library routine is just about
to update the buffer pointer when an I/O routine gets invoked
in the signal handler, then the result could be program crashes
that were quite difficult to track down.

Rather than put restrictions on the ways that each library function
may use persistant state, the C standard says "Only these very few
things are certain to be safe."

There are other standards, such as POSIX.1, that go into much more
detail about exactly which routines are safe in signal handlers
and which ones are not. POSIX.1 does not invalidate this section
of the C standard, as C allows implementations to offer additional
functionality... but if you used a POSIX.1 guarantee of safety then
as soon as you took your C program to a non-POSIX.1 system, all
bets would be off again.

--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Mar 15 '06 #3
On Wed, 15 Mar 2006 10:21:23 -0500, "Rod Pemberton"
<do*********@sorry.bitbuck.cmm> wrote in comp.lang.c:

<ma***********@gmail.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
Why would a signal handler cannot in general call standard library
functions.

I have read whats is said in Standard but needs an explaination on that
as i couldn't uunderstand it.


From 7.1.2
"4 The functions in the standard library are not guaranteed to be reentrant
and may modify
objects with static storage duration.158)"

"158) Thus, a signal handler cannot, in general, call standard library
functions."

This has to do with the design of the OS. Many OS's only use non-reentrant
functions.


This doesn't necessarily have anything at all to do with the OS.

Admittedly, it would be difficult, but not impossible, to make C
library functions reentrant on a platform where the underlying OS was
not. But it is quite possible to make C library functions
non-reentrant even if the underlying OS is reentrant.

The point is that the C standard does not require standard library
functions to be reentrant regardless of whether or not a host OS makes
it easy, difficult, or flat out impossible.

--
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.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Mar 16 '06 #4
On Wed, 15 Mar 2006 15:31:58 +0000 (UTC), ro******@ibd.nrc-cnrc.gc.ca
(Walter Roberson) wrote:
In article <11*********************@u72g2000cwu.googlegroups. com>,
ma***********@gmail.com <ma***********@gmail.com> wrote:
Why would a signal handler cannot in general call standard library
functions.
I have read whats is said in Standard but needs an explaination on that
as i couldn't uunderstand it.


A signal handler can interrupt existing operations (including
library calls) between sequence points, possibly even
terminating an time-consuming instruction (such as division)
{requiring that the instruction be restarted when the handler
completes.}

Library functions aren't required to contain sequence points except at
entry and exit, and even if they do, it doesn't matter whether an
interrupt occurs at one or not. What matters is whether the interrupt
occurs while the library is "in the middle" of doing something.
Library calls are allowed to have internal persistant state
that might get overwritten if the same library call were invoked
in the signal handler. Or possibly even a different library call,
as there are tight ties between some of them.
Right.
I/O is the obvious example: if a library routine is just about
to update the buffer pointer when an I/O routine gets invoked
in the signal handler, then the result could be program crashes
that were quite difficult to track down.
In that particular case garbled output is rather more likely, but in
general almost any kind of problem up to crashing is possible.
Rather than put restrictions on the ways that each library function
may use persistant state, the C standard says "Only these very few
things are certain to be safe."
Right.
There are other standards, such as POSIX.1, that go into much more
detail about exactly which routines are safe in signal handlers
and which ones are not. POSIX.1 does not invalidate this section
of the C standard, as C allows implementations to offer additional
functionality... but if you used a POSIX.1 guarantee of safety then
as soon as you took your C program to a non-POSIX.1 system, all
bets would be off again.


And the async-safe functions in POSIX are, not by accident, exactly
those that traditionally were implemented in the Unix kernel, and thus
pseudo-uninterruptible or at least only "gracefully" interruptible.

- David.Thompson1 at worldnet.att.net
Mar 27 '06 #5

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

Similar topics

4
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...
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.
13
by: vashwath | last post by:
Hi all, In my current project I am using signals for error handling. Since I cannot show full code, I have just shown important piece of code which is relevant. void sigsegenv() {...
18
by: Sven | last post by:
Hi, I found a strange behaviour when using the time() function from time.h. Sometimes when it is called, it does not show the correct time in seconds, but an initial value. This time seem to be...
10
by: subramanian | last post by:
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...
2
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...
10
by: Udai Kiran | last post by:
Hi all Iam having trouble with signal handler. Where will the execution return after invoking a signal handler when that particular signal is recieved? will return to the point where we register...
9
by: thagor2008 | last post by:
Is the behaviour of throwing exceptions in a unix signal handler defined? eg: void sighandler(int sig) { ... do something throw myobj; }
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.