C program that does not suspend when Ctrl+Z is pressed
Last Updated :
26 Sep, 2017
Write a C program that doesn’t terminate when Ctrl+Z is pressed. It prints a message “Cannot be suspended using Ctrl+Z” and continues execution.
We can use Unix signal for this. When Ctrl+Z is pressed, SIGTSTP signal is generated.
SIGTSTP signal is sent to a process by its controlling terminal to request it to stop (terminal stop). We can catch this signal and run our own defined signal.
The standard C library function signal() can be used to set up a handler for any of the above signals.
#include <stdio.h>
#include <signal.h>
void sighandler( int sig_num)
{
signal (SIGTSTP, sighandler);
printf ( "Cannot execute Ctrl+Z\n" );
}
int main()
{
signal (SIGTSTP, sighandler);
while (1)
{
}
return 0;
}
|
Output:
Cannot execute Ctrl+Z
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...