V
vippstar
What is the purpose of signals and why do they exist in C?
thanks in advance
thanks in advance
What is the purpose of signals and why do they exist in C?
What is the purpose of signals and why do they exist in C?
thanks in advance
What is the purpose of signals and why do they exist in C?
thanks in advance
Perhaps you'll feel better if you hear that I am a hobby programmer.That sounds eirily like a class assignment question...
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.
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.
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.
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."
There is something which can be done in portable C, but it is not much.vippstar said:Thanks mr Sossman, but I still don't see the reason why ISO C would
need signals.
Thanks mr Sossman, but I still don't see the reason why ISO C would
need signals.
Mark McIntyre said: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.
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".
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.