Linux Signal Capture

Source: Internet
Author: User
Tags sigint signal signal handler

The pause function of the pause function suspends the process that calls it until any message arrives . The calling process must be able to handle the signal being served, otherwise the default deployment of the signal will occur.  int pause (void), pause is returned only if the process captures a signal, and if a signal is sent to signal processing, the processing will be performed before pause returns. Pause always returns -1, and the variable errno is set to Eintr.
Sending signals and capturing signals are mutually reinforcing. each process can decide to respond to all signals except Sigstop and Sigkill. Sigstop and Sigkill cannot be captured or ignored. the capture signal is not really going to capture it, but instead waits for the signal to be sent over. When executing a program, the state of all signals is the system default or ignored. When a process calls fork, its child processes continue the processing of the parent process, so the address of the signal capture function is meaningful in the child process,
Because the child process shares the signal with the parent process, the child process can also receive a signal sent to the parent process.

defining signal handlers in some cases, the default action of a signal is the desired behavior. But on more occasions, you can change the default behavior or execute additional code. If you want to change the default behavior, you must define and install an automatic signal processor.
pointer to a function-- you can point to an integer variable, a string, an array, a struct, or a function with a pointer variable. -- a function is assigned an entry address at compile time, which is called a function pointer. --You can use a pointer variable to point to a function, and then call this function from that pointer variable.
callback Function-- The callback function is a function called through a function pointer. -- If you pass the pointer (address) of the function as an argument to another function, when the pointer is used to invoke the function it points to, we say this is a callback function. --The callback function is not called directly by the implementing party of the function, but is called by the other party when a particular event or condition occurs, and is used to respond to the event or condition. the window procedure under--windows programming is a typical callback function.
signal Function--unix/The most common interface for Linux processing signals is the signal function. Use the KILL command at the command line to send a signal to the specified process. --using the Kill function typedefvoid(*sighandler_t) (int); sighandler_t Signal (intSignum, sighandler_t handler); the signal function is generally used to capture a signal, and its function is to register a callback function for a signal . The first parameter, Signum, is usually the system-supplied signal that is to capture the signal. The second parameter specifies a function pointer of type (typedefvoid(*func) (int) , the function has an int parameter, and int represents the captured signal value. When capturing multiple signals requires the use of multiple signal () function processes to capture signals and process the signals, the sequence of instructions being executed by the process is temporarily interrupted by the signal processing program. It first executes the instructions in the signal handler . If the signal handler returns (no call to exit (0or Abort ()), the normal sequence of instructions that the process is executing when the signal is captured is resumed. In a signal handler, it is not possible to determine where the capture signal is being performed by the process. After compiling, execute the program with the KILL command to send a signal to the program--The kill command actually does not mean ' kill ', but sends the specified signal to the specified process. --kill [-S signal|-p] [--] pid ...--kill can be used-s specifies the specific signal to be sent to the specified process. --kill-s 2 2365 (the value of SIGINT is 2, this command sends the SIGINT signal to the specified process)--The signals are all integers.
//Capturing signals#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<signal.h>voidCatch_signal (intSign ) {    Switch(sign) { Casesigint:printf ("SIGINT signal\n"); Exit (0);  Break;  Casesigalrm:printf ("SIGALRM signal\n");  Break; }}intMainintArgChar*args[])    {signal (sigint,catch_signal);    Signal (sigalrm,catch_signal); intI=0;  while(1) {printf ("Hello God%d\n", i++); Sleep (1); }    return 0;}
. suffixes:.c. OCC=Gccsrcs=hello.cobjs=$ (srcs:.c=. O) EXEC=tecstart:$ (OBJS)     -o $ (EXEC) $ (OBJS)    "^_^----OK----^_^". C.O:    - WALL-G-o [email protected]-C $< clean:    -F $ (    Objs)F $ (EXEC)

Linux Signal Capture

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.