Program without interruption of signal SIGINT

1

I'm trying to write a program that stays in an infinite loop, and can not be interrupted by the SIGINT (^ C keypad) signal.

What I have so far is the following code:

void sigint();

int main()
{
    int pid;
    pid = fork();

    if(pid == -1)
    {
        perror("fork");
        exit(1);
    }
    if(pid == 0)
    {
        signal(SIGINT, SIG_IGN);
    }
    else
    {
        sleep(3);
        printf("\nPARENT: sending SIGINT\n\n");
        kill(pid,SIGINT);
    }
    void sigint()
    { while(1);}
    
asked by anonymous 21.03.2018 / 12:57

1 answer

2

A very simple implementation of how to capture SIGINT (ctrl + c) would look like this:

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

void sigHandler(int sig)
{
    printf("SIGINT!\n");
}

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

    while(1)
        usleep(100000);

    return 0;
}

Each time you press ctrl + c, you will call the function sigHandler . From this small example you can apply in context with whatever you want.

    
21.03.2018 / 16:44