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.
Output: 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- C program that does not suspend when Ctrl+Z is pressed
- Write a URL in a C++ program
- Write a C program that won't compile in C++
- C program to write an image in PGM format
- Write a program that produces different results in C and C++
- Write a C program that displays contents of a given file like 'more' utility in Linux
- Write a C program to print "GfG" repeatedly without using loop, recursion and any control structure?
- Write a C program to print "Geeks for Geeks" without using a semicolon
- When should we write our own assignment operator in C++?
- Write your own memcpy() and memmove()
- When should we write our own copy constructor?
- How to write your own header file in C?
- How to write a running C code without main()?
- Write a C macro PRINT(x) which prints x
- Read/Write structure to a file in C
- How to write long strings in Multi-lines C/C++?
- Write your own strlen() for a long string padded with '\0's
- Write one line functions for strcat() and strcmp()
- Does C++ compiler create default constructor when we write our own?
- Read/Write Class Objects from/to File in C++
Improved By : nidhi_biet