C program that does not suspend when Ctrl+Z is pressed

In Programing when a program malfunction and runs unusually in the terminal compiler the programmer has the power to explicitly stop the program from running. For explicitly stopping the program, the user must know the right keyboard shortcut that is needed to be pressed.

To terminate the execution of a block of code, there are two types of keyboard shortcuts used.

  • Ctrl+c − It is used to stop the execution of the program, it take a bit of time to complete the i/o operations and then suspends the execution. It sends a SIGINT signal to the process which gets terminated. In some languages, there are ways to handle this SIGINT like the signal function in C.

  • Ctrl+z − It is used to stop the execution of the program, all the tasks related to the process are shut and execution is suspended. It sends a SINTSTP signal to the process which terminates the program though the implementation is the same but this signal is more powerful as compared to others. This can also be handled.

Here, we will write a code that will be able to surpass the ctrl+z call. And instead of getting suspended the program will print “ctrl+z cannot suspend this code”.

As discussed above, the ctrl+z call can be handled in the C programming language. When the SINTSTP signal is invoked to end the process of the program. We will redefine what this signal does so that I will not terminate the code and print a line when used.

The signal() method is used to handle this type of thing.


 Live Demo

#include <stdio.h>
#include <signal.h>
void signalhandler(int sig_num){
   signal(SIGTSTP, signalhandler);
   printf("Cannot execute Ctrl+Z
"); } int main(){    int a = 1;    signal(SIGTSTP, signalhandler);    while(a){    }    return 0; }


// an infinite loop