Solved

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

Posted on 1999-01-27
2
1,142 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to create frequencies of a variable from SAS dataset? 10 125
Whole sheet autoscrub still needed 19 51
Problem to open Excel file 15 135
Problem to App 4 83
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

810 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