473,797 Members | 3,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why do signals exist?

What is the purpose of signals and why do they exist in C?
thanks in advance
Jan 28 '08 #1
11 1840
vi******@gmail. com wrote:
What is the purpose of signals and why do they exist in C?
thanks in advance
Signals are C's model of mechanisms most operating environments
provide to handle two kinds of events: erroneous or exceptional
conditions arising from a program's own actions (for example, when
it divides by zero or dereferences NULL), and interventions from
outside the program (for example, hitting ^C to interrupt it). A
C program can set up "signal handlers," special functions that are
called when these exceptional or external events occur; the handler
can respond to the event in some appropriate way.

The details of exactly how these events arise, are dispatched,
and are processed vary greatly from one O/S to another, so the
C Standard says very little about what really goes on: It doesn't
list all the possible signals, it doesn't say which can be ignored
and which cannot, it doesn't say what happens if a signal handler
tries to resume normal operation after most signals. In a sense,
the Standard's mechanism for signals is mostly a "stub," a plain-
vanilla interface to a realm whose details are mostly outside the
Standard itself. Thus, there is not a lot you can do with signals
in portable C code, because even though the signal() function is
a portable interface, the thing it interfaces with is non-portable.
A program that makes non-trivial use of signals is usually tied
pretty tightly to the specifics of a particular O/S.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jan 28 '08 #2
vi******@gmail. com wrote:
What is the purpose of signals and why do they exist in C?
thanks in advance
They exist to provide asynchronous notification of events to the
program. The C99 Standard defines six standard signals. Implementations
may define more and almost everything about handling signals is
implementation defined. POSIX provides richer signal support and more
guarantees on behaviour.

See section 7.14 of the Standard.

Jan 28 '08 #3
On Jan 28, 8:34 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
vipps...@gmail. com wrote:
What is the purpose of signals and why do they exist in C?
thanks in advance

Signals are C's model of mechanisms most operating environments
provide to handle two kinds of events: erroneous or exceptional
conditions arising from a program's own actions (for example, when
it divides by zero or dereferences NULL), and interventions from
outside the program (for example, hitting ^C to interrupt it). A
C program can set up "signal handlers," special functions that are
called when these exceptional or external events occur; the handler
can respond to the event in some appropriate way.

The details of exactly how these events arise, are dispatched,
and are processed vary greatly from one O/S to another, so the
C Standard says very little about what really goes on: It doesn't
list all the possible signals, it doesn't say which can be ignored
and which cannot, it doesn't say what happens if a signal handler
tries to resume normal operation after most signals. In a sense,
the Standard's mechanism for signals is mostly a "stub," a plain-
vanilla interface to a realm whose details are mostly outside the
Standard itself. Thus, there is not a lot you can do with signals
in portable C code, because even though the signal() function is
a portable interface, the thing it interfaces with is non-portable.
A program that makes non-trivial use of signals is usually tied
pretty tightly to the specifics of a particular O/S.

Thanks mr Sossman, but I still don't see the reason why ISO C would
need signals.
I understand why a standard like POSIX would need them, but I don't
understand ISOs decision to include them in standard C.
I also think you misunderstood my question, perhaps it was poorly
worded.
I do know what signals are for (as a concept in programming) but I did
not understand the concept behind signals in ISO C, and their use.
Then again, perhaps it was me who misunderstood.
Jan 28 '08 #4
On Jan 28, 8:37 pm, santosh <santosh....@gm ail.comwrote:
vipps...@gmail. com wrote:
What is the purpose of signals and why do they exist in C?
thanks in advance

They exist to provide asynchronous notification of events to the
program. The C99 Standard defines six standard signals. Implementations
may define more and almost everything about handling signals is
implementation defined. POSIX provides richer signal support and more
guarantees on behaviour.

See section 7.14 of the Standard.
What greatly annoys me is that I must have signals in mind when
programming in ISO C.
I believe they should not be part of the language and I see no reason
for them to be.
I am also aware of POSIX signals, and it makes sense for such standard
to have signals but not for ISO C.
Jan 28 '08 #5
vi******@gmail. com wrote:
On Jan 28, 8:37 pm, santosh <santosh....@gm ail.comwrote:
>vipps...@gmail .com wrote:
What is the purpose of signals and why do they exist in C?
thanks in advance

They exist to provide asynchronous notification of events to the
program. The C99 Standard defines six standard signals.
Implementation s may define more and almost everything about handling
signals is implementation defined. POSIX provides richer signal
support and more guarantees on behaviour.

