Linux inter process communication (four) signal, signal communication capture ki

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Signal overview

● The signal is a simulation of the interrupt mechanism at the software level. In principle, a process to receive a signal processor receives an interrupt request can be said to be the same.

● The signal is asynchronous, a process need not be reached by any operation to wait for the signal process, in fact do not know what the arrival time of signal.

● Signals can directly interact between user space processes and the kernel process, the kernel process can also occur which system events, use it to notify the user space process. It can send a process at any time, without the need to know the status of the process. If the signal is not currently in the implementation of state (Running), the signal by the kernel stored, until the process resumes execution is then transmitted to it. If a signal is set as the blocking process, the transmission of signal is delayed, until the obstruction was cancelled when it is passed to the process.

● The signal is the inter process communication mechanism.OnlyAsynchronous communication mechanism, can be regarded as a process of asynchronous notification, notification received signal which event occurred. Signal mechanism in addition to the basic notice, can also pass additional information.

Signal source

Source signal events two:

Hardware source. As we press the buttons on the keyboard or other hardware fault,

The source of software. The system function is most commonly used to send signals with kill (), raise (), alarm (), setitimer () and sigqueue (software), the source also includes some illegal operations.

Process response signal mode

① ignore signal. Ignore the signal on signal without treatment, among them, there are two signal can not be ignored: SIGKILL and SIGSTOP.

The signal capture. Definition signal processing function, when the signal occurs, execution function response.

Execute the default action. The Linux of each signal provided by default, as shown in the following table.:


The life cycle of the signal

A complete signal life cycle can be divided into 3 stages, the 3 stage consists of 4 important events depicted; signal generation, signal registration, in the process, the signal cancellation in the process of signal processing function. The signal generation, registration, cancellation is refers to the signal the internal implementation mechanism, but not the function realization of the signal (not under our control). Therefore the signal or not registered, and later sent the signal function (such as kill) (etc.) and signal installation function (such as signal) and independent (), is only related to the signal value.

Two adjacent event time interval to form a phase signal of the life cycle, as Figure 1 note here signal processing in a variety of ways, is usually completed by the kernel, but also by the user process to complete.

Signal processing includes sending, capture and processing of the signal, they have a common function corresponding:

● Signaling function: Kill(), raise().

● Function signal capture: alarm(), pause().

● Function signal processing: signal(), sigaction().

This section is mainly about sending and capture the signal, the next section more processing

Signal transmitting function kill () and raise()

Function description

kill()The function with kill system's command (but not for kill (kill) is the HA), can send a signal to the process or process group (in fact, the kill system commands (kill) is a user interface function). It should be noted that, kill () function not only can terminate the process (in fact is by sending a SIGKILL signal termination), can also be sent to other signal process.

And the kill () function is different, the raise () function to allow the process to send a signal to its.

Function format

The table below lists kill (respectively) and raise (format)


Basic experiment

This experiment makes use of fork () to create a child process, then in order to ensure the process is not in the parent process calls kill () before exit, the use of raise in the child process (SIGSTOP) function sends signals to itself, so that the child process to suspend. Then call in the parent process (kill) sending a signal to the child process, used in the experiment is SIGKILL. The following code:

Click here to download the kill_raise.c file

Compile execution result

You see, how hard ah, do not let the child process output twenty-second lines, directly with to shoot there.

In addition, suggest you take 27 lines of code to perform again try have a look have what different.

Signal catching function: alarm(), pause()

Function description

alarm()Also known as the alarm clock function, it can set a timer in the process, when the timer specifies the time when, it is to process SIGALARM signal transmission. It is necessary to pay attention to, a process can have only one alarm clock time, if you call alarm () has set the alarm time, any previous time alarm are replaced by new value.

pause()Function for the calling process to hang until the capture signal. This function is very common, usually can be used to determine whether the signal has to.

Function format



Basic experiment

This experiment is actually complete a simple sleep () function, procedure as shown below

The compiler implementation, the results as below

You can see the 12 line statement didn't perform, but think of the execution flow of a program is very clear, the first program timing, implementation to pause (11); when the process will be hung, when the timing, signal SIGALARM, then pause () to capture the signal, to direct the process is terminated.

Now screen out 11 lines, as follows

Compiler implementation again, results as below

Learn about the signal processing section, if must reprint, please indicate the source:

Finally, I wish you a happy Dragon Boat festival!! Also wish this year the golden nine silvers ten smooth!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Katherleen at December 16, 2013 - 7:46 PM