Solved

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

717 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