Solved

catching a signal !

Posted on 2000-02-16
5
709 Views
Last Modified: 2008-03-06
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
0
Comment
Question by:arut
5 Comments
 
LVL 40

Accepted Solution

by:
jlevie earned 50 total points
ID: 2531773
Yep, you need to register a signal handler to catch the exception. Take a look at the man pages for signal() and sigaction() and friends.
0
 

Expert Comment

by:montenegro
ID: 2533268
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.
0
 

Author Comment

by:arut
ID: 2533935
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 ( ).
0
 
LVL 40

Expert Comment

by:jlevie
ID: 2535133
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.
0
 

Expert Comment

by:abhishekpatel
ID: 2584569
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.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now