Link to home
Start Free TrialLog in
Avatar of arut
arut

asked on

catching a signal !

I want to catch the SIGINT signal without using the pause( ) call in
my program.Basically I should be able to catch a signal without having
to wait for the signal to occur. My signal handler should get executed
whenever the signal gets delivered to my process.

Currently I have to use pause(. )

Is there a way out...

Kindly let me know.

Regards,
arut
ASKER CERTIFIED SOLUTION
Avatar of jlevie
jlevie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of montenegro
montenegro

It is easy to catch a signal without pause(), like this:

use the function "signal" to install a signal handler ( a function made by you that is called when your process receives a certain signal), in your case:

signal(SIGINT, handler);

where "handler" is a function made by you with this aspect (it can have any other name):

void handler(int signo)
{
      here you do whatever you whant to do when SIGINT is catched...

}

signo is the number of the signal that called this handler , in your case SIGINT.

try it...
            montenegro.
Avatar of arut

ASKER

I have registered a signal handler but the handler does
not get executed because the signal itself does not
get delivered unless the program has a pause( )

All I want is ...

signal(SIGINT,handler);

do some processing...

if signal has come the handler gets executed automatically


-------------------------------------------

I don't want to use a pause( ), which suspends the process till the signal is received.

If I wait for the signal I can't do other work in the program...

Whenevr the signal comes handler should be executed.
Currently I am not able to do this without pause ( ).
Well, then it may not be coded correctly. The following is a little demonstration program that works:

cat >gork.c <<EOF
#include <stdio.h>

void catcher()
{
  printf("Caught SIGINT\n");
  exit(1);
}
#include <signal.h>

main()
{
  /*
   * Register my signal handler
   */
  signal(SIGINT, &catcher);
  (void) getchar();
  printf("Normal exit\n");
  exit(0);
}
EOF

Compile it and run it. It'll wait for either something to be typed at the keyboard or a SIGINT to be delivered.
use the following

signal(SIGINT,sig_catch);

 when SIGINT event occurs the function named "sig_catch" will be
executed. so keep the code inside the sig_catch function which must me executed when SIGINT occurs.
         here u can do other work when the SIGINT occurs it executes the sig_catch function.u need not have to use pause.