sigaction() function in C

In this article, you will learn about the Sigaction() Method in C with their syntax, parameters, and example.

What is the Sigaction() Function?

The sigaction() function in C can change a process's response to a specific signal. It allows the programmer to define a unique signal handler function or change a signal's behaviour. By using the sigaction() function, a process can be made to operate differently after receiving a certain signal. It provides greater flexibility and control than the older signal() function.

Syntax:

It has the following syntax:

Parameters:

  • signum: The number designated by which the action is to be taken.
  • act: A reference to a sigaction structure that details the new action that has to be performed.
  • oldact: It is an optional pointer to the previous action's storage location in a sigaction structure.

Return Value:

This function returns -1 value in the case of failure; otherwise, returns 0 in the case of success. The specific error is indicated by the value of 'errno'.

struct sigaction:

Information on the appropriate course of action for a given signal is contained in this structure.

Typically, it consists of members like:

  • sa_handler: A pointer, if any, to the signal handling program.
  • sa_sigaction: A pointer, if any, to a different signal handling function.
  • SA_Flags: Flags designate extra actions (such as SA_RESTART, which initiates system calls automatically when interrupted).

Signal Handling:

  • Asynchronous notifications are known as signals. These are transmitted to a process to notify it when particular event occurs, such as software bugs or hardware problems.
  • Software interrupts, user-defined events, and hardware exceptions are examples of events.

Handlers of Signals:

  • Signal handlers are activated when a process receives a certain signal.
  • They can perform operations like cleaning up, logging, or changing the program's behaviour in response to the signal.

Changing Signal Actions:

  • Change how the process responds to a signal using sigaction() method.
  • To do this, you may ignore the signal, carry out the default action, or call a signal handler function you created.

Handling Errors:

  • In the case of a failure, the function returns -1 and sets errno to the error code. Common problems include incorrect signal numbers, insufficient permissions, and incorrect pointer arguments.

Thread Safety:

  • Compared to signal(), sigaction() provides better control over signal handling behaviour, making it a more suitable option for managing signals in multithreaded programs.

Example:

Let us take an example to illustrate the Sigaction() method in C.

Output:

Running. Press Ctrl+C, Ctrl+\, or kill to generate signals...

Explanation:

This C program uses the sigaction() method to configure signal handlers for the SIGINT, SIGQUIT, and SIGTERM signals. Each signal handler function prints a message, including the signal number and the obtained signal. The program goes into an endless loop after printing a message to let users know it is operating. Each signal (SIGINT, SIGQUIT, and SIGTERM) may be produced at the terminal executing the programme by hitting Ctrl+C, Ctrl+, or the kill command. Each time a signal is received, the associated signal handler function is called, and a message containing the signal number and the captured signal is printed. Error checking is done for every call to sigaction() to manage installation issues.

In general, sigaction() is a crucial C function that controls how signals are handled in processes; it provides more control and dependability than other signal-handling methods. It is frequently utilised in managing exceptions, asynchronous I/O, and systems programming.