Inter-Process Communication Linux # # # signal (sinal)

In computer science, the signal is Unix, Unix-like, and one kind among other POSIX-compliant operating system, the communication process has restricted manner. It is an asynchronous notification mechanism, a process used to alert event has occurred. When a signal is sent to a process, the process of the operating system interrupts the normal flow of control, this time, any non-atomic operation will be interrupted. If the process is defined signal processing functions, then it will be executed, otherwise execute the default handler.

Soft interrupt signal (signal, and the signal for short) is used to inform the process of asynchronous events occurred. You can call each other kill soft interrupt signal is sent through the system between processes. The kernel can also send a signal to the process because of internal events, the occurrence of an event notification process. Note that the signal is only used to notify a process of what happened event, the process is not to transfer any data. Process received signals have different approaches to various signals. The method of treatment can be divided into three categories: the first one is similar to the interrupt handler, the signal to be processed, the process can be specified processing functions for processing by the function. The second method is to ignore a signal, the signal is no treatment, just not happened. The third method is the default value of the signal processing system is retained, such default operation, the default action is such that most of the signal process terminates. The process to specify the behavior of the process for handling a signal call signal through the system. There are entries in the process table in a soft interrupt signal domain, the domain of each bit corresponds to a signal, when a signal is sent to the process, the corresponding bit is set. It can be seen, the process can be reserved for different signals at the same time, but for the same signal, the process does not know how many came before processing.

Semaphore Signal and these two terms is only one word, sometimes we will be very easy to think of these two things is not a thing, but it is two completely different concepts.

  • Signal: is sent by the user, system, or process information to the target process to inform the target process to change the system or a state of exception. Notification process produces an event
  • Semaphore : semaphore is a special variable, its essence is a counter which records the number of semaphores critical resources, the number of how many, how much the value of the semaphore is on, the process of accessing all its atomic operations (pv operation , p: footprint, v: release resources). Its role is that co-ordination process accesses to shared resources, so that at the same time a critical section is only a process to access it. For the synchronization process (for co-ordination process access to shared resources)

The following situations can cause signal

  • Keyboard events   such as SIGINT signal generating ctrl + c, ctrl + \ SIGQUIT generated signal, ctrl + z signal generating SIGTSTP
  • Hardware exceptions   hardware failure. Illegal Access Memory (burst error), except 0 (floating point exception), memory alignment errors (bus error) will generate a signal detected by the hardware and notifies the kernel, the kernel then sends the appropriate signal to the process, such as division by zero is performed instruction, the illegal process to access the memory address, cpu arithmetic unit generate an exception, the exception will be interpreted as a kernel signal is sent to a process.
  • System call   kill, abort, raise system function calls, etc.
  • Software conditions   using a timer alarm
  • System command    kill

Signal is an asynchronous event, when the signal arrives, save the current execution environment, turn to perform signal processing functions, when the signal handler is completed, the recovery site, continue.

Process signals received three treatments mode

  • Default: The default action to perform signal processing, if it is SIGINT system, then this process will be interrupted
  • Ignore: We do not deal with the signal, pretended not to see SIGKILL SIGSTOP can not be ignored
  • Capture and processing: when the signal came, the execution code to write our own to provide a signal processing function, requires the core to switch to when processing signals from the user mode to perform this processing function (capturing signal this action is we need done) SIGKILL SIGSTOP can not capture

Sign up signal

typedef void  (* sighandler_t) (int);
sighandler_t signal (int signum   /*要注册的信号*/, sighandler_t handler /*信号执行函数*/);

Signal capture

  • (1) First, the user is normally performed in such a main control flow due to an interrupt, exception or system call directly into the kernel mode for processing exceptions,
  • (2) the kernel processed abnormal ready to return to user mode, and before that the current process will see there is no signal can reach, if there is to be handed over to the signal processing,
  • (3) If the signal processing function is to return to user mode user-defined signal processing function to perform user-defined
  • (4) After performing signal processing functions, the system will call a particular function call sigreturn once again into the kernel mode, the system performs call
  • (5) This system after the call is completed, it will return to the main control flow is interrupted place continues to execute the following code
  • (6) when the main control flow of execution if an exception is encountered again, interrupt or system call will continue to return to (1), continue with the following procedure

 

