1

I have a daemon which works under Linux. Program received SIGSEGV signal but it did not stopped immediately, according to log time when it received SIGSEGV is - 01:11:45.085 and time when it stopped to work is - 01:11:48.219 It is multithreaded and on time SIGSEGV was generated it had like 25threads in it.

So, question is how the program managed to work additional 3 second, even though no mo actions were done from the thread which generated SIGSEGV ?

Thanks.

unresolved_external
  • 1,930
  • 5
  • 30
  • 65
  • Maybe `SIGSEGV` is caught or "ignored" and other threads where able to run a few more seconds... Anyway `SIGSEGV` is sent to the offending *thread* (but by default kill the entire process). – Basile Starynkevitch May 23 '13 at 14:01

1 Answers1

3

SIGSEGV will by default kill the process, but you can handle the signal, signal.h is the header in C, if you need to clean up files or communications before exiting for example. I made a short code example that does cause a SIGSEGV and handles it and does something afterwards, just an example, probably not a very good one.

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

void sighandler( int signal);
int main(){
    signal(SIGSEGV,sighandler);
    int* a = NULL;
    a[0] = 5;
    return 0;
}

void cleanup(){
    // do neccesary cleanup
    sleep(3);
    exit(0);
}

void sighandler( int signal){
    if ( signal == SIGSEGV){
        printf("oups, sigsegv\n");
    } else {
        printf("some sig\n");
    }
    cleanup();
}
cybrhuman
  • 156
  • 3
  • 1
    Warning: Signal handlers can call -directly or indirectly- a very restricted set of functions. Read carefully [signal(7)](http://man7.org/linux/man-pages/man7/signal.7.html) – Basile Starynkevitch May 23 '13 at 14:19
  • 1
    Indeed, you can't do arbitrary "cleanup" work from a signal handler. Instead, tell a thread about the signal (e.g. by setting a `sig_atomic_t` variable or writing to a pipe) and let the thread shut the program down cleanly. – Mike Seymour May 23 '13 at 14:25
  • Thanks for the list! I don't use signals often, mostly for my own amusement, but I never looked into what I am allowed to do. – cybrhuman May 23 '13 at 14:26