C program that does not Suspend when Ctrl+Z is Pressed
In Programming when a program breakdown and runs surprisingly in the terminal compiler the developer has the ability to prevent the program from running unequivocally. For expressly halting the program, the client should know the right console alternate necessary route to be squeezed.
To end the execution of a block of code, there are two kinds of console easy routes utilized.
- Ctrl+c:
The CTRL + C is utilized to send a hinder to the ongoing executing task. In this program, we will perceive how to get the CTRL + C occasion utilizing C++.
The CTRL + C is one sign in C or C++. So we can find by signal getting procedure. For this sign, the code is SIGINT (Signal for Intrude). Here the sign is gotten by signal() capability. Then, at that point, one callback address is passed to call capability subsequent to getting the sign.
It is utilized to stop the execution of the program, it requires a digit of investment to finish the I/o tasks and afterward suspends the execution. It conveys a SIGINT message to the interaction which gets ended. In certain dialects, there are ways of dealing with this SIGINT like the sign capability in C.
Example:
//simple program to understand the ctrl+c command
#include <unistd.h> //here, we are including the unistd.h module into our //program
#include <iostream> //here, we are including the input and output stream //module into our program
#include <cstdlib> //here, we are including the cstdlib module into our //program
#include <signal.h> //here, we are including the signal.h module into our //program
using namespace std; //here, we are using the namespace standard in our //program
// here, we are defining the function to be called when ctrl-c (SIGINT) is sent //to process
void signal_callback_handler(int signum) {
cout << "Caught signal " << signum << endl;
// here, we are terminating the program
exit(signum);
}
int main(){
// here, we are registering the signal, and the signal handler
signal(SIGINT, signal_callback_handler);
while(true){
cout << "The Program is in processing..." << endl;
sleep(1);
}
return EXIT_SUCCESS;
}
Output:
$ g++ test.CPP
$ ./a.out
The Program is in processing...
The Program is in processing...
The Program is in processing...
The Program is in processing...
The Program is in processing...
The Program is in processing...
^CCaught signal 2
$
- Ctrl+z
It is utilized to stop the execution of the program, every one of the undertakings connected with the interaction is closed and execution is suspended. It conveys a SINTSTP message to the interaction which ends the program however the execution is the equivalent yet this sign is all the more remarkable when contrasted with others. This can likewise be dealt with.
Here, we can compose a code that will outperform the ctrl+z call. And on second thought of getting suspended the program will print "ctrl+z can't suspend this code".
As examined over, the ctrl+z call can be dealt with in the C programming language. At the point when the SINTSTP signal is conjured to end the course of the program. We will rethink what this sign does so I won't end the code and print a line when utilized.
The sign() strategy is utilized to deal with this sort of thing.
Example:
// simple C program that does not suspend when
// Ctrl+Z is pressed
#include <stdio.h> //here, we are including the standard input and output //module into our program
#include <conio.h> //here, we are including the console input and output //module into our program
#include <signal.h> //here, we are including the signal.h module into our //program
//here, the Signal Handler is used for SIGTSTP
void sighandler(int sig_num)
{
//here, we are resetting the handler to catch SIGTSTP next time
signal(SIGTSTP, sighandler);
printf("Cannot execute the Ctrl+Z command when it is pressed: \n");
}
int main()
{
//here, we are setting the SIGTSTP (Ctrl-Z) signal handler
// to sigHandler
signal(SIGTSTP, sighandler);
while(1) // here, we are declaring the while loop with the true condition
{
}
return 0;
}
Output:
The above program will terminate or produce an infinite loop.