Public account: Direct welcome attention


qrcode_for_gh_4fc7f02f29ce_258 (3).jpg

Vocabulary Learning Record:

English Chinese
occur on …. Occurred in… Act on on
occur in …. Happen to…

Signal mechanism

Now we’ll look at a higher level of exception control flow in software form known as

Linux Signal

Allows a process or kernel process to interrupt other processes.

Linux signals

number name default action corresponding event
1 SIGHUP Terminate Terminal line hangup
. Details to be improved later
30 SIGPWR Terminate Power failure

7 signal [man] man7.org/linux/man-p…

Signal Dispositions

Each signal has a current disposition, which determines how the process behaves when it is delivered the signal.

The entries in the “Action” column of the table below specify the default disposition for each signal, as follows:

Term Default action is to terminate the process. Ign Default action is to ignore the signal. Core Default action is to terminate the process and dump core (see core(5)). Stop Default action is to stop the process. Cont Default action is to  continue the process if it is currently stopped.

The translation

The signal processing

Each signal has a current configuration that determines how a process will behave when it receives the signal.

The entries in the Action column in the following table specify default Settings for each signal (see above)

This configuration can also be passed

sigaction

signal

System Call changed. Using these System Call processes, the System Call process can choose one of the following actions to apply to the passed signal: a) default behavior b) ignore c) programmer custom actions such as try… catch…

A signal is a small piece of information that informs a process that an event of some type has occurred in the system. Each signal corresponds to some kind of system event. Low-level hardware exceptions are handled by the kernel exception handler and are not visible to the user. Signals provide a mechanism to expose the current exception to the user process.

For example, when a process tries to divide by 0, the kernel sends SIGFPE to a process.

Signaling technology

Signals are passed to the target process in two explicit steps:

  • Sending signals: The kernel sends signals to the target process by changing some state of the target process’s context. Signals are transmitted for one reason:

    • The kernel detected some events on the system

    • The process called

      kill

      function

  • Receive signal: When the target process is forced by the kernel to react in a certain way to a transmitted signal, the target process accepts the signal. The target process can ignore, terminate, or catch signals.

A signal that has been sent but not yet received is called a pending signal. At any time, a type of signal has at most one pending signal. Excess will be discarded. A process can selectively block to receive certain signals.

The kernel maintains a collection of Pending signals through a Pending bit Vector and blocked Bit Vector. When a signal of type K is sent, the KTH bit of the pending bit Vector is set. When the signal is received, the setting is canceled.

Send a signal

All of the signaling mechanisms in Unix systems rely on the concept of process groups.

  • Process group
    • Each process belongs to an explicit process group that has an explicit, positive integer ID called the Process Group ID

      getpgrp

      The getgroup () function gets the group ID of the current process.

    • Child processes and parent processes belong to the same process group and can pass

      setpgid

      Change that

  • Signals are sent through the /bin/kill program
  • Send signals via keyboard (e.g. Ctrl + C)
  • Send signals by calling kill and alarm functions

    int kill (pid_t pid, int sig); unsigned int alarm(unsigned int secs); 
Received signal

When process P moves from kernel mode to user mode, the kernel checks the set of non-blocking signals for process P. If the set is empty, the kernel passes control to the p process’s next instruction. If it is not empty, the kernel forces process P to receive signal K (usually, k is minimal). The receiving signal triggers some action of the process, and when the action is complete, control is passed to the next instruction of the P process.

Note that each type of signal has a default action. These default actions can pass

signal function

Changed, but the default behavior of SIGSTOP and SIGKILL cannot be changed.

You can view the API of this method using man 2 Signal

When a process P catches a signal of type K, it calls the handler/processor by setting the parameter K, so that the handler processes the signal of type bit K.

When the processor executes its own return statement, control is usually passed to the instruction p interrupted by the signal. Usually, that’s because some systems return an error.