The signaling mechanism of Linux

Source: Internet
Author: User

A soft interrupt signal (signal, also referred to as a signal) is used to notify the process that an asynchronous event has occurred. Processes can send soft interrupt signals to each other through system call kill. The kernel can also send a signal to the process because of an internal event, notifying the process that an event has occurred. Note that the signal is only used to notify a process of what has happened and does not pass any data to the process.

Brief introduction

The process of receiving signals has different processing methods for various signals. The processing method can be divided into three categories: the first is similar to the interrupt handler, for the signal to be processed, the process can specify a handler function, which is handled by the function. The second method is to ignore a signal and do nothing about it, as if it had not happened. Of these, two signals can not be ignored: Sigkill and Sigstop; The third method is that the processing of the signal retains the default value of the system, which is the default operation for most signals, which causes the process to terminate. The process calls signal through the system to specify the processing behavior of a process for a signal.

The method by which the kernel sends a soft interrupt signal to a process is the bit that corresponds to the signal field of the process table entry where the process is located. This is to add that if the signal is sent to a sleeping process, then the process goes to sleep priority, and if the process sleeps at an interruptible priority, the process is awakened, otherwise only the corresponding bit of the signal field in the process table is set and the process is not awakened. This is important because the process checks whether the signal is received when a process is about to return from the kernel state to the user state, or when a process is going to enter or leave an appropriate low-dispatch priority sleep state.

    1. The time the kernel handles a signal received by a process is when a process returns to the user state from the kernel state. Therefore, when a process is running in the kernel state, the soft interrupt signal does not work immediately, and is not processed until it returns to the user state. The process will return to the user state only after processing the signal, and the process will not have an unhandled signal in the user state.
    2. The second note is that if the signal to be captured occurs when the process is in a system call, and the process sleeps at an interruptible priority, then the signal causes the process to make a longjmp, jump out of sleep, return to the user state, and perform a signal processing routine. When returned from the signal processing routine, the process returns as if it were returned from a system call, but returns an error code indicating that the system call was interrupted. It is important to note that the kernel in the BSD system can automatically restart system calls.
    3. The third thing to note: If the process sleeps on an interruptible priority, the process wakes up when it receives a signal to ignore, but does not do longjmp, and generally continues to sleep. But the user does not feel that the process has been awakened, but that the signal has not happened.

The semantics of SIGCHLD are: When a child process state changes to produce this signal, the parent process needs to call a wait function to determine what happened. By default, the process does not receive the signal, and if the parent process executes a system call to wait, the process wakes up from the system call wait and returns the wait call, performing a sequence of wait calls (identifying the zombie child process, releasing the process table entry for the child process), It is then returned from the wait. If the process captures this signal, it goes to the processing routine just like normal signal processing.

Realize

sighandler_t signal (int signum, sighandler_t handler);
However, this format has different type definitions in different systems, so it is best to refer to the online manual for this format.

In the call, the parameter Signum indicates the signal to set the processing method. The second parameter, handler, is a handler function, or
Sig_ign: Ignores the signal indicated by the parameter signum.
SIG_DFL: The default value is the processing method of the signal Signum by the recovery parameter.

The integer parameter passed to the signal processing routine is the signal value, which allows a signal processing routine to process multiple signals. The system call signal return value is the specified signal Signum the previous processing routine or error when the error code Sig_err is returned.

1#include <stdio.h>2#include <unistd.h>3#include <signal.h>4 5 voidSigroutine (intNO) {6     Switch(NO) {7          Casesigint:printf ("sigint\n"); Break;8          Casesigquit:printf ("sigquit\n"); Break;9     }Ten } One  A intMain () { -printf"process ID is%d\n", Getpid ()); - signal (SIGINT, sigroutine); the signal (sigquit, sigroutine); -      -      while(true); -  +     return 0; -}

CTRL + C is SIGINT.

ctrl+/is sigquit.

Kill-9 process_id Kill the process. SIGKILL.

System call Kill is used to send a signal to the process. The invocation declaration is in the following format:
int Kill (pid_t pid, int sig);
Add the following header file to the process that uses the call:
#include <sys/types.h>
#include <signal.h>

