×

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.

Related Topics

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.

How to use Typedef Struct in C

The “typedef” is a predefined keyword in C programming language which is used to declare the new name to an existing type of variable. For better understanding, if you declare a...

3 minutes read.

Pointer arithmetic in C

In the C programming language, a pointer is an address which stores a numeric value. Hence, a developer can perform several arithmetic operations on the same just as one does...

4 minutes read.

Reverse a Stack using Recursion in C

In this tutorial we will learn how recursion will be used in this case to reverse a stack. For loops, while loops, do-while loops, and similar constructions are not permitted....

5 minutes read.

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.

Long int in C

We use data type to avoid the type of data to be stored for a variable at the time of declaration.So, we can say that the variable type is known...

3 minutes read.

How to convert a string to hexadecimal in C

Converting a character array or any string to its respective hexadecimal form is simple. The only thing we have to do is to follow the below steps. Take each character from...

3 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

If else Programs in C Programming

If else Programs in C programming The if-else statement in C is based on some particular conditions to perform the operations. If and only if the given condition is valid, the operations listed in...

4 minutes read.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

Use of Structure in C

We can usually use arrays in C programming to store elements of the same data type. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

Dynamic memory allocation

Dynamic memory allocation is used to allocate the memory at runtime. It has following function within <stdlib.h> header file. Function Syntax malloc() malloc (number *sizeof(int)); calloc() calloc (number, sizeof(int)); realloc() realloc (pointer_name, number * sizeof(int)); free() free (pointer_name); malloc() function The process...

2 minutes read.

How to Avoid Structure Padding in C

Generally, when an object of some structure type is declared, some contiguous memory will be allocated in block form, which will be allocated to structure members. To understand structure padding...

3 minutes read.

Do WHILE LOOP in C Programming Examples

Before understanding the programming examples of the Do-While loop, we have to know what is Do-While loop in C. So, let's start with the definition of the Do-While loop. What is...

5 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 minutes read.

Deadlock Detection Program in C

What is Deadlock? A deadlock is a circumstance where a group in the process is halted because they are each holding onto resources while waiting for other methods to obtain them. Think...

5 minutes read.

Structure in C

Why Structure came into the picture? In the actual world, we deal with entities that are collections of objects rather than tiny bits of data like integers, characters, floats, etc. So,...

4 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.