Home  >  Article  >  Backend Development  >  C program that won't pause when Ctrl+Z is pressed

C program that won't pause when Ctrl+Z is pressed

WBOY
WBOYforward
2023-09-19 18:13:021320browse

C program that wont pause when Ctrl+Z is pressed

In programming, when a program malfunctions and runs in an unexpected way in the terminal compiler, the programmer has the right to explicitly stop the program from running. To explicitly stop a program, the user must know the correct keyboard shortcut to press.

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

  • Ctrl c - Used to stop the execution of a program that takes some time to complete input/output operations and then suspends execution. It sends a SIGINT signal to the process and the process will be terminated. In some languages, this SIGINT can be handled through a signal function similar to that in C language.

  • Ctrl z - Used to stop the execution of the program. All tasks related to the process are closed and execution is suspended. It sends a SINTSTP signal to the process, terminating the execution of the program. Although implemented in the same way, this signal is more powerful than the others. This can also be handled.

Here, we will write a code that can bypass the call of ctrl z. Instead of being paused, the program will print out "ctrl z cannot pause this code ".

As mentioned above, the C programming language can handle calls to ctrl z. When the SINTSTP signal is called to end the program's process, we will redefine the role of this signal so that when used it does not terminate the code and prints a line.

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

Example

Demonstration

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

Output

// an infinite loop

The above is the detailed content of C program that won't pause when Ctrl+Z is pressed. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete