Linux Application Development (9): Signal Communication

Source: Internet
Author: User

I. Common signals:


1. sighup: end signal sent from the terminal


2. SIGINT: the interrupt signal from the keyboard, CTRL + c


3. sigkill: the signal will end the process of receiving the signal.


4. sigterm: signal sent by the kill command


5. sigchld: indicates the signal indicating the sub-process to stop or stop


6. sigstop: indicates the stop signal from the CTRL + z keyboard or from the debugging program.

Ii. signal sending:

1. Main functions for sending signals include kill and raise.

1.1. Differences:

(1) Kill can send signals to itself or to other processes.

(2) The raise function sends signals to the process itself.

1.2 usage:

(1) introduce the header file:

# Include <sys/types. h>, # include <signal. h>

(2) function prototype:

I. Kill function with connected parameters: int kill (pid_t PID, int signo );

A. PID indicates the process to be sent a signal

B. the PID parameters of kill are different in four cases:

1 ~, PID> 0. Send the signal to the process whose ID is PID.

2 ~, PID = 0, send the signal to the same group of processes

3 ~, PID <0, send the signal to the process whose process group ID is equal to the absolute value of PID

4 ~, PID =-1, send the signal to all processes

Ii. There is a parameter raise function: RINT raise (INT signo );

2. The alarm scheduled signal sending function. You can use the alarm function to set an event value. When the set time is reached, the sigalrm signal is generated. If this signal is not captured, the default action is to terminate the process.

(1) introduce the header file # include <unistd. h>

(2) function prototype: Unsigned int alarm (unsigned int seconds );

I. Seconds: specifies that after seconds, the sigalrm signal is generated and sent to the user.

(3) Note:

I. Each process can only have one alarm time. If the alarm time has been set for the process before calling alarm, and it has not timed out yet, the previous alarm time will be replaced by the new value

Ii. In special cases, if there is a previously registered alarm time that has not timed out, and this time sets the alarm time to 0, it indicates canceling the previous alarm time.

3. The pause function suspends the calling process until a signal is captured:

(1) introduce the header file: # include <unistd. h>

(2) int pause (void );

The suspension ends only after a signal processing function is executed.

Iii. Signal Processing,

1. When a signal appears, it will be processed in one of the following three ways:


(1) Ignore this signal. Most signals are processed in this way, but there are two types of signals that cannot be ignored,

I, sigkill II, sigstop

The reason is that they provide super users with a way to terminate or stop the process.

(2) execute the desired action to notify the kernel to call a user function when a signal occurs.

(3) execute the default system action. For most signals, the default processing method is to terminate the process.

2. There are two main signal processing methods:

(1) Use simple Signal Functions

(2) Use a signal set function group

3. register the signal processing function: signal. In general, signal is a function pointer, so it has a return type and a parameter. (when talking about function pointers, we should discuss them from these two perspectives)

(1) header file: # include <signal. h>

(2) prototype: void (*Signal (INT signo, void (* func) (INT ))) (INT)

Understanding:

I. typedef void (* sighandler_t) (INT); we first define a type (sighandler) using typedef, which is actually a function pointer.

Ii. sighandler signal (int signum, sighandler_t handler)

Iii. Possible values of FUNC:

= Sig_ign (ignore): Ignore this signal

= Sig_dfl: handled by default

= Signal processing function name: use this function for processing









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.