-2

I have really really simple program in C, let me cope paste it from internet there:

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
void sigint_handler(int dummy)
{
    printf("SIGINT HANDLED!\n");
    signal(SIGINT,sigint_handler);
}

int main(){
    signal(SIGINT,sigint_handler);

    kill(getpid(),SIGINT);
    sleep(1);

    return 0;
}

My question is, what does this line do ?

signal(SIGINT,sigint_handler);

Is it even necessary? Without this line this program works just fine and nothing changes.

Cœur
  • 37,241
  • 25
  • 195
  • 267
ogarogar
  • 323
  • 2
  • 14

1 Answers1

0

signal(SIGINT,sigint_handler); tells the computer to call sigint_handler the next time your program receives SIGINT.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • Sadly I think u misunderstood me. I was thinking about line in signal handler. I know that this command is necessary in function int main, but what about this command in sigint_handler itself? – ogarogar Nov 25 '16 at 08:55
  • @ogarogar It does exactly what I said. It tells the computer to call `sigint_handler` the *next* time your program receives SIGINT. – user253751 Nov 26 '16 at 01:08