See section 7.14 of the Standard.

What greatly annoys me is that I must have signals in mind when
programming in ISO C.
You don't have to. You can completely ignore the existence of signals if
you want to. In fact, that's what most small C programs will do. The OS
and your C runtime library will take the default action for any signals
delivered, which usually means termination.
I believe they should not be part of the language and I see no reason
for them to be.
You may have a point.
I am also aware of POSIX signals, and it makes sense for such standard
to have signals but not for ISO C.
As such there is not much that ISO C /does/ define about signals, and
fortunately, it's not something you need to consider if you don't want
to. If you do need to consider signals, you'll very likely find
yourself actual coding to a more elaborate standard like POSIX.

Jan 28 '08 #6
vi******@gmail. com wrote:
On Jan 28, 8:34 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
>vipps...@gmail .com wrote:
>>What is the purpose of signals and why do they exist in C?
thanks in advance
Signals are C's model of mechanisms most operating environments
provide to handle two kinds of events: erroneous or exceptional
conditions arising from a program's own actions (for example, when
it divides by zero or dereferences NULL), and interventions from
outside the program (for example, hitting ^C to interrupt it). A
C program can set up "signal handlers," special functions that are
called when these exceptional or external events occur; the handler
can respond to the event in some appropriate way.

The details of exactly how these events arise, are dispatched,
and are processed vary greatly from one O/S to another, so the
C Standard says very little about what really goes on: It doesn't
list all the possible signals, it doesn't say which can be ignored
and which cannot, it doesn't say what happens if a signal handler
tries to resume normal operation after most signals. In a sense,
the Standard's mechanism for signals is mostly a "stub," a plain-
vanilla interface to a realm whose details are mostly outside the
Standard itself. Thus, there is not a lot you can do with signals
in portable C code, because even though the signal() function is
a portable interface, the thing it interfaces with is non-portable.
A program that makes non-trivial use of signals is usually tied
pretty tightly to the specifics of a particular O/S.


Thanks mr Sossman, but I still don't see the reason why ISO C would
need signals.
I understand why a standard like POSIX would need them, but I don't
understand ISOs decision to include them in standard C.
I also think you misunderstood my question, perhaps it was poorly
worded.
I do know what signals are for (as a concept in programming) but I did
not understand the concept behind signals in ISO C, and their use.
Then again, perhaps it was me who misunderstood.
Since most uses of signals rely on things the C Standard
does not describe, it's reasonable to wonder why the loose
descriptions are there at all. But even though the nature
and the processing of signals are highly system-specific, the
Standard still has reason to say a few things about them. For
example, the Standard describes how signals must behave w.r.t.
sequence points, thus allowing the programmer to rely on the
values of certain variables and warning him to steer clear of
others. The Standard requires that after a signal handler
returns (if it can), the interrupted function proceeds as if
nothing had happened; a signal will not, for example, restart
a loop that was already in progress. This guarantee applies
even if the signal handler calls the interrupted function; thus,
a compiler cannot say "This function calls no others and hence
cannot be called recursively; I'll take shortcuts."

So the Standard needs to talk about signals a little bit,
to constrain their behavior w.r.t. the "base" language and give
the programmer a few tools for dealing with them. Since a bare-
bones notion of signals is already present in the Standard, it
is used in a few convenient places, too: arithmetic overflow may
provoke a signal, abort() is defined in terms of SIGABRT, and
so on. Most of the serious work with signals is outside the
Standard's realm, but the Standard has a need to make a few
minimal specifications anyhow.

If the Standard is a "contract" between the C implementor
and the C programmer, the part about signals is a "treaty"
between the C implementation and the foreign realm called the O/S.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jan 28 '08 #7
In article <h7************ *************** ***@comcast.com >,
Eric Sosman <es*****@ieee-dot-org.invalidwrot e:
>The Standard requires that after a signal handler
returns (if it can), the interrupted function proceeds as if
nothing had happened; a signal will not, for example, restart
a loop that was already in progress. This guarantee applies
even if the signal handler calls the interrupted function; thus,
a compiler cannot say "This function calls no others and hence
cannot be called recursively; I'll take shortcuts."
However, if the signal handler refers to any object with static
storage duration, and the operation is not -writing- to an
object of sig_atomic_type , the interrupted function might not
proceed "as if nothing had happened".

