Table of contents
Various functions are inseparable when using signals. This article takes a look at the most commonly used functions when using signals.
- kill: Send the specified signal to the specified process
- raise: Send the specified signal to the calling process
- abort: Terminate the running of the program
- alarm: timer
- sigqueue: Send a specified signal to a specified process and carry information
- signal: Modify the processing method of the specified signal
1.kill
int kill(pid_t pid, int sig)
Function: Send a specified signal to the specified process.
- pid_t pid: the specified process id
- int sig: The signal to send. Note: A signal is actually a macro.
Return value: 0 is returned on success, and -1 is returned on failure.
2.raise
int raise(int sig)
Function: Send a specified signal to the process that calls this function.
- int sig: signal sent
Return value: return 0 on success, non-zero on failure.
3.abort
void abort()
Function: suspend program execution and jump out directly from the calling place. This is actually an anomaly. SIGABRT
4.alarm
unsigned int alarm(unsigned int seconds)
Function: This is a timer. When the program calls this function, it starts timing. When the timing reaches the specified time, the kernel will send a SIGALRM signal (signal function: exit the process) to the process.
- unsigned int seconds: the length of time to be timed
5. sigqueue
int sigqueue(pid_t pid, int sig, const union sigval value)
Function: Send a specified signal to the specified process and carry information.
- pid_t pid: the specified process id
- int sig: the signal to send
- const union sigval value: The information to be carried, usually does not carry information, so it is usually set to NULL
Return value: return 0 on success, non-zero on failure.
6.signal
typedef void (*sighandler_t)(int)
This is to declare a function pointer type whose return value is void and whose parameter is int .
sighandler_t signal(int signum, sighandler_t handler)
Function: Modify the processing method of the specified signal
- int signum: pass in the specified signal
- sighandler_t handler: The new processing method of this signal. You can pass in a custom signal processing function or SIG_DFL (default processing method) or SIG_IGN (ignore processing method)
Return value: the original signal processing function of the signal
7. Examples
Here to show how to modify the processing function of the specified signal.
(1) Define a custom processing function:
- void sigcb(int no){}
Pay attention to the form of the custom processing function. The form of the function must be the same as the default processing function of the system. The return value is void and the parameter is int.
The int no parameter is actually the signal corresponding to this processing function. When calling this processing function, the signal will be passed in as a parameter.
(2) Modify the processing function of signal No. 3 to the sigcb function:
- signal(3,sigcb)
The return value of the signal function is the original processing function of signal No. 3.