×

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

Write a program that produces different results in C and C++

In this tutorial, we'll explore several programs that, depending on whether they are developed using C or C++ compilers, will produce varying outputs. There are numerous similar programs, but we will just...

2 minutes read.

Memory leak in C

What is memory leak in C? Memory leak occurs when we keep allocating memory in the heap without freeing it, i.e., the allocated memory in heap is not released back to...

3 minutes read.

GCD program in C

C language : Dennis Ritchie developed the general-purpose computer language C at Bell Laboratories in 1972. Despite being an ancient language, it is extremely popular. It is among the most widely used...

4 minutes read.

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

Positioning of file in C

Positioning() function in c The fseek() function is utilized to change the document position of the stream . The fully extent of value from the source should be one of the...

3 minutes read.

Garbage in C

Garbage in C The C programming language is a perfect fit for embedded systems as it provides low level control, structured programming, and portability. On the contrary, it does not provide...

3 minutes read.

Round Robin Scheduling in C

Round Robin Scheduling in C Round robin is a CPU (Central Processing Unit) scheduling algorithm designed to share the time systems. It is one of the simplest and easiest scheduling algorithms...

4 minutes read.

How to return a Pointer from a Function in C?

In the C programming language, pointers are variables that hold the address memory location of another variable. We could also input pointers to a function and return pointers from a...

3 minutes read.

Bank management system in C

This is a mini project that is constructed completely using the C language. The bank management system is a beginner friendly project. To construct this user only needs to know...

12 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.

Binary Search in C with Best and Worst Time Complexity

What is Binary Search? Binary search is an algorithm that is used to find an element in a sorted array efficiently. It has a time complexity of O(log n). It means...

3 minutes read.

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 minutes read.

How to get ASCII value in C

For text data on computers and the internet, ASCII (American Standard Code for Information Interchange). It is the most widely used character encoding standard. One hundred twenty-eight alphabetic, numeric, special...

3 minutes read.

C Programming Tutorial

What is C Programming C is most popular and widely used computer programming language. It was developed by Dennis Ritchie in 1972 at the Bell Lab. It is used to develop system application software. There...

8 minutes read.

Typedef vs define in C

Typedef VS define in C Typedef In the C programming language, a keyword called typedef can be used to give a type a new name. In other words, it is used to...

5 minutes read.

Simple Programs in C Language

In this article, we will discuss the basic programs in C language. Addition of two values in C. Swapping two values. Finding the odd or even values. Finding vowels and consonants in C. Finding the...

11 minutes read.

Comments in C

Comments are used to comment on the line of code in the program. Comments are a way of inserting remarks and reminders into code without affecting its behavior. The compiler...

1 minute read.

Tower of Hanoi in C

What is Tower of Hanoi? The Tower of Hanoi is a gaming problem that was created in 1883 by a French mathematician named Édouard Lucas. The Tower of Hanoi temple in...

4 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it shows...

4 minutes read.