Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Q: <signal.h>

5 views
Skip to first unread message

Hiro

unread,
Aug 29, 2002, 9:34:29 AM8/29/02
to
Hi;

I'm learning ANSI C and confused to using <signal.h>.

I wrote a sample program;

*****
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void ctrlc(int i)
{
printf("CTRL-C hit!\n");
printf("i in ctlrc = %d\n",i);
exit(0);
}

int main(void)
{
int i = 1;

signal(SIGINT, ctrlc);

for(;;);
return NULL;
}


Whatever number I assigned to i in main function, the program say " i
in ctrl = 2". How this happen? And how to pass parameters used in main
function to handler?

Jens.T...@physik.fu-berlin.de

unread,
Aug 29, 2002, 9:58:44 AM8/29/02
to

The signal handler gets the signal number as its only argument - and
the signal number of SIGINT is 2. You can't pass any variables from
main() to the signal handler, the only way to do this is having a
global variables. The 'i' in main() is a local variable, no other
function is able to see it. You will have to move it out of main().
Bu then you should use a different variable name as the argument of the
signal handler, because otherwise it will hide the global variable.

BTW, if you're using a global variable like this, i.e. in a function and
in a signal handler it's usually a good idea to declare it as 'volatile'
to make sure the value stays in sync in both places.

Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Jens.T...@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring

Dan Pop

unread,
Aug 29, 2002, 12:09:28 PM8/29/02
to

>I'm learning ANSI C and confused to using <signal.h>.
>
>I wrote a sample program;
>
>*****
>#include <stdio.h>
>#include <stdlib.h>
>#include <signal.h>
>
>void ctrlc(int i)
>{
> printf("CTRL-C hit!\n");
> printf("i in ctlrc = %d\n",i);

Bzzzt! You can't call printf (or most other standard library functions)
in a signal handler that is invoked asynchronously.

> exit(0);
>}
>
>int main(void)
>{
> int i = 1;
>
> signal(SIGINT, ctrlc);
>
> for(;;);
> return NULL;

Bzzzt! NULL is not a valid return value for a function declared as
returning int. If you mean 0, write 0, NULL is not supposed to be a
replacement for 0, except for certain pointer contexts.

>}
>
>Whatever number I assigned to i in main function, the program say " i
>in ctrl = 2". How this happen?

Well, SIGINT seems to be defined as 2 on your implementation. The
argument of the signal hadler is assigned the value of the signal whose
delivery resulted in the invocation of the handler.

>And how to pass parameters used in main function to handler?

You neither can nor want to do this. If you *must* access the value
of some variable inside the signal handler, this variable must be global
and its type should be volatile sig_atomic_t.

To stay away from undefined behaviour, your signal handler should only
set a global variable (again of type volatile sig_atomic_t) and possibly
reenable the signal handler (if necessary) with another signal() call.

Let the main loop of your program check whether a signal has been
delivered, by testing the value of the global variable that is set by the
signal handler.

Here's an example of how to do it properly:

#include <stdio.h>
#include <signal.h>

volatile sig_atomic_t gotsig = 0;

void handler(int signo)
{
gotsig = signo;
}

int main()
{
signal(SIGINT, handler);
puts("Press the interrupt key to exit.");
while (gotsig == 0) ;
printf("The program received signal %d.\n", (int)gotsig);
return 0;
}

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan...@ifh.de

Hiro

unread,
Aug 29, 2002, 8:14:27 PM8/29/02
to
Very Thanks

Dan...@ifh.de (Dan Pop) wrote in message news:<aklgvo$12n$1...@sunnews.cern.ch>...

Jack Klein

unread,
Aug 29, 2002, 11:53:49 PM8/29/02
to
On 29 Aug 2002 13:58:44 GMT, Jens.T...@physik.fu-berlin.de wrote
in comp.lang.c:

Actually, it must be defined as volatile and of type sig_atomic_t if
you want the behavior to be defined.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

0 new messages