The OS will send a signal, which is a software-generated interrupt, to a process whenever the user presses the “Ctrl-C” key or when another process needs to communicate with the process. A process can only receive a certain set of signals. Integers serve as the identifiers for signals. The symbolic names are assigned to signal numbers. A signal issued to the parent process when a child’s process terminates is SIGCHLD, for instance.
A signal is transmitted to a process, which sets the relevant bit in the process’s pending signals integer. The OS examines the pending and blocked integers each time a process is chosen to run on a particular processor. The process is resumed properly and resumes the execution at the next instruction if there are no pending signals. When one or more signals are stopped but one or more are still pending, the procedure is also continued normally with the signals still being noted as pending. If one or more signals are not blocked but are pending, the OS calls the signal-handling functions in the process’ code.
Syntax of the Signal Function in C Language
The C library’s signal function creates a signal handler or function with the signal number sig. The signal() function’s declaration is provided in the following:
Let’s discuss each of the signal function parameters in detail. The “sig” is the signal number that has a handling function associated with it. Here are some crucial standard signal numbers:
The SIDABRT is termed as signal abnormal termination like that which is started by the function. The SIGFPE is referred to as a signal floating-point exception which is an improper mathematical operation such as a division by zero or an overflowing operation (not always using a floating-point operation.)
The SIGILL is the signal illegal and is an invalid instruction or an invalid function image. Usually, an effort to execute a data or code corruption is to blame for this. The SIGINT is termed as a signal interrupt which is an interactional attention cue. The user of the application is usually the one who generates it. The SIGSEVG is the abbreviation of the signal segmentation violation and its unauthorized access to data that happens when the software attempts to read or write the data that is not in the memory that has been allotted to it.
The SIGTERM is the acronym for the signal termination where the program received a request for termination.
A function pointer is represented by the “func” parameter. One of the following preset functions may be used here or a function created by the programmer. The SIG_DFL is the signal handling by default. The signal is dealt with by the default response for that specific signal. The SIG_IGN is the signal ignored. The signal is disregarded.
Example 1:
There are various standard signal handler routines available. Each signal has a corresponding default handler routine. The common actions performed by the various default handler procedures include the ignoring of the signal which means to do nothing and simply go back. The process should be ended. Continue by releasing a blocked process. Stop the procedure by blocking it. This instance illustrates the default signal handler in the C language:
#include <pthread.h>
#include <signal.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
void handle_sigint(int sig)
{
printf("Signal handler");
}
int main()
{
signal(SIGINT, handle_sigint);
while (1)
{
printf("Hello to C language\n");
sleep(1);
}
return 0;
}
Along with the other required header files, we imported the “signal.h” header file. The “signal.h” header file contains a macro constant definition for the signals. The handle_sigint function is constructed which takes the integer object “sig”. We called this function inside our main function. The main function utilized the signal function call and takes the two arguments: the first argument is the SIGINT for the signal interruption and the second argument is the handle_signint for signal handler purpose. The while loop is defined which iterates over the printf statement that displays the “Hello to C language” string infinite times until the user presses the ctrl C command.
The string is printed infinite times in the output. Upon receiving the SIGINT signal and using its default handler to end the process, the user may press Ctrl+C to end the process.
Example 2:
Except for SIGKILL, a process can use its handler code to substitute the default signal handler for practically all signals. Any name is acceptable for a signal handler function, but it must have one int parameter and a return type of void. The following example code is the illustration of a user-defined signal handler:
#include <signal.h>
void handle_sigint(int sig)
{
printf("signals catched %d\n", sig);
}
int main()
{
signal(SIGINT, handle_sigint);
while (1) ;
return 0;
}
After including the header file, just begin with the code. We defined the function of a signal handler. The int sig parameter is passed to the signal handler function. This function is the handler for the SIGINT and prints the statement of the signal caught when the user inputs the “Ctrl+C” command. Inside the main function, we have a signal system call that takes the SIGINT argument and the handle_sigint argument.
The program will go in the while loop and prints the following output upon every “Ctrl+C” command:
Conclusion
This whole guide educates you about the signal function in C. We have gone over an introduction to signals in C, along with a structure of signals and their corresponding examples. When it comes to addressing the unplanned disruptions that happen throughout a program’s execution during run time, the C language’s signal command is crucial. As it serves as a signal for the user and can detect errors during the program execution, the signal function can handle these with ease.