First, the basic concept
1. Interruption
Aborts, pauses the currently executing process, and performs other tasks instead.
Hard interrupts: interrupts from hardware devices
Soft interrupts: interrupts from other programs
2. Signal
A signal is a soft interrupt that can be seen as a way for processes to communicate with processes, cores, and processes, providing technical support for asynchronous execution of processes.
3. Some common signals
SIGINT (2) Terminal Interrupt signal Ctrl + C
sigquit (3) terminal exit signal ctrl+/
 SIGABRT (6) Call the Abort function to generate the signal
 SIGFPE (8) arithmetic signal
sigkill (9) death signal
SIGSEGV (11) Segment error signal
 SIGALRM (14) Alarm clock signal
 SIGCHLD (17) Child process end signal
sigcont (18) process continuation signal
sigstop (19) Process pause signal
 SIGTSTP (20) terminal stop signal
4, unreliable signal (not real-time)
1, the signal is less than Sigrgmi (34) is unreliable, these signals are based on the early signal mechanism, an event occurs may produce multiple signals.
2, unreliable signal does not support the exclusion, in the reception of the signal may be lost, if one sent to a process multiple times, it may only receive one time, the other may be lost.
3, process in processing this signal, even if the signal processing function set, when the signal processing function is completed, will again revert to the default signal processing mode.
5. Reliable signal (real-time)
1, is located in [Sigrgmi, Sigrtmax (64)] interval is a reliable signal.
2, reliable signal support to eliminate, will not be lost.
3, whether reliable signal or unreliable signal are through: kill, Signal, Sigqueue, sigaction processing.
Second, the signal capture and processing
#include <signal.h>
typedef void (*sighandler_t) (int);
sighandler_t signal (int signum, sighan‐
dler_t handler);
Function: To register a signal processing function
Signum: The number of the signal, you can write the number directly, you can also use the system-provided macros.
Handler: function pointer
Sig_ign Ignore Signal
SIG_DFL Restore signal Default processing mode
Return value: Is the previous signal processing method
function pointers, sig_ign, SIG_DFL, Sig_err
(1) eg:signal (SIGINT, sig_ing);
Sig_ing represents ignoring SIGINT signals, SIGINT signals are generated by Interruptkey, usually ctrl +c or delete. The process that is sent to all foreground group.
Here we write a dead loop:
#include
#include
int main (int argc, char *argv[])
{
Signal (sigint,sig_ign);
for (;;);
return 0;
}
Then we save the execution.
Press CTRL _c The program does not respond. That's right
If we want to end the program you can press CTRL +\ to end
Actually, when we press CTRL +\, the Sigquit signal is generated.
(2) eg:signal (SIGINT, SIG_DFL);
SIGINT signals are generated by Interruptkey, usually ctrl +c or delete. The process that is sent to all foregroundgroup. The SIG_DFL represents the system default action, which in fact terminates the process for most signals when the system default action is performed. This is the same as not writing this handler function.
We changed the above procedure to:
#include
#include
int main (int argc, char *argv[])
{
Signal (sigint,sig_ign);
Signal (SIGINT,SIG_DFL)
for (;;);
return 0;
}
You can then press CTRL +C to terminate the process. Put signal (SIGINT,SIG_DFL); this sentence is removed, the effect is the same.
(3) void (*signal (int sig, void (* handler) (int))) (int);
Int (*p) ();
This is a function pointer, and p points to a function that has no arguments and returns a value of int.
Int (*fun ()) ();
The difference between this and the above is that the fun () instead of P, and fun () is a function, so it can be considered as fun () after the function is executed, its return value is a function pointer, the function pointer (in fact, the p above) is a function that points to no parameters, And a function that returns a value of int.
void (*signal (int signo, void (*handler))) (int)), which can be seen as a signal () function (which itself is a function with two parameters, an integer type, and a function pointer), and this signal () The return value of a function is also a function pointer, which points to a function with an integer parameter and a return value of void.
The function for signal processing when writing a signal processing function is also void sig_fun (int signo), which is exactly the same as the function pointer that is returned by the signal () function above. void (*signal ()) (int);
Signal is a function that returns a function pointer to a function that takes an integer parameter and does not return a value, looking closely at the 2nd argument of the siganal (int signo, void (*handler) (int)), and the fact that he is returning the S Ignal 2nd signal processing function, point to the signal processing function, you can execute the function (signal internal, signal the signal as a parameter to the handler signal processing function, and then signal function return pointer, and then point to the signal handler function, start to execute it)
Third, the signal processing of the sub-process
1. A child process created by fork inherits the signal processing of the parent process.
2. A child process created through Vfork+exec does not inherit the parent process's signal processing and reverts to the default.
Exercise: Test whether a child process created through vfork+exec inherits the signal processing of the parent process.
Iv. signal sending and kill commands and some operations of the KILL function
The KILL command is the end of the process that follows it, learning the signal that we know that it should be a signal to the process to end the process to complete this function.
How to send a signal to a process. The KILL function will be used here;
Its prototype is as follows;
int Kill (Pid_ pid,int Signum); return value Meaning: Success 0 failed-1
Recipient signal Type
What signal does the kill command send? We know that the signal to end the process is SIGINT.
But kill actually has kill and kill-9 points. Kill does not necessarily kill the process, but kill-9 can kill the process 100%. What is this for?
Is there any other signal that will end the process?
The answer is yes.
The following is a simple program to implement a simple kill command
- #include<stdio.h>
- #include<signal.h>
- #include<unistd.h>
- #include<stdlib.h>
- int main(int argc,char* argv[])
- {
- if (argc==1)
- {
- printf ("error cmd!\n");
- return -1;
- }
- int Pid=atoi (argv[2]);
- int Sig=atoi (argv[1]);
- Kill (Pid,sig);
- }
The Atoi function converts the string argument in the command line to an integer type so that it can be passed to the KILL function!
The simplest operation of this program is to kill a process with the process number!
Iv. Some operations of the Pause/sleep/alarm function
#include <unistd.h>
int pause (void);
Function: Hibernate
1, the process calls the pause function after the process sleep state, until there is a signal to wake it (not ignored signal).
2. When the signal arrives, the signal processing function is executed before the signal processing function ends and the pause is returned.
3. The pause function either does not return (sleeps) or returns-1, and modifies the value of the errno.
4, functionally speaking it is equivalent to no time limit sleep function.
#include <unistd.h>
unsigned int sleep (unsigned int seconds);
Function: Use the calling process sleep seconds seconds
1. The process of calling sleep will not return until the signal is received if it does not have enough seconds to sleep.
2, the return value of sleep is 0, or the remaining number of sleep seconds.
3, the equivalent of a time-limited pause
int Usleep (useconds_t usec); v
Function: Sleep usec microseconds
1 seconds =1000 milliseconds =1000000 microseconds.
It is a more precise function of sleep.
#include <unistd.h>
unsigned int alarm (unsigned int seconds);
Function: Timing an alarm signal
1. Let the kernel send a SIGALRM signal to the process that calls it, after seconds seconds.
2, SIGALRM signal Default processing method is to exit directly.
The following is a simple timing operation to let you feel the few to the function
- #include<stdlib.h>
- #include<signal.h>
- void sigalrm(int signum)
- {
- printf ("I have received%d\n", Signum);
- }
- int main(int argc,char* argv[])
- {
- Signal (SIGALRM,SIGALRM);
- if (argc<1)
- {
- printf ("error cmd!\n");
- return -1;
- }
- int Sec=atoi (argv[1]);
- Alarm (SEC);
- Pause ();
- printf ("timed%d seconds!\n", sec);
- }
This program is simple with alarm and pause to achieve a timing function! Let the program sleep, alarm send a signal after the specified time, pause will be over
Achieve the purpose of the timing!
V. Signal sets and signal shielding
1. Signal set:
A collection of multiple signals, sigset_t
Consists of 128 bits, each bits represents a signal
int Sigemptyset (sigset_t *set);
Function: Clear Signal set
int Sigfillset (sigset_t *set);
Function: Fill signal letter
int Sigaddset (sigset_t *set, int signum);
function: Adding signals to the signal set
int Sigdelset (sigset_t *set, int signum);
Function: Remove signal from signal set
int Sigismember (const sigset_t *set, int
Signum);
Function: Test whether a signal set has a certain signal
Return value: Has return 1, no return 0, failed to return-1
2, shielding signal concentration of the signal
Each process has a signal mask (signal mask), which is a signal set that contains the signal that the process is blocking.
int sigprocmask (int how, const sigset_t
*set, sigset_t *oldset);
Function: Set the signal mask of the process (signal Shield code)
How to: Modify the signal mask
Sig_block: Adding a signal to the signal mask
Sig_unblock: Removing a signal from the signal mask
Sig_setmask: Replacing the old signal mask with a new signal set
Newset: Newly added, deleted, replaced set of signals, can also be empty
Oldset: Get the old signal mask
When Newset is empty, it is backing up the signal mask
When a process does not want to be disturbed when performing some sensitive operations (atomic operations), this needs to be masked to the signal.
The purpose of shielding the signal is not to not receive the signal, but delay to receive, when the processing of the things to do, should be the shielding signal to restore.
The signal that occurs when the signal is screened is recorded once, and the signal is set to the final state, which is sent again once the signal screen is closed.
Unreliable signals are sent only once when the signal is unblocked, regardless of the number of times the signal has been masked.
Signals that occur during signal masking are queued for reliable signals and processed individually after the signal is unblocked.
When executing a handler function, the currently processed signal is masked by default, and then restored after execution is completed.
int sigpending (sigset_t *set);
function: Get signal of final state
- #include <stdio.h>
- #include <unistd.h>
- #include <signal.h>
- Update data
- void updata(void)
- {
- for (int i=0; i<; i++)
- {
- printf ("Update article%d data ... \ n", i);
- Sleep (1);
- }
- }
- void SIGINT(int signum)
- {
- printf ("received signal%d, processing ... \ n", Signum);
- }
- int main()
- {
- Signal (34,sigint);
- printf ("I am process%d\n", getpid ());
- Defining a signal Set
- sigset_t Set,oldset;
- Initializing the signal set
- Sigemptyset (&set);
- Adding signals to the signal set
- printf ("Add%d%s\n to the signal set",34,sigaddset (&set,)?" Failure ":" Success ");
- Set the signal mask
- printf ("Set signal mask%s\n", Sigprocmask (sig_setmask,&set,&oldset)?" Failure ":" Success ");
- Updata ();
- Sigpending (&set);
- for (int signum=1; signum<; signum++)
- {
- if (Sigismember (&oldset,signum))
- {
- printf ("Default shielded Signal%d\n", signum);
- }
- }
- printf ("restore signal mask%s\n", Sigprocmask (Sig_setmask,&oldset,NULL)?" Failure ":" Success ");
- Pause ();
- }
This code is a simple example of shielding signal, here is not too much to repeat!
In summary: A signal is a soft interrupt and is a method of handling asynchronous events. In general, the operating system supports many signals. In particular, UNIX, a more important application typically processes signals.
UNIX defines a number of signals, such as SIGINT to interrupt the character signal, that is, the CTRL + C signal, Sigbus indicates a hardware failure signal, SIGCHLD indicates the child process status change signal, sigkill means to terminate the program running signal, and so on. Semaphore programming is a very important technology under UNIX.
Summary of UNIX processes (ii) some operations on signals