Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
List of Signals in C++
3.
The signal() function 
4.
The raise() function 
5.
Frequently asked questions
6.
Key takeaways 
Last Updated: Mar 27, 2024

Introduction of Signal Handling

Author Shivam Verma
1 upvote

Introduction 

Signals are the interrupts that are delivered to a process by an OS to stop its ongoing task and attend to the task for which the interrupt has been sent. These interrupts can discontinue service in any program of an OS. We can generate interrupts by pressing Ctrl+C on a LINUX, UNIX, Mac OS X, or Windows system.

There are some signals that the program cannot catch, but there is also a list of signals that a programmer can catch in his program and take appropriate actions based on the signal. These signals are defined in the <csignal> header file. Also see, Literals in C.

Fibonacci Series in C++

List of Signals in C++

Here is a list of signals along with their description and working capability:

Signals

Operation description

SIGTERM This signal sends a request to the program for termination from the normal execution.
SIGINT This signal produces a receipt for an active signal.
SIGBUS This is a BUS error that indicates access to an invalid address.
SIGABRT This signal is used for the abnormal termination of the program.
SIGILL This signal is used for detecting illegal instruction.
SIGALRM This signal is mostly used by the alarm() function to indicate the expiration of the timer.
SIGSEGV This signal indicates invalid access to storage.
SIGSTOP A process gets stopped, ignored, blocked, and handled by this signal.
SIGCONT This signal is sent to the process to make it continue.
SIGQUIT This signal is used to terminate a process and generate a core dump.
SIGFPE It recognizes any mathematically incorrect or overflow operations.
SIGTRAP It is used to trace all traps.
SIGUSR1, SIGUSR2 User-Defined Signals.

 

The signal() function 

The signal library provides this signal() function and is used to trap unexpected interrupts or events. 

Syntax:

signal(registered signal, signal handler)


In the above syntax, The first argument registered signal is an integer, representing the signal number, and the second argument signal handler is the pointer to a signal handling function. We must remember that the signal we would like to catch must be registered using a signal function and associated with a signal handling function. The signal handling function must be the void type.

Example

#include <iostream>
#include <csignal>
using namespace std;
void Signal_Handler(int sig_no)
{
    cout<<"The interrupt signal is ("<<sig_no<<")."<<endl;
    exit(sig_no);
}
int main()
{
    signal(SIGABRT, Signal_Handler);
    while(true)
    cout << "Hello Coding Ninjas..." << endl;
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output

Hello Coding Ninjas...
Hello Coding Ninjas...
Hello Coding Ninjas...
Hello Coding Ninjas...
Hello Co

 

Try and compile with online c++ compiler.

The above example illustrates the interruption getting created using signal handler and with SIGABRT method once it reaches in the infinite loop gets aborted after exiting.

The raise() function 

We can generate signals by the raise() function. The raise() function takes an integer signal number as an argument.

Syntax:

raise (signal);


Example1 

#include <iostream>
#include <csignal>
using namespace std;
void Signal_Handler(int sig_no)
{
    cout<<"The interrupt signal is ("<<sig_no<<")."<<endl;
    exit(sig_no);
}
int main()
{
    int c=0;
    signal(SIGILL, Signal_Handler);
    while(++c)
    {
        cout<<"Hello Coding Ninjas"<<endl;
        if(c==4)
        raise(SIGILL);
    }
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output

Hello Coding Ninjas
Hello Coding Ninjas
Hello Coding Ninjas
Hello Coding Ninjas
The interrupt signal is (4).


The above example illustrates the SIGILL signal within the signal function().

Example2 

#include <iostream>
#include <csignal>
using namespace std;
void Signal_Handler(int sig_no)
{
    cout<<"The interrupt signal is ("<<sig_no<<")."<<endl;
    exit(sig_no);
}
int main()
{
    int c=0;
    signal(SIGALRM, Signal_Handler);
    while(++c)
    {
        cout<<"Hello Coding Ninjas"<<endl;
        if(c==4)
        raise(SIGALRM);
    }
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output

Hello Coding Ninjas
Hello Coding Ninjas
Hello Coding Ninjas
Hello Coding Ninjas
The interrupt signal is (14).

 

The above example illustrates the SIGALRM signal within the signal function().

Frequently asked questions

  1. What is the syntax of signal() function in C++?
    Ans: void signal(int sig_no, signal signal_handler) is the syntax of signal() function in C++.
     
  2. Which signal is used to generate invalid access to the storage?
    Ans: SIGSEGV signal indicates invalid access to storage.
     
  3. Which header file is used to handle signals in C++?
    Ans: The header file which is used to handle signals in C++ is <csignal>.
     
  4. Which signal is used to send a termination request to the program?
    Ans: SIGTERM signal sends a request to the program for termination from the normal execution.

Key takeaways 

In this blog, we discussed the introduction to signal in C++ and the list of signals. We also discussed signals operations and some signal and raise function examples.

This blog is over, but If you want to take your learnings to the next level, you can use our practice platform Coding Ninjas Studio to practice various DSA questions asked in many interviews. You can also visit and read our DSA blogs by clicking here.

Live masterclass