Linux signal programming practices (I) and linux signal programming practices

Source: Internet
Author: User
Tags sigint signal

Linux signal programming practices (I) and linux signal programming practices
Interrupted

Interruption is the system's response to asynchronous events. During code execution, the process can be interrupted at any time and then run the exception handling program;

Computer System interruption scenario: interrupt source sends interrupt signal-> CPU determines whether the interrupt is blocked and protects the site-> CPU (query the interrupt vector table and find the entry address of the interrupt service program) execute the Interrupt Processing Program-> (after the interrupt is handled)-> restore the site and continue to execute the original task


Interrupt category

Hardware interruption (external interruption)

An external interrupt is an interruption that occurs when an external device sends a hardware request. It is also called a hardware interrupt.

Software interruption (internal interruption)

An internal interruption is an interruption caused by a CPU running program error or an internal program call. It is also called a software interruption (for example, a division of 0 operations is performed, and the user space falls into the kernel space ).

 

Signal

A signal is an event generated when the UNIX/Linux system responds to certain conditions. When a process receives a signal, it takes corresponding action. Signals are generally generated due to certain error conditions, such as memory segment conflicts, floating point processor errors, or invalid commands. signals are a simulation of interruptions at the software level, so it is usually called Soft Interrupt;

 

Signal and interrupt similarity:

(1) adopts the same asynchronous communication mode;

(2) When a signal or interrupt request is detected, all processes being executed are suspended and transferred to execute the corresponding processing procedures;

(3) All returned to the original breakpoint after processing;

(4) shielding of signals or interruptions.

 

Differences between signals and interruptions:

(1) The interruption has a priority, but the signal has no priority. All signals are equal;

(2) The signal processing program runs in the user State, while the interrupt processing program runs in the core State;

(3) The interrupt response is timely, and the signal response usually has a large time delay.

Common Linux signals and their key values:

01 SIGHUP pending (hangup)

02 SIGINT interrupted. When you press ^ c or ^ break from the keyboard

03 SIGQUIT exits. When the user presses the quit key from the keyboard

04 invalid SIGILL command

05 SIGTRAP trace trap: start the process and track Code Execution

06 sigiot iot instructions

07 SIGEMT command

08 SIGFPE floating point operation Overflow

09 SIGKILL killing and killing Processes

10 SIGBUS Bus Error

11 SIGSEGV segment violation (segmentation violation), the process attempts to access a location other than its virtual address space

12. The parameter in the SIGSYS system call is incorrect. If the system call number is invalid

13 SIGPIPE writes data to a non-read Pipe

14 SIGALRM alarm. This signal is sent when a process wants to receive signals after a certain time

15 software termination)

16 SIGUSR1 User-Defined signal 1

17 SIGUSR2 User-Defined Signal 2

18 SIGCLD a sub-process is dead

19 SIGPWR Power Failure

For more details, see http://www.chengxuyuans.com/Linux/65677.html

Processes can respond to signals in the following situations:

(1) Ignore the signal

Do not take any operation. Two signals cannot be ignored: SIGKILL and SIGSTOP.

[Why cannot a process ignore the SIGKILL/SIGSTOP signal. (If the application can ignore these two signals, system management cannot kill or pause the process, or manage the system.)]

(2) capture and process signals

The Code being executed by the kernel interrupt is transferred to the previously registered handler.

(3) perform the default operation

The default operation is usually to terminate the process, depending on the signal sent.

Default signal operation: View by man 7 signal


typedef void (*__sighandler_t) (int);#define SIG_ERR ((__sighandler_t) -1)#define SIG_DFL ((__sighandler_t) 0)#define SIG_IGN ((__sighandler_t) 1)__sighandler_t signal(int signum, __sighandler_t handler);

Parameters

Signal is a function with signum and handler parameters. signals to be captured or blocked are provided by the signum parameter, the function to be called when receiving a specified signal is given by handler. This function must have an int type parameter (that is, the received signal code). Its type is void, handler can also be the following two special values:

SIG_IGNShield this signal

SIG_DFLRestore Default Behavior

And the return is the last signal processing function. The first return is the default processing function.

void handler(int sig){        printf("recv a sig=%d\n",sig);}int main(){        sig_t oldhandler;        oldhandler=signal(SIGINT,handler);        if(oldhandler==SIG_ERR)                ERR_EXIT("signal error!");        while(getchar()!='\n');        if(signal(SIGINT,oldhandler)==SIG_ERR)// fanhuishangyicide signal                ERR_EXIT("signal error!");        while(1);        return 0;}

Let's analyze the results of the above program: We first register a SIGINT signal from Shuyong signal, send a signal every time we use ctrl + c, and execute the handler function, note that handler must have an int parameter. Press enter to jump out of the while loop. When ctrl + c is input again, the handler is oldhandler (default), so it will exit.





Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.