Solved

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

Posted on 1999-01-27
2
1,112 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
Comment Utility
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
Comment Utility
thanks, that is the answer I was looking for.  
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

9 Experts available now in Live!

Get 1:1 Help Now