Reliable and unreliable signal signal

Real-time signal: is a reliable signal
of non-real-time signal: unreliable signal

Unreliable signal

  • 1-31 are not reliable, there will be signal loss phenomenon
  • Linux signals inherited from the earlier Unix signal, the defect signal of Unix
  • Signal processing function is finished, the signal processing reset to the default mode (Linux have improved)
  • There will be loss of signal, the signal does not line up

Reliable signal

  • A signal set 34-64 redesign
  • The signal will not be lost support line, signal processing function is finished, will not revert to the default processing mode

 

[] Kill -l  command you can view all of the signals it is now the signal has been increased to 65, but here I would like to mention, 33-64 These signals are generally not from the use of this is to distinguish a reliable signal and unreliable signal 32 newly added signals.

cll@cll-linux:~ $ kill -l
 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 6) SIGABRT	 7) SIGBUS	 8) SIGFPE	 9) SIGKILL	10) SIGUSR1
11) SIGSEGV	12) SIGUSR2	13) SIGPIPE	14) SIGALRM	15) SIGTERM
16) SIGSTKFLT	17) SIGCHLD	18) SIGCONT	19) SIGSTOP	20) SIGTSTP
21) SIGTTIN	22) SIGTTOU	23) SIGURG	24) SIGXCPU	25) SIGXFSZ
26) SIGVTALRM	27) SIGPROF	28) SIGWINCH	29) SIGIO	30) SIGPWR
31) SIGSYS	34) SIGRTMIN	35) SIGRTMIN+1	36) SIGRTMIN+2	37) SIGRTMIN+3
38) SIGRTMIN+4	39) SIGRTMIN+5	40) SIGRTMIN+6	41) SIGRTMIN+7	42) SIGRTMIN+8
43) SIGRTMIN+9	44) SIGRTMIN+10	45) SIGRTMIN+11	46) SIGRTMIN+12	47) SIGRTMIN+13
48) SIGRTMIN+14	49) SIGRTMIN+15	50) SIGRTMAX-14	51) SIGRTMAX-13	52) SIGRTMAX-12
53) SIGRTMAX-11	54) SIGRTMAX-10	55) SIGRTMAX-9	56) SIGRTMAX-8	57) SIGRTMAX-7
58) SIGRTMAX-6	59) SIGRTMAX-5	60) SIGRTMAX-4	61) SIGRTMAX-3	62) SIGRTMAX-2
63) SIGRTMAX-1	64) SIGRTMAX	

[Man 7 signal]  This command uses the man to see the manual, because in the seventh page, so the direct use man 7. Common standard signal as follows:

Standard signals
       Linux  supports the standard signals listed below.  Several signal num‐
       bers are architecture-dependent, as indicated in  the  "Value"  column.
       (Where three values are given, the first one is usually valid for alpha
       and sparc, the middle one for x86, arm, and most  other  architectures,
       and  the  last one for mips.  (Values for parisc are not shown; see the
       Linux kernel source for signal numbering on that  architecture.)   A  -
       denotes that a signal is absent on the corresponding architecture.)

       First the signals described in the original POSIX.1-1990 standard.

       Signal     Value     Action   Comment
       ──────────────────────────────────────────────────────────────────────
       SIGHUP        1       Term    Hangup detected on controlling terminal
                                     or death of controlling process
       SIGINT        2       Term    Interrupt from keyboard
       SIGQUIT       3       Core    Quit from keyboard
       SIGILL        4       Core    Illegal Instruction
       SIGABRT       6       Core    Abort signal from abort(3)
       SIGFPE        8       Core    Floating point exception
       SIGKILL       9       Term    Kill signal
       SIGSEGV      11       Core    Invalid memory reference
       SIGPIPE      13       Term    Broken pipe: write to pipe with no
                                     readers
       SIGALRM      14       Term    Timer signal from alarm(2)
       SIGTERM      15       Term    Termination signal
       SIGUSR1   30,10,16    Term    User-defined signal 1
       SIGUSR2   31,12,17    Term    User-defined signal 2
       SIGCHLD   20,17,18    Ign     Child stopped or terminated
       SIGCONT   19,18,25    Cont    Continue if stopped
       SIGSTOP   17,19,23    Stop    Stop process
       SIGTSTP   18,20,24    Stop    Stop typed at terminal
       SIGTTIN   21,21,26    Stop    Terminal input for background process
       SIGTTOU   22,22,27    Stop    Terminal output for background process

       The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.

       Next  the  signals  not  in  the POSIX.1-1990 standard but described in
 Manual page signal(7) line 134 (press h for help or q to quit)

