简体   繁体   English

sched_yield() 系统调用和实时调度策略

[英]sched_yield() system call and real time scheduling policies

I was wondering how to use the scheduling mechanisms in Linux kernel with multiple threads, and I saw that sched_yield() system call can send the current thread to the end of the process queue.我想知道如何使用多线程的Linux kernel中的调度机制,我看到sched_yield()系统调用可以将当前线程发送到进程队列的末尾。 Since SCHED_OTHER is the default scheduler policy, the manual doesn't recommend to use this system call while using SCHED_OTHER because it is intended to be used with real time scheduling policies ( man 2 sched_yield ) and is unspecified with SCHED_OTHER :由于SCHED_OTHER是默认调度程序策略,因此手册不建议在使用SCHED_OTHER时使用此系统调用,因为它旨在与实时调度策略 ( man 2 sched_yield ) 一起使用,并且未与SCHED_OTHER一起指定:

sched_yield() is intended for use with real-time scheduling policies (ie, SCHED_FIFO or SCHED_RR). sched_yield() 旨在与实时调度策略(即SCHED_FIFO 或SCHED_RR)一起使用。 Use of sched_yield() with nondeterministic scheduling policies such as SCHED_OTHER is unspecified and very likely means your application design is broken.未指定将 sched_yield() 与 SCHED_OTHER 等非确定性调度策略一起使用,这很可能意味着您的应用程序设计已损坏。

However, I saw this page from the RedHat documentation saying that sched_yield() shouldn't be used with real time tasks:但是,我从 RedHat 文档中看到这个页面sched_yield()不应该用于实时任务:

The sched_yield system call is used by a thread allowing other threads a chance to run. sched_yield 系统调用由允许其他线程有机会运行的线程使用。 Often when sched_yield is used, the thread can go to the end of the run queues, taking a long time to be scheduled again, or it can be rescheduled straight away, creating a busy loop on the CPU.通常在使用sched_yield时,线程可以go到运行队列的末尾,需要很长时间才能再次调度,或者可以直接重新调度,在CPU上创建一个繁忙的循环。 The scheduler is better able to determine when and if there are actually other threads wanting to run.调度程序能够更好地确定何时以及是否真的有其他线程想要运行。 Avoid using sched_yield on any RT task.避免在任何 RT 任务上使用 sched_yield。

So how do I should use the sched_yield() system call?那么我应该如何使用sched_yield()系统调用呢? What this would do with a SCHED_OTHER or a SCHED_FIFO policy?这对SCHED_OTHERSCHED_FIFO策略有什么影响? From what I understood is that with a SCHED_FIFO policy, the thread won't be interrupted unless a sched_yield() , and then the second requested thread will be processed unless it end, and the first thread will continue when all the thread queue has been processed (with a FIFO behavior).据我了解,使用SCHED_FIFO策略,除非sched_yield() ,否则线程不会被中断,然后第二个请求的线程将被处理,除非它结束,第一个线程将在所有线程队列结束后继续已处理(具有 FIFO 行为)。

Also, what this system call would do with the SCHED_RR policy, since all the threads are partially processed each quantum?另外,这个系统调用会对SCHED_RR策略做什么,因为所有线程都在每个量程中被部分处理?

Linus Torvalds' comment about sched_yield : Linus Torvalds 关于sched_yield的评论

The problem with that is "yield" is pretty much undefined.问题是“产量”几乎没有定义。 The definition of it is literally about single queue of a real-time behavior with a real-time scheduler with priorities.它的定义实际上是关于具有优先级的实时调度程序的实时行为的单个队列。

But that "definition" has almost nothing to do with actual usage.但那个“定义”几乎与实际使用无关。 There are various random people who use it, and some might use it for locking, while some use it for other things.有各种各样的随机人使用它,有些人可能将其用于锁定,而有些人将其用于其他用途。

... ...

sched_yield() is basically historical garbage. sched_yield()基本上是历史垃圾。 It's often literally wrong even for the defined usage because times have moved on from that whole "we run one thing at a time" long long ago.即使对于定义的用法,它也经常是错误的,因为很久以前,“我们一次只运行一件事”的时间已经过去了。 If your RT system actually has multiple concurrent threads (as opposed to being limited to a dedicated CPU) it's not really all that well-defined even there.如果您的 RT 系统实际上有多个并发线程(而不是仅限于专用 CPU),那么即使在那里也不是那么明确。 But at least there you can still pretend it is.但至少在那里你仍然可以假装它是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM