0

So I am working on this code for a class and I have little experience with signal handlers. I have 95% of my code done, however, I am struggling with this bit.

This is what the professor is asking for on this assignment.

Then, main() should install 4 signal handler:
The handler for TIME_OVER_SIGNAL announces that the time is up and sets shouldRun to 0.
The handler for WIN_SIGNAL announces that the user won and sets shouldRun to 0.
The handler for CORRECT_SIGNAL announces that the user got their last guess correct.
The handler for INCORRECT_SIGNAL announces that the user got their last guess wrong, and should start again from the beginning.
NOTE: Perhaps you can make the same handler handle both CORRECT_SIGNAL and INCORRECT_SIGNAL.

I haven't been successful finding any examples online similar to this to steer me in the right direction of what to do.

This is what I basically got out of the explanation and I know for a fact it's probably way off the mark... How do I go about installing these so they output a particular message like is being asked? The only examples I can find online are for generic alarms, sigChld, or to be able to Ctrl^C out of a running program.

void timeOverSignalHandler(int sigINT)  
{  
        printf("Oh no! The time is up!");  
        shouldRun = 0;  
}

I'm confused what arguments should be passed. I'm also confused whether or not I should be doing something different within the handler and when I call the handler within the main I should have the output generate there.

If anybody could be kind enough to guide me in the right direction with this one I would greatly appreciate your help! Any sort of example that would be similar to this would be great, where the handler is used to output a specific phrase to the user. Doesn't have to necessarily be these particular handlers that I need to do for the assignment. This is a C program!

Kevin Gombos
  • 37
  • 1
  • 6
  • 1
    Is it `c` or `c++`? Please choose one. What is the question? –  Feb 08 '17 at 23:04
  • This is a C program, my apologies. – Kevin Gombos Feb 08 '17 at 23:06
  • My question is how do I go about installing these signal handlers that need to be installed. All I seem to be able to find online as far as examples are standard signal handlers for alarms, or Ctrl^C'ing out of a running program. Nothing that prints out a particular output based upon a given circumstance like these. – Kevin Gombos Feb 08 '17 at 23:07
  • You should not define functions in a header file, except `inline` functions. And if your task is not clear, your should ask your prof so he can adjust his didactic approach. Not clear what your problem is. Show your code, read the documentation of the required functions. – too honest for this site Feb 08 '17 at 23:22

1 Answers1

0

Not really sure what your aim is but signals according to GNU:

A signal is a software interrupt delivered to a process. The operating system uses signals to report exceptional situations to an executing program. Some signals report errors such as references to invalid memory addresses; others report asynchronous events, such as disconnection of a phone line.

If you have a 64 bit OS you should be able to use SIGNUM's from 1 to 64, notice some of this include already managed signals like SIGTERM. The following code manages and raises signal 10(SIGUSR1), that I defined with one of your ID's:

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

#define TIME_OVER_SIGNAL 28 


static void catInteractive attention sch_function(int signo) {
    puts("Signal caught.");
}

int main(void) {
    if (signal(TIME_OVER_SIGNAL, catch_function) == SIG_ERR) {
        fputs("An error occurred while setting a signal handler.\n", stderr);
        return EXIT_FAILURE;
    }
    puts("Raising the interactive attention signal.");
    if (raise(TIME_OVER_SIGNAL) != 0) {
        fputs("Error raising the signal.\n", stderr);
        return EXIT_FAILURE;
    }
    puts("Exiting.");
    return EXIT_SUCCESS;
}
PeCosta
  • 537
  • 4
  • 13