Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 1999-01-27
2
Medium Priority
?
1,216 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 600 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
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.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

618 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