C library - signal() function



The C library signal() function allows user to handle asynchronous event during the program execution. A asynchronous event refer to software notification which reports the events outside the program. For example − division by zero, keyboard interruption, etc. Here, void(*signal(int sig, void (*func)(int))) (int) sets a function to handle signal i.e. a signal handler with signal number sig.

Syntax

Following is the syntax of the C library signal() function −

void (*signal(int sig, void (*func)(int)))(int)

Parameters

  • sig − The sig define the signal to build a handling function. Following is the list of important standard signal numbers −

Sr.No. Macro & Signal
1

SIGABRT

(Signal Abort) Abnormal termination, such as is initiated by the function.

2

SIGFPE

(Signal Floating-Point Exception) Erroneous arithmetic operation, such as zero divide or an operation resulting in overflow (not necessarily with a floating-point operation).

3

SIGILL

(Signal Illegal Instruction) Invalid function image, such as an illegal instruction. This is generally due to a corruption in the code or to an attempt to execute data.

4

SIGINT

(Signal Interrupt) Interactive attention signal. Generally generated by the application user.

5

SIGSEGV

(Signal Segmentation Violation) Invalid access to storage − When a program tries to read or write outside the memory it is allocated for it.

6

SIGTERM

(Signal Terminate) Termination request sent to program.

  • func − This is a pointer to a function. This can be a function defined by the programmer or one of the following predefined functions −

Sr.No. Function & Description
1

SIG_DFL

Default handling − The signal is handled by the default action for that particular signal.

2

SIG_IGN

Ignore Signal − The signal is ignored.

Return Value

This function returns the previous value of the signal handler, or SIG_ERR on error.

Example 1

Following is the basic C library program to see the demonstration of signal() function.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>

void sighandler(int);

int main () {
   signal(SIGINT, sighandler);

   while(1) {
      printf("Going to sleep for a second...\n");
      sleep(1); 
   }
   return(0);
}

void sighandler(int signum) {
   printf("Caught signal %d, coming out...\n", signum);
   exit(1);
}

Output

On execution of above code, we will produce the following result and program will go in infinite loop. To come out of the program we used CTRL + C keys.

Going to sleep for a second...
Going to sleep for a second...
Going to sleep for a second...
Going to sleep for a second...
Going to sleep for a second...
Caught signal 2, coming out...

Example 2

In this example, we illustrate the SIGINT to ignore the signal.

#include <stdio.h>
#include <signal.h>
#include <unistd.h>

void handle_sigint(int signum) {
    printf("Received Ctrl+C, but ignoring it.\n");
}

int main() {
   // Press Ctrl+C to ignore (SIGINT)
   signal(SIGINT, SIG_IGN); 
   while (1) {
       printf("Press Ctrl+C (ignored)...\n");
       sleep(1);
    }
   return 0;
}

Output

The above code produces the following result and the program will go into an infinite loop. To exit the program, use the CTRL + C keys.

Press Ctrl+C (ignored)...
Press Ctrl+C (ignored)...
Press Ctrl+C (ignored)...
Press Ctrl+C (ignored)...
^CPress Ctrl+C (ignored)...

Example 3

In this example, we use the signal SIGTERM to request program termination.

#include <stdio.h>
#include <signal.h>
#include <unistd.h>

void sig_handler(int signum) {
   printf("Received SIGTERM. Exiting gracefully...\n");
   // Perform cleanup or other necessary actions before exiting
   _exit(0);
}

int main() {
   // Set custom signal handler for SIGTERM
   signal(SIGTERM, sig_handler); 
   while (1) {
       printf("Press Ctrl+C to send SIGTERM...\n");
       sleep(1);
    }
   return 0;
}

Output

After executing the code, we get the following result −

Press Ctrl+C to send SIGTERM...
Press Ctrl+C to send SIGTERM...
Press Ctrl+C to send SIGTERM...
Press Ctrl+C to send SIGTERM...
^C

Advertisements