Program without SIGINT signal interruption

Asked

Viewed 66 times

1

I’m trying to write a program that stays on an infinite loop, and can’t be stopped by the SIGINT( C signal on the keyboard).

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

1 answer

2

A very simple implementation of how to capture SIGINT (Ctrl+c) would be 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 its context with want.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.