Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Standard Library –

C Standard Library -

Introduction

is a header file in the C Standard Library used for handling signals.

signal.h The header file defines a variable type sig_atomic_t, two function calls, and some macros to handle the different signals reported during program execution.

A signal is an asynchronous notification mechanism that allows a process to execute a predefined handler function when a specific event occurs.

The following is a simple example program that demonstrates how to use the signal function to catch the SIGINT signal (typically generated by Ctrl+C).

Example

#include

#include

#include

// Global variable indicating whether the program should exit

volatile

sig_atomic_t stop

=

0

;

void

handle_sigint

(

int

sig

)

{

   

printf

(

"Caught signal %d\n"

,

sig

)

;

    stop

=

1

;

// Set the exit flag

}

int

main

(

)

{

   

// Set the handler for the SIGINT signal to the handle_sigint function

    signal

(

SIGINT

,

handle_sigint

)

;

   

while

(

!

stop

)

{

// Check whether the program should exit

       

printf

(

"Running...\n"

)

;

        sleep

(

1

)

;

   

}

   

printf

(

"Exiting...\n"

)

;

   

return

0

;

}

In the above code, when the program is running, if the user presses Ctrl+C, the SIGINT signal is caught and the handle_sigint function is called, printing the signal number.

Compiling and running the above code produces the following output:

Running...

Running...

Running...

^CCaught signal 2

Exiting...

Code analysis:

Library variables

The variable types defined in the header file signal.h are listed below:

No. Variable & Description
1 **sig_atomic_t **
This is int type, used as a variable in signal handlers. It is an integer type of an object that can be accessed as an atomic entity; even in the presence of asynchronous signals, the object can be accessed as an atomic entity.
2 **sigset_t **
A data type used to represent a set of signals.

Library macros

<

In the C Standard Library,SIG_ macros prefixed with are used together with signal the function to define and handle signal behavior. The following are common SIG_ macros and their purposes:

No. Macro & description
1 SIG_DFL
Indicates the default handler for a signal. Use this macro to restore a signal to its default behavior.
2 SIG_ERR
Indicates the return value when setting a signal handler fails.
3 SIG_IGN
Ignores a signal.

Signal types:

Constant Description
SIGABRT modified by abort Signal generated by the abort function, indicating abnormal termination
SIGALRM modified by alarm Signal emitted when a timer set by the alarm function expires
SIGBUS Illegal memory access (for example, accessing an unaligned memory address)
SIGCHLD Child process status change (exit or stop)
SIGCONT Continue a suspended process
SIGFPE Arithmetic error (for example, division by zero, floating-point exception)
SIGHUP Hangup signal (typically used to detect a terminal disconnect)
SIGILL Illegal instruction
SIGINT Interrupt signal (typically generated by Ctrl+C)
SIGKILL Immediate termination signal (cannot be caught or ignored)
SIGPIPE Writing to a pipe with no reading process
SIGQUIT Terminal quit signal (typically generated by Ctrl+\), generating a core dump
SIGSEGV Segmentation fault (illegal memory access)
SIGSTOP Stop the execution of a process (cannot be caught or ignored)
SIGTERM Termination signal
SIGTSTP Suspend the process (typically generated by Ctrl+Z)
SIGTTIN Signal generated when a background process reads from the terminal
SIGTTOU Signal generated when a background process writes to the terminal
SIGUSR1 User-defined signal 1
SIGUSR2 User-defined signal 2
SIGPOLL I/O event (replacement for SIGIO)
SIGPROF Timer expiration signal (generated when a profiling timer set by setitimer the function expires)
SIGSYS Illegal system call
SIGTRAP Breakpoint or trap instruction
SIGURG Socket urgent condition signal
SIGVTALRM Virtual clock timer expiration signal
SIGXCPU CPU time limit exceeded
SIGXFSZ File size limit exceeded

Library functions

The functions defined in the header file signal.h are listed below:

function Description
void (*signal(int sig, void (*func)(int)))(int); Sets a signal handler.
int raise(int sig); Sends a signal to the current process.
int kill(pid_t pid, int sig); Sends a signal to a specified process.
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset); Examines or changes the set of blocked signals.
int sigaction(int sig, const struct sigaction *act, struct sigaction *oldact); Examines or changes a signal handler.
int sigsuspend(const sigset_t *mask); Temporarily replaces the current signal mask and suspends the process until a signal is caught.
int sigpending(sigset_t *set); Examines the set of pending signals.
int sigemptyset(sigset_t *set); Initializes a signal set to the empty set.
int sigfillset(sigset_t *set); Initializes a signal set to the full set of signals.
int sigaddset(sigset_t *set, int signum); Adds a specified signal to a signal set.
int sigdelset(sigset_t *set, int signum); Removes a specified signal from a signal set.
int sigismember(const sigset_t *set, int signum); Checks whether a specified signal is in a signal set.
void abort(void); Generates SIGABRT a signal, causing the process to terminate abnormally.
unsigned int alarm(unsigned int seconds); Sends a signal after the specified number of seconds SIGALRM signal to the calling process.
int pause(void); to suspend the process until a signal is caught.
void psignal(int sig, const char *s); Prints signal description information.
char *strsignal(int sig); Returns a signal description string.
int sigwait(const sigset_t *set, int *sig); Blocks while waiting for a signal and handles it.