Solved

signal(..),sigaction(..)  which is better and why??

Posted on 1999-01-27
2
1,155 Views
Last Modified: 2013-12-26
I am finding a need to be able to handle signals, and am not sure what proc is better.  I have used signal in the past, for small programs, for time outs and the such.  Could someone give me a simple example of sigaction?

Also, where can I find where what the signals mean? or more info about them.  

thanks
0
Comment
Question by:kellyjj
2 Comments
 
LVL 3

Accepted Solution

by:
_Scotch_ earned 150 total points
ID: 1296408
Taking your questions in reverse order...  A good tutorial on
signal handling may be found at:

http://grove.ufl.edu./usr/share/doclib/online/DOCS/HTML/APS33DTE/DOCU_006.HTM

I'm not affiliated with that site in anyway... Its just the first
likely looking link I found with a quick altavista search.

On the signal vs sigaction question:

sigaction is more robust.  It allows you to specify handlers for
signals and get information about the handler you're replacing or
it allows you to simply ask what the current signal handlers are
without changing anything.

Here's a short example of using sigaction to catch and handle
alarm signals and ^C signals.  My platform was Digital Unix.
YMMV.

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

//
//      Alarm clock handler - printf for illustrative purposes only
//
void alarm_handler(int foo)  {
        printf("Alarm timer fired.\n");
        alarm(6);
}

//
//      ^C Handler - printf for illustrative purposes only
//

int interrupts_allowed = 5;

void interrupt_handler(int foo)  {
        if (--interrupts_allowed)
                printf("^C handled you have %d left.\n", interrupts_allowed);
        else
                exit(0);
}

int main()  {

struct sigaction int_action = { &interrupt_handler, NULL, NULL };
struct sigaction tim_action = { &alarm_handler, NULL, NULL };
struct sigaction old_int_action;
struct sigaction old_tim_action;

        sigaction(SIGINT, &int_action, &old_int_action);
        printf("^C Handler set.\n");
        printf("Old Handler: %X\n", old_int_action.sa_sigaction);
        printf("^C when you feel like it.\n\n");

        sigaction(SIGALRM, &tim_action, &old_tim_action);
        printf("Alarm handler set, setting alarm for 6 second intervals.\n");
        printf("Old Handler: %X\n\n", old_tim_action.sa_sigaction);
        alarm(6);

        printf("ZZZZzzzz..... .... ... .. .\n\n");

        while (1)
                pause();
}

0
 
LVL 2

Author Comment

by:kellyjj
ID: 1296409
thanks, that is the answer I was looking for.  
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Expand macro to include alternate header placement and naming 5 75
Replacement selected text 2 63
fizzArray2 challenge 1 91
sumHeights  challenge 17 74
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa‚Ķ
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

860 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