内核如何处理linux的alarm() [英] how linux's alarm() is handled by kernel

查看:265
本文介绍了内核如何处理linux的alarm()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 alarm()调用在Linux上的工作方式.
alarm(5)(警报(5))将在至少5秒钟内向发出此呼叫的进程发送 SIGALRM .
由于内核设置为0的递减计数器,因此在该时刻产生了警报.
我的怀疑在这里-我们可以有N个进程发出警报,并且为此目的,系统中存在一个计数器.因此,内核必须使用一个递减计数器来跟踪向其发送信号的所有过程.它是如何做到的?

I was reading about how the alarm() call works on the linux.
alarm(5) would send a SIGALRM in a minimum of 5 seconds to the process which has made this call.
The alarm is caused at that moment thanks to a down counter which is set by the kernel reaching zero.
My doubt is here - we can have N number of processes which make an alarm call, and there is one down counter in the system available for this purpose. So the kernel has to keep track of all the processes it has to send a signal to, with one down counter. How does it do it?

[它是否维护一个排序的链接列表,每个节点都表示该过程?]

[Does it maintain a linked list of sort, with each node signifying the process?]

推荐答案

我不是Linux内核开发人员,但是由于 alarm()的手册页,您已经可以预期其实现方式.

I'm no Linux kernel developer, but given the man-page for alarm(), you can already expect how it is implemented.

首先,很明显,警报值是针对每个进程的.由于内核已经保留了每个进程的数据结构( task_struct ) ,它只是在其中存储所需的闹钟时间.然后,Linux仅使用其内部计时器系统在指定时间注册回调.然后,上述回调会将SIGALRM传递给您的进程.

First, it is clear that the alarm value is per process. Since the kernel already keeps per-process data structures (task_struct), it just stores the desired alarm time in there. Then Linux just uses its internal timer system to register a callback at the specified time. Said callback then delivers the SIGALRM to your process.

不需要警报的内核全局状态或链接的显式链接列表.内核仅保留一个进程列表,并将警报超时存储为每个进程数据的一部分.

No need for kernel-global state or linked explicit linked lists for the alarms. The kernel simply keeps a process list and stores the alarm timeout as part of the per process data.

如果您想更深入地研究,则调用树看起来像这样:

If you want to dig deeper, the calling tree looks something like this:

  • sys_alarm()
  • alarm_setitimer()
  • do_setitimer(): This stores a kernel timer under as task_struct->signal->real_timer; it then passes calls
  • hrtimer_start(), which is part of Linux' internal high resolution timer API.

进一步挖掘,Linux高分辨率内核计时器系统可以完成很多事情,例如区分实时时间(如果有人更改计算机上的时间/日期可能会倒退)和单调时间.请参阅此LWN文章以获取概述

Digging even further, the Linux high-res kernel timer system does a whole number of things, e.g. distinguishing between real time (which may go backwards if someone changes the time/date on the computer) and monotonic time. Take a look at this LWN article for an overview.

出于这个问题的目的,它在内部保留了一个按到期时间排序的计时器列表(很快使计时器到期,最后使计时器到期).出于性能方面的考虑,它不是实现为双向链接列表,而是实现为 red-黑树.处理完任何回调后,hrtimers会查看其列表,选择第一个条目(最早到期的计时器),然后告诉底层硬件计时器及时中断以服务下一个事件.发生这种情况时,hrtimers会调用相关的回调,然后重复该过程.

For the purposes of this question, it internally keeps a list of timers ordered by expiry time (soon to expire timers first, later expiring timers last). For performance reasons, this is not implemented as a doubly-linked list but as a red-black tree. After the any callback has been handled, hrtimers looks at its list, picks the first entry (the soonest to expire timer) and then tells the underlaying hardware timer to interrupt in time to service the next event. When that happens, hrtimers invokes the relevant callback, and the process repeats.

因此有计时器列表(以树形式实现),但是没有明确的递减计数器,该部分通过硬件计时器进行处理.该部分源代码的主要参考资料是 enqueue_hrtimer() .

So there are timer lists (implemented as trees), but no explicit down counter, that part is handled via hardware timers. A leading reference for that part of the source code is enqueue_hrtimer().

这篇关于内核如何处理linux的alarm()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