0

I have this solution for an exercise I was doing but when I try to run it and send SIGUSR1 the terminal shows "Stack fault", and I don't know why.

It shows nothing when I send SIGUSR2, but the thing is that with SIGTERM and SIGINT the program works.

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>

void manejador(int signum){
    switch (signum){
    case SIGUSR1:
        printf("He recibido la señal SIGUSR1\n");
        break;
    case SIGUSR2:
        printf("He recibido la señal SIGUSR2\n");
        break;
    case SIGTERM:
        printf("Fin de ejecucion\n");
        exit(EXIT_SUCCESS);
    }
}

int main(int argc, char *argv[])
{
    if (signal(SIGUSR1, manejador) == SIG_ERR) {
    perror("error en la señal SIGUSR1");
    exit(EXIT_FAILURE);
}
if (signal(SIGUSR2, manejador) == SIG_ERR) {
    perror("error en la señal SIGUSR2");
    exit(EXIT_FAILURE);
}
if (signal(SIGTERM, manejador) == SIG_ERR) {
    perror("error en SIGTERM");
    exit(EXIT_FAILURE);
}
if (signal(SIGINT, SIG_IGN) == SIG_ERR) {
    perror("error en SIGINT");
    exit(EXIT_FAILURE);
}

while (1)
    pause();
}
Garf365
  • 3,619
  • 5
  • 29
  • 41
alberto
  • 29
  • 5
  • Which operating system are you running this on ? Are you certain you have properly built/compiled this code ? – nos Aug 25 '16 at 13:21
  • 2
    Possible duplicate of [How to avoid using printf in a signal handler?](http://stackoverflow.com/questions/16891019/how-to-avoid-using-printf-in-a-signal-handler) – LPs Aug 25 '16 at 13:21
  • and take also a look[at this SO answer](http://stackoverflow.com/a/9547988/3436922) – LPs Aug 25 '16 at 13:22
  • I didn´t know this could be caused by printf. I´m running the last version of Debian @nos – alberto Aug 25 '16 at 13:48
  • from the man page for `signal()` -- The behavior of signal() varies across UNIX versions, and has also var‐ ied historically across different versions of Linux. Avoid its use: use sigaction(2) instead. – user3629249 Aug 25 '16 at 17:25
  • the function: `perror()` outputs the system message that is currently associated with the variable `errno`, probably not what you want. Suggest using `write()` as that function can be safely used inside a signal handler; while `perror()` cannot be safely used inside a signal handler. – user3629249 Aug 25 '16 at 17:38
  • Thanks, I will always think about that tips. I was starting to believe all the solutions to signal exercises were wrong @user3629249 – alberto Aug 25 '16 at 18:53

0 Answers0