How to Prevent C program from getting killed with ctrl-c pressed, the program should not terminate. Ctrl+C is used to terminate process. Pressing Ctrl+C generates interrupt signal (SIGINT ) to a process.

To handle a linux signal in c or c++ program a process has to register the signal using signal(..) system call. For example in Below code the signal SIGINT ( interrupt ) is handled.

How to Prevent C program from getting killed with ctrl-c pressed

compile the code

#gcc -o sample sample.c

Run the sample on linux terminal and press Cntl+C ,, observer the output.

$ ./sample

Running .. process

Running .. process

^CGot an Cntr+C , continue..

Running .. process

Pressing Contrl+C would not terminate the process. The above method to handle Linux signal is deprecated. Let us write a simple c program using sigation system call.

The sigaction() system call is used to change the action taken by a process on receipt of a specific signal.

Ref:

https://linux.die.net/man/3/sleep

https://linux.die.net/man/2/sigaction




Related Contents to follow