Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

catching a signal !

Posted on 2000-02-16
5
Medium Priority
?
753 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 150 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Screencast - Getting to Know the Pipeline

916 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