Write a C program that doesn’t terminate when Ctrl+C is pressed. It prints a message “Cannot be terminated using Ctrl+c” and continues execution.
We can use signal handling in C for this. When Ctrl+C is pressed, SIGINT signal is generated, we can catch this signal and run our defined signal handler. C standard defines following 6 signals in signal.h header file.
SIGABRT – abnormal termination.
SIGFPE – floating point exception.
SIGILL – invalid instruction.
SIGINT – interactive attention request sent to the program.
SIGSEGV – invalid memory access.
SIGTERM – termination request sent to the program.
Additional signals are specified Unix and Unix-like operating systems (such as Linux) defines more than 15 additional signals. See http://en.wikipedia.org/wiki/
The standard C library function signal() can be used to set up a handler for any of the above signals.
Ouput: When Ctrl+C was pressed two times
Cannot be terminated using Ctrl+C Cannot be terminated using Ctrl+C
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Signals in C language
- Write your own memcpy() and memmove()
- How to write a running C code without main()?
- Convert a floating point number to string in C
- Print a long int in C using putchar() only
- Implement your own itoa()
- How to change the output of printf() in main() ?
- How to find length of a string without string.h and loop in C?
- Comparator function of qsort() in C
- Name Mangling and extern "C" in C++
- How to measure time taken by a function in C?
- Can we call an undeclared function in C++?
- How does "void *" differ in C and C++?
- Implement Your Own sizeof
- Write a C program to print "Geeks for Geeks" without using a semicolon