In the above signal,

Program can not capture, block or ignore the signals 9) SIGKILL,19) SIGSTOP
Not return to the default action of the signal 4) SEAL, 5) SIGTRAP
Default would lead to a miscarriage of process signals 6) SIGABRT, 7) SIGBUS, 8) SIGFPE, 4) SEAL, 29) SIGIO, 3) SIGQUIT, 11) SIGSEGV, 5) SIGTRAP, 24) SIGXCPU, 25) SIGXFSZ
The default will lead to signal process to exit 14) SIGALRM, 1) SIGHUP, 2) SIGINT, 9) SIGKILL, 13) SIGPIPE, SIGPOLL (sent when an event is sent to Pollable Device, the part of the system), 27) SIGPROF, 31) SIGSYS, 15) SIGTERM, 10) SIGUSR1,12) SIGUSR2,26) SIGVTALRM
The default will lead to signal process to stop 19) SIGSTOP, 20) SIGTSTP, 21) SIGTTIN, 22) SIGTTOU
The default process ignores the signal 17) SIGCHLD, 30) SIGPWR, 23) Sigurd, 28) SIGWINCH
  • In addition, SIGIO is in SVR4 exit, is ignored in 4.3BSD;
  • SIGCONT is to continue to hang in the process, otherwise it is ignored and can not be blocked.

Default operation:

  •               Term: terminate the process
  •               Ign: ignore signals (default instant ignored the kind of signal operation)
  •               Core: terminate the process, generating Core files. (Identification process causes of death, gdb for debugging)
  •               Stop: Stop (pause) process
  •               Cont: Continue running processes

 

kill、killall、kill(pid_t pid, int sig);

kill、killall

These two commands are used to send signals to the process. kill command requires a process number as an argument, while killall requires a process name. Further, also may be added to the transmission signal number as a parameter, after these two commands. By default, they are sent to the relevant process signal 15 (TERM). For example, if you want to terminate the process with PID 666, enter the following command:

kill 666

If you want to send it signal 9 (SIGKILL), enter:

kill -9 666

Suppose you want to know the process to terminate the command name. You may terminate it by that name, but do not have to use ps to find the process ID of the process:

killall -9 your-process-name

 

kill(pid_t pid, int sig);

For sending a signal to any process group or process.

Header file Usage:

#include <sys/types.h>
#include <signal.h>
int kill(pid_t pid, int sig);

Parameters:
pid: There are four possible options

  • 1. pid is greater than zero, pid identification signal is to be sent to the process.
  • 2. pid equal to zero when the signal will be sent to all belong to the same group and use that process calls kill () process.
  • 3. pid equal to -1, the signal will be sent to all of the calling process to the process the right to send a signal, in addition to the process 1 (init).
  • When 4. pid is less than -1, the signal is sent to the process group ID -pid.

sig: ready signal transmitted code, its value is zero if no signal is sent, but the system will perform error checking, often use sig value of zero to check if a process is still being executed.

Published 170 original articles · won praise 207 · Views 4.59 million +

Guess you like

Origin blog.csdn.net/xiaoting451292510/article/details/103731042