Signal programming down on Linux

xiaoxiao2021-03-06  17

1 concept

1.1 production conditions

- When the user presses some terminal keys (such as Ctrl-C)

- Hardware abnormal generation signal

- Send a signal to another process or process group with a kill (2) function

- The user sends a signal to the process with the Kill (1) command

- Some software conditions (such as SIGPIPE and other signals)

1.2 action

- Ignore this signal

- capture signal

- Execute system default action

1.3 Signal classification

- Unreliable signal, usually a normal signal. If a process receives a normal signal, the same signal is present in the pending signal set, and the newly transmitted signal is lost. The universal signal concentration of the same time process may only have a normal signal. All normal signals are defined in file /usr/include/bits/signum.h.

- Reliable signal, that is, real-time signal. The signal value is between Sigrtmin (32) and Sigrtmax (64). Real-time signals are added to the universal signals each time.

2 Signal function

2.1 function original

TYPEDEF VOID (* SIGHANDLER_T) (INT);

SIGHANDLER_T SIGNAL (Int Signum, Sighandler_t Handler);

2.2 Description

The value of Handler can be SIG_IGN (ignition), SIG_DFL (default) or a user-defined signal processing function. Returns the previous signal processing function pointer or SIG_ERR.

3 interrupt system call

If the process performs a low speed system call and the block is captured, the system call will be broken back, and Errno is set to Eintr. 4.2BSD introduces automatic restart system calls include: Read, Readv, Write, Writev, IOCTL, WAIT, and WAITPID.

4 can be re-entered

The rearrangement function is generally referred to: a) use a static data structure, b) call Malloc or Free or C) standard I / O function. If the signal processing function is invoked, it may cause an error.

Even the load function, because Errno is only one, or it may cause an error. Therefore, it is generally called the system call to use Errno to save Errno, and then recover after calling.

5 send a waiting signal

5.1 related functions

INT KILL (PID_T PID, INT SIG);

int Raise; INT SIG

Unsigned int alarm (unsigned int seconds);

INT PAUSE (VOID);

5.2 Description

KILL can send a signal to a process, and Raise sends a signal to the current process.

ALARM sets an alarm value, and the time has been set, producing a SigaLarm signal, which terminates the process when the default action.

Pause is the current process hangs until a signal is received.

Example:

- Incomplete implementation of Sleep: Signals / Sleep1.c

6 signal set

6.1 related functions

INT SiGemptySet (SIGSET_T * SET);

INT SigfillSet (SIGSET_T * SET);

INT SigaddSet (SIGSET_T * SET, INT SIGNUM);

INT Sigdelset (SIGSET_T * SET, INT SIGNUM);

INT Sigismember (const squares); intidum);

6.2 Description

SigSet is a data type representing a plurality of signals. The SiGemptySet function allows the Signal Set not contain any signal, and the SIGFILLSET enables the SET contains all signals. SigaddSet and SigdelSet add and delete a signal. SIGISMEMBER detects whether the signal set contains a specific signal.

7 POSIX signal processing function

7.1 related functions

INT SigAction (int Signum, Const Struct SigAction * ACT, STRUCT SIGACTION * OLDACT); Int SigProcmask (int how, const sigset_t * set, sigset_t * Oldset);

INT SIGPENDING (SIGSET_T * SET);

Int Sigsuspend (const sigset_t * mask);

7.2 Description

Set the process signal shielding word with SigProcmask, three options: SIG_BLOCK, SIG_UNBLOCK and SIG_SETMASK.

SIGPENDING Returns to the calling process is blocked and the currently unresolved signal set.

SigAction replaces the early Signal function and is used to set the processing action associated with the signal. The type of parameter ACT is:

Struct sigaction {

Void (* sa_handler) (int);

Void (* sa_sigction) (int, siginfo_t *, void *);

Sigset_t sa_mask;

INT SA_FLAGS;

}

Because you may be implemented with UNION, you can only give one assignment in the two signal processing function pointer SA_HANDLER and SA_SIGACTION. SA_MASK is a signal that should be masked when the signal processing function is executed. Flag is some identifier. To use SA_SIGAction, sa_siginfo must be set so that some information of this signal and the transmitted variable are set to the second parameter of SA_SIGAction. See the MAN documentation on the use of the second parameters of SA_SIGACTION.

Sigsuspend is suspended by parameter MASK to hang up until a signal is received. Equipped with atomic operations:

SigProcmask (SIG_SETMASK, & MASK, & OLDMASK);

Pause ();

SigProcmask (SIG_SETMASK, & OLDMASK, NULL);

Example:

- Father process synchronous implementation: lib.rhlin /tellwait.c

- Protect the critical area with Sigsuspend: Signals / Suspend1.c

- Wait a global variable with Sigsuspend is set: signals / suspend2.c

- ABORT implementation: Signals / Abort.c

- SYSTEM POSIX.2 Implementation: Signals / System.c

- Handling SIGTSTP Sign (Ctrl-Z): Signals / Sigtstp.c

8 non-local jump

8.1 related functions

Int SigsetJMP (Sigjmp_BUF ENV, INT SAVESIGS);

Void Siglongjmp (Sigjmp_buf Env, Int Val);

8.2 Description

The only difference between these two functions and setjmp, longjmp is that SigSetJMP adds a parameter. Savesigs is non-0, then SigsetJMP saves the current mask word of the process in the ENV.

Example:

- Signal shield and Siglongjmp use: signals / mask.c

转载请注明原文地址:https://www.9cbs.com/read-41692.html

New Post(0)