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

Home Posts Topics Members FAQ

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 2101

<ma***********@ gmail.com> wrote in message
news:11******** *************@u 72g2000cwu.goog legroups.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************ *********@u72g2 000cwu.googlegr oups.com>,
ma***********@g mail.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*********@so rry.bitbuck.cmm > wrote in comp.lang.c:

<ma***********@ gmail.com> wrote in message
news:11******** *************@u 72g2000cwu.goog legroups.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.l earn.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.nr c-cnrc.gc.ca
(Walter Roberson) wrote:
In article <11************ *********@u72g2 000cwu.googlegr oups.com>,
ma***********@g mail.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.ne t
Mar 27 '06 #5

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

Similar topics

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
3
2680
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 know the proper name /useage then I'll use google to try and find out more information ).... Thus any tips or pointers would be helpful (and I'm using a UNIX variant as OS)
11
2975
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
17723
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() { printf("\n\n ********** F D S Message ***********\n\n"); printf("\n\n ********** S E G M E N T A T I O N F A U L T inside F D S ***********\n\n");
18
3920
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 the time when the program was started the first time. My platform is a DEC machine with Tru64 onboard. A possible explanation could be, that the time() function is called
10
4035
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 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...
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.
10
2149
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 the signal? The following code is recieveing recursive signals. 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <unistd.h> 5
9
5838
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
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
10157
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10097
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
9957
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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...
0
5518
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
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.