This system call can be used to send any signal to any process or process group. If the parameter PID is a positive number, the call sends the signal SIG to a process with a PID. If the PID is equal to 0, then the signal sig is sent to all processes in the process group to which the current process belongs. If the parameter PID equals-1, the signal sig is sent to all processes except process 1 and itself. If the parameter PID is less than-1, the signal sig is sent to all processes that belong to the process group-pid. If the parameter sig is 0, no signal will be sent. When the call executes successfully, the return value is 0, the error returns 1, and the corresponding error code errno is set.

The function of the system call to pause is to wait for a signal. The invocation is declared in the following format:
int pause (void);
Add the following header file to the process that uses the call:
#include <unistd.h>

This call causes the process that makes the call to sleep until a signal is received. The call always returns-1 and sets the error code to EINTR (a signal is received).

1#include <stdio.h>2#include <unistd.h>3#include <signal.h>4 5 voidSigroutine (intNO) {6     Switch(NO) {7          Casesigint:printf ("sigint\n"); Break;8          Casesigquit:printf ("sigquit\n"); Break;9     }Ten } One  A intMain () { -printf"process ID is%d\n", Getpid ()); - signal (SIGINT, sigroutine); the signal (sigquit, sigroutine); -      -      while(Pause ()); -     return 0; +}

int Getitimer (int which, struct itimerval *value);
int Setitimer (int which, const struct itimerval *value, struct itimerval *ovalue);
Add the following header file to the process that uses the two calls:
#include <sys/time.h>

The system calls the process to provide three timers, each with its own unique timing domain, when any one arrives, sends a corresponding signal to the process, and causes the timer to start over again. The three timers are specified by the parameter which, as follows:
Timer_real: Timed to the actual time, the timing arrives will send the SIGALRM signal to the process.
Itimer_virtual: Timing occurs only when the process is executing. The timed arrival will send a SIGVTALRM signal to the process.
Itimer_prof: Timed when the process executes and when the system performs an action for the process. As with itimer_vir-tual, this timer is often used to count the time spent by the process in the user state and kernel state. The timed arrival will send a sigprof signal to the process.

The parameter value in the timer is used to indicate the time of the timer, and its structure is as follows:
struct Itimerval {
struct Timeval it_interval; /* Next time value */
struct Timeval it_value; /* Set value for this time */
};

The structure of the TIMEVAL structure is defined as follows:
struct Timeval {
Long tv_sec; /* sec */
Long tv_usec; /* microseconds, 1 seconds = 1000000 microseconds */
};

In the Setitimer call, the parameter Ovalue if it is not empty, it retains the value of the last invocation setting. The timer decrements the It_value to 0 o'clock, generates a signal, sets the value of the It_value to It_interval value, and then restarts the chronograph, again and again. When It_value is set to 0 o'clock, the timer stops, or when it time expires, and It_interval stops at 0 o'clock. When the call succeeds, return 0; error, return-1, and set the corresponding error code errno:
Efault: The parameter value or Ovalue is an invalid pointer.
EINVAL: Parameter which is not one of itimer_real, Itimer_virt, or itimer_prof.

1#include <stdio.h>2#include <unistd.h>3#include <signal.h>4#include <sys/time.h>5 6 voidSigroutine (intNO) {7     Switch(NO) {8          Casesigalrm:printf ("sigalrm\n"); Break;9          Casesigvtalrm:printf ("sigvtalrm\n"); Break;Ten     } One } A  - intMain () { -printf"process ID is%d\n", Getpid ()); the signal (SIGALRM, sigroutine); - signal (SIGVTALRM, sigroutine); -  - Itimerval Real; +Real.it_value.tv_sec =5; -Real.it_value.tv_usec =0; +Real.it_interval.tv_sec =0; AReal.it_interval.tv_usec =500000; atSetitimer (Itimer_real, &Real, NULL); -      - itimerval VT; -Vt.it_value.tv_sec =2; -Vt.it_value.tv_usec =0; -Vt.it_interval.tv_sec =1; inVt.it_interval.tv_usec =0; -Setitimer (Itimer_virtual, &VT, NULL); to  +      while(Pause ()); -}

Pro-Test Ubuntu 14.04 does not receive the SIGVTALRM signal ...

Signaling mechanism for Linux

Related Article

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.