Linux soft interrupt communication

Process response to signal

(1) Ignore the signal  

  No action is taken, and there are two signals that cannot be ignored: SIGKILL and SIGSTOP. If omitted, the system management cannot kill or suspend the process, and cannot manage the system.

(2) Capture and process the signal   

(3) Perform the default operation, usually to terminate the process

  The default operation of the signal: view through man 7 signal

System call or library function

1. lockf (fd, function, size) is used for process mutual exclusion

2. Int kill (pid, sig) transmits interrupt signal

(1) When pid>0, the core sends a signal to the process pid.
(2) When pid=0, the core sends the signal to all processes in the same group as the sending process.
(3) When pid=-1, the core sends the signal to all processes whose user ID is really equal to the effective user ID of the sending process.

3. If signal(sig, function) receives an interrupt signal, it will be processed accordingly

   Among them, sig is used to specify the type of signal, and sig of 0 means that no signal is received. See the end of the article for commonly used signals and corresponding key values.

    function: the address of a function in the process

   (1) When function=1, the process ignores sig signals, that is, shields such signals;
   (2) When function=0, the default value, the process should terminate itself after receiving the sig signal;
   (3) ) When function is a non-zero or non-1 type integer, the value of function is used as a pointer to the signal handler.

4、wait()和waitpid()

wait() will temporarily stop the execution of the process until a signal comes or the child process ends.

 

The role of waitpid and wait is essentially the same, but waitpid has two more parameters, pid and options,

pid: When pid>0, only wait for the child process whose process ID is equal to pid, regardless of the child process

        When pid=-1, wait for any child process to exit without any restriction. At this time, waitpid has the same function as wait

        When pid=0, wait for any child process in the same process group. If the child process has joined another process group, waitpid will not pay any attention to it.

        When pid<-1, wait for any child process in a specified process group whose ID is equal to the absolute value of pid.

options provides some additional options to control waitpid. Currently, only two options, WNOHANG and WUNTRACED, are supported in Linux. These are two constants. You can use the "|" operator to connect them together, such as: ret=waitpid(- 1,NULL,WNOHANG | WUNTRACED). If we don’t want to use them, we can also set options to 0

5. sleep() and pause()

The sleep(n) process pauses for n seconds, ctrl+c, the SIGINT signal, can end sleep early and continue running.

pause() will pause the current process (go to sleep) until it is interrupted by a signal.

6、alarm()

alarm() is used to set the signal SIGALRM to be sent to the current process after the seconds specified by the parameter seconds. If the parameter seconds is 0, the previously set alarm will be cancelled and the remaining time will be returned.

Return value: Return the remaining seconds of the previous alarm, or 0 if no alarm has been set before.

You can set to ignore or not capture this signal. If the default mode is used, the action is to terminate the process that calls the alarm function.

 

Attachment: Common signals and corresponding key values ​​are as follows:

01 SIGHUP hangup

02 SIGINT interrupt, when the user presses ^c or ^break from the keyboard

03 SIGQUIT exits when the user presses the quit key from the keyboard

04 SIGILL illegal instruction

05 SIGTRAP trace trap, start the process, trace the execution of the code

06 SIGIOT IOT instruction

07 SIGEMT EMT instruction

08 SIGFPE floating point operation overflow

09 SIGKILL kill and terminate the process 

10 SIGBUS bus error

11 SIGSEGV segmentation violation (segmentation violation), the process tries to access a location outside of its virtual address space

12 Wrong parameters in SIGSYS system call, such as illegal system call number

13 SIGPIPE writes data to a non-read pipeline

14 SIGALRM alarm clock. This signal is sent when a process wants to receive a signal after a certain time

15 SIGTERM software termination (software termination)

16 SIGUSR1 User-defined signal 1

17 SIGUSR2 User-defined signal 2

18 SIGCLD A child process died

19 SIGPWR power failure
Among them, SIGUSR1 and SIGUSR2 are user-defined signals, and the default processing after receiving the signal is process termination
 

Guess you like

Origin blog.csdn.net/qq_44378854/article/details/106162576