[Linux Study Notes] Signal Processing

Source: Internet
Author: User

Signal function: notifies the process of asynchronous events.

In Linux, different signals can be identified in 29. The process can be displayed and sent to another process by calling the kill or killpg system. When the signal occurs, the kernel interrupts the current process, and the process executes the processing function to respond to the signal. After the signal ends, the normal process is resumed.

 
Signal Value processing reason -------------------------------------------------------------------------- sighup 1 A terminal suspension or Control Process Termination SIGINT 2 a keyboard interruption (for example, the Break Key is pressed) the exit key of the sigquit 3 C keyboard is pressed by the invalid sigill 4 C command SIGABRT 6 C by abort (3) the exit command sigfpe 8 C floating point exception sigkill 9 AEF kill signal SIGSEGV 11 C Invalid Memory Reference sigpipe 13 a pipe rupture: write a pipeline sigalrm 14 A without a read port signal sigterm 15 a termination signal SIGUSR1 30,10, 16 a user-defined signal 1sigusr2 31,12, 17 A User-Defined Signal 2 sigchld 20, 17, 18 B sub-process end signal sigcont 19,18, 25 process continue (previously stopped process) sigstop 17,19, 23 def termination process sigtstp, 24 d control terminal (TTY) press the stop key sigttin, 26 d background processes attempt to read sigttou from the control terminal, 27 d background processes attempt to write from the control terminal
Sigbus, 10 C Bus Error (wrong Memory Access) sigpoll a sys v defined pollable event, synonymous with sigio sigprof 27,27, 29 a profiling timer to sigsys 12 ,-, 12 C invalid System Call (svid) sigtrap 5 C trace/breakpoint capture sigurg 4.2, 21 B socket emergence emergency condition (BSD) sigvtalrm, 26, 28 A actual time alarm clock signal (4.2 BSD) sigxcpu 24, 24, 30 C exceeds the set CPU time limit (4.2 BSD) sigxfsz 25, 25, 31 C exceeds the set file size limit (4.2 BSD)

Related functions:

1. Capture signal: Signal

Used to determine the response of the system signal.

Format:

# Include <signal. h> void (* signal (INT signo, void (* func) (INT); // If func is sig_ign, the kernel ignores this signal.
 
// Func is sig_dfl, which indicates the default action when the signal is received.
 
// Capture the signal when func is the function address

2. sending signals: Kill and raise, alarm, getimer, setimer, pause

Kill: sends signals to other processes

Raise: sends a signal to the current process

Alarm: set a time value (alarm time). When the value is set to exceed the siglrm signal, the process is terminated by default.

Getitimer and setitimer: Get and set the interval timer function

Pause: suspends the process until a signal is captured.

Format:

# Include <sys/types. h> # include <signal. h> int kill (pid_t PID, int sig); int raise (INT sig); // raise is equivalent to kill (getpid (), sig) // # include <unist. h> unsigned int alarm (unsigned int seconds); // # include <sys/time. h> int getimer (INT which, struct itimerval * value); intsetitimer (INT which, const struct itimerval * value, struct itimerval * ovalue); // # include <unistd. h> int pause (void );

Let's talk about

[Signal shielding words ]:

Each process has a blocking word that specifies the signal set to block delivery to the process.

1. Set

Function: sigpromask, sigpending

Sigpromask: detects or modifies the signal shielding characters of a process.

Sigpending: returns the signal set that cannot be delivered and is not determined by the current status when the calling process is blocked.

Format:

 
# Include <signal. h> int sigpromask (INT how, const sigset_t * Set, sigset_t * oset); int sigpending (sigset_t * Set );

2. Implementation

Function: sigaction

Sigaction: checks or modifies the processing action associated with the specified signal.

Format:

# Include <signal. h> int sigaction (INT signo, const struct sigaction * Act, struct sigaction * oact); // The struct prototype is as follows: struct sigaction {void (* sa_handler )(); // ADDR of signal handler, or sig_ign, or sig_dfl sigset_t sa_mask; // additional to block int sa_flag; // signal options };

[Related operations ]:

Functions: sigsetjmp, siglongjmp, sigsuspend, abort, system, and sleep

 Sigsetjmp and siglongjmp: non-local transfer Signal Processing

Sigsuspend: Implements restoration signal shielding in an atomic operation, and then sleep the process.

Abort: MakeProgramExceptional termination

System: execute system commands

Sleep: suspends a process in the call until it expires or receives a signal and returns it from the signal processing program. It can be implemented by the alarm function.

Format:

 
# Include <setjmp. h> int sigsetjmp (sigjmp_buf ENV, int savemask); void siglongjmp (sigjmp_buf ENV, int Val); // # include <signal. h> int sigsuspend (const sigset_t * sigmask); // # include <stdlib. h> void abort (void); INT system (const char * string); // # include <unistd. h> unsigned int sleep (unsigned int second );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.