当前位置:网站首页>The use of signal function (signal) in C language

The use of signal function (signal) in C language

2022-08-09 11:16:00 XV_

Let's briefly talk about the signal in C language (signal)

First of all, signal is a function in the C language library. It is actually a soft interrupt, which is a terminal issued by the software. In essence, it is similar to int n.

For the process that receives the soft interrupt signal, it will stop the work at hand and go to execute the signal handler signal handler.

For this, it is similar to hardware interrupts.It's easier to learn by analogy with hardware interrupts!

So what we need to know is

  • What soft interrupts are available: like hardware divide by zero interrupt, overflow interrupt, peripheral interrupt...
  • What are the soft interrupt handlers: like interrupt service routines (location information is in idt, ivt)
    • Default soft interrupt service routine: the default interrupt service routine is initialized when the system starts
    • User-defined software interrupt service routine: the operating system can customize the interrupt service routine

And need to understand

  • When the interrupt is triggered (trigger condition)
  • Who issued the interrupt
  • Who received the interrupt

The above are all abstract levels of understanding, we are a thread of the process

  1. Process x executes
  2. Process x triggers an interrupt --> issues a soft interrupt
  3. Process y receives an interrupt, what should process x do
  4. Process y stops the current task and executes the interrupt service routine instead
  5. The execution is complete, and the process y continues to execute its own program

As for processes x and y, yes

  • Same process
  • Two processes
    • Parent process and child process
    • As for the two independent processes, leave it alone for now. To do this, at least the two must be able to communicate

Okay, the overall framework is sorted out, let's talk slowly.

What are the signals

All signals in c/c++

The reference link above gives all the signals, now we will briefly illustrate them.

SignalValueDescription
SIGCHLD17Child status has changed (POSIX). Signal sent to parent process whenever one of its child processes terminates or stops. See the YoLinux.com Fork, exec, wait, waitpid tutorial
SIGCHLD signal: parent process child process

Please see my other article: The use of the semaphore SIGCHLD, how to let the parent process know that the child process executesEnd, how to let the parent process distinguish the end of multiple child processes

Signal handler

Reference:
[1] Signals in C language
[2] How to use signal handlers in C language?

There are two types of signal processing functions

  • Processing functions that come with the system
  • User-defined function

Similar to the underlying hardware interrupt, the interrupt vector table is built-in, and user-defined is also allowed.

There are also links to default handling of interrupts and custom handling.

Who issued?Who receives it?

  • Originator: operating system, or a process
  • Receiver: a process

As for the details, it depends on which signal you use.The emitting and receiving actions and objects of each signal, as well as the default handler functions, are specified.

How does it work?

I won't mention this anymore...just a few routines, it's easy.

It should be noted that if process 1 receives signal X from other processes, before process 1 executes the signal x processing function, if multiple signals x are sent to process 1, then these signals may be combined and the process1 executes the signal processing function only once.There could be many reasons for this, such as the operating system being busy...


I have not dealt with the following reference link:

Handling multiple SIGCHLD

原网站

版权声明
本文为[XV_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091103197895.html