Solved

catching a signal !

Posted on 2000-02-16
5
732 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

752 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