The C89 wording specifically prohibits calling any function in the
standard library and then talks about references to objects with static
storage duration. It seems to me that logically the implication would
be that if the signal handler calls some function that refers to an
object with static storage duration (other than writing
sig_atomic_type ) that the behaviour is undefined just as if the
signal handler itself had referred to the prohibitted object;
however the transitivity of the constraint is not explicitly specified
in the C89 wording, so other people might have different interpretations .
--
"Okay, buzzwords only. Two syllables, tops." -- Laurie Anderson
Jan 28 '08 #8
vippstar wrote:
Thanks mr Sossman, but I still don't see the reason why ISO C would
need signals.
There is something which can be done in portable C, but it is not much.
For example
#include <signal.h>
#include <stdio.h>
static sig_atomic_t stop = 0;
static void handle(int sig) { stop = 1; }
int main(void)
{
signal(SIGINT, handle);
while (!stop)
puts("AAAAAAAAA !");
puts("Ok, I'll stop it.);
return 0;
}

(Well, the way to send a SIGINT is implementation defined, but so is the
way to cause getchar() to return EOF if stdin is interactive...)
--
Army1987 (Replace "NOSPAM" with "email")
Jan 28 '08 #9
vi******@gmail. com wrote:
On Jan 28, 8:34 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
> Signals are C's model of mechanisms most operating environments
provide to handle two kinds of events: erroneous or exceptional
conditions arising from a program's own actions

Thanks mr Sossman, but I still don't see the reason why ISO C would
need signals.
The ISO standard doesn't codify what is *needed* in some absolute sense.
It codifies what the committee members felt was useful enough to
standardise, and what could be standardised without losing usefulness.
Presumably they felt that some standardised handling of signal events
would be useful.

On the other hand, graphics, while very useful, couldn't be standardised
sensibly. On the other other hand, quite possibly if C were being
standardised today then networking would be included, since TCP/IP and
sockets are universal enough. However retrofitting it wouldn't be useful
- its already standardised elsewhere.

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >
Jan 28 '08 #10

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

Similar topics

1
6097
by: Isidro Vila Verde | last post by:
Greetings, I need to handle signals to close some excel applications that my script open, when the script is killed by another process. My script is a little bit complex, but I wrote just two very small scripts to test the kill and signal. The scripts signal.pl is this one: map {$SIG{$_} = sub {my $n = shift; print "n=$n\n"}} keys %SIG;
4
16311
by: Gabriele Bartolini | last post by:
Hi, I am writing an application in C++ on Linux, using threads (this is my first experience with pthreads). The application itself is fine, it is just that I wanted to handle asynchronous signals correctly, in particular the SIGINT, preventing the application from stopping ungracefully. Indeed, I'd like to catch the signal, close everything in a consistent way and stop the application.
1
1658
by: Leo Kirch | last post by:
Hello XSLT gurus, i've got a rather difficult problem. Some explanations first. theres a signal oriented xml-file - the graphical represantation looks like: | startsignal (signal00) \ / ---------------------------------------------------------
4
6917
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
4
9214
by: junky_fellow | last post by:
what is the difference between signals SIGBUS and SIGSEGV ? when does an application program receive SIGBUS and in which cases SIGSEGV ? thanx in advance for any help ...
0
1946
by: Arnaud Debaene | last post by:
Hello all. I've got a bunch of existing, non managed, C++ DLLs that export types with, among other things, public events implemented using the boost::signals library. Now, I need to have these DLL interoperate with managed code. Among others things, the managed code need to be able to register for notification with the boost signals. I've tried different approaches (trying to register a pinned delegate with the boost::signal, etc...),
0
1013
by: Brian Vanderburg II | last post by:
I don't know if any such support is already built in, so I ended up making my own simple signals/slots like mechanism. If anyone is interested then here it is, along with a simple test. It can connect to normal functions as well as instance methods. It also supports weak connections where when an object is gone, the slot is gone as well, the slot just holds a weak reference to the object. Brian Vanderburg II
1
1790
by: Scott SA | last post by:
On 5/1/08, Brian Vanderburg II (BrianVanderburg2@aim.com) wrote: Did you review this? <http://pydispatcher.sourceforge.net/> from what I understand is originally based upon this: <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/87056> and subsequently integrated into this:
2
1564
by: wongjoekmeu | last post by:
Dear All, I have some a program in which I link a static library. The static library has a initialize() and uninitialized() function. Now when I call the initialize function a thread is being started up and it makes use of the linux timer and start sending signals in a certain frequency. This is a problem as when I use the getch() function I see that something is being written continuously to the console, which is not a satisfactory...
0
9537
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,...
0
10469
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
10246
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
10209
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
6803
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
5459
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4135
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
3750
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.