Installing signal handlers for variables that are defined in a header file in C

377 Views Asked by At

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!

1

There are 1 best solutions below

0
PeCosta On

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;
}