Skip to content
Related Articles

Related Articles

Improve Article

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

  • Difficulty Level : Hard
  • 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.




// C program that does not suspend when
// Ctrl+Z is pressed
#include <stdio.h>
#include <signal.h>
  
// Signal Handler for SIGTSTP
void sighandler(int sig_num)
{
    // Reset handler to catch SIGTSTP next time
    signal(SIGTSTP, sighandler);
    printf("Cannot execute Ctrl+Z\n");
}
  
int main()
{
    // Set the SIGTSTP (Ctrl-Z) signal handler
    // to sigHandler
    signal(SIGTSTP, sighandler);
    while(1)
    {
    }
    return 0;
}

Output:

Cannot execute Ctrl+Z

This article is contributed by Pramod Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.
My Personal Notes arrow_drop_up
Recommended Articles
Page :