简体   繁体   English

C ++ / Linux中的线程排序

[英]Threads ordering in C++/Linux

I'm currently doing a simulation of a hard disk drive IOs in C++, and I'm using pthread threads and a mutex to do the reading on the disk. 我目前正在用C ++对硬盘驱动器IO进行仿真,并且正在使用pthread线程和互斥锁来读取磁盘。

However I'm trying to optimize the reading time by ordering my threads. 但是,我正在尝试通过订购线程来优化阅读时间。 The problem is that is my disk is currently reading a sector, and a bunch of requests to read arrive, any of them will be executed. 问题是我的磁盘当前正在读取一个扇区,并且一堆读取请求到达,其中的任何一个都会被执行。 What I want is ordering them so that the request with the closest sector is executed next. 我想要的是对它们进行排序,以便接下来执行具有最接近扇区的请求。

This way, the head of the virtual hard disk drive won't move excessively. 这样,虚拟硬盘驱动器的磁头不会过度移动。

My question is : Is using Linux process priority system a good way to make sure that the closest reading request will be executed before the others? 我的问题是:使用Linux进程优先级系统是否是确保最接近的读取请求在其他请求之前执行的好方法? If not, what could I rely on to do this? 如果没有,我可以依靠什么来做到这一点?

PS: Sorry for my english. PS:对不起,我的英语。

Thanks for your help. 谢谢你的帮助。

It is very rarely a good idea to rely on the exact behaviour of process priority schemes, especially on a general purpose operating system like Linux, because they don't really guarantee you any particular behaviour. 最好不要依赖进程优先级方案的确切行为,尤其是像Linux这样的通用操作系统,因为它们不能真正保证您的任何特定行为。 Making something the very highest priority won't help if it references some address in memory or some I/O call that causes it to held up for an instant - the operating system will then run some lower priority process instead, and you will be unpleasantly surprised. 如果最高优先级的东西引用了内存中的某个地址或某些导致其被搁置的I / O调用,将无济于事-操作系统将改为运行一些较低优先级的进程,而您将不满意惊讶。

If you want to be sure of the order in which disk I/O requests are completed, or to simulate this, you could create a thread that keeps a list of pending I/O and asks for the requests to be executed one at a time, in an order it controls. 如果要确定完成磁盘I / O请求的顺序,或者要模拟该过程,可以创建一个线程,该线程保留一个挂起的I / O列表,并请求一次执行一个请求。 ,以它控制的顺序。

The I/O schedulers in the Linux kernel can re-order and coalesce reads (and to some extent writes) so that their ordering is more favorable for the disk, just like you are describing. 正如您所描述的那样,Linux内核中的I / O调度程序可以重新排序并合并读取(在某种程度上可以进行写入),以便它们的排序对磁盘更有利。 This affects the process scheduler (which takes care of threads too) in that the threads waiting for I/O also get "re-ordered" - their read or write requests complete in the order in which the disk served them, not in the order in which they made their request. 这会影响进程调度程序(它也负责线程),因为等待I / O的线程也会“重新排序”-它们的读取或写入请求是按照磁盘为它们服务的顺序完成的,而不是按顺序进行他们提出了要求。 (This is a very simplified view of what really happens.) (这是对实际情况的非常简化的视图。)

But if you're simulating disk I/O, ie if you're not actually doing real I/O, the I/O scheduler isn't involved at all. 但是,如果您正在模拟磁盘I / O,即,如果您实际上并未在进行真正的I / O,则根本不涉及I / O调度程序。 Only the process scheduler. 仅流程调度程序。 And the process scheduler has no idea that you're "simulating" a hard disk - it has no information about what the processes are doing, just information about whether or not they're in need of CPU resources. 而且,进程调度程序不知道您正在“模拟”硬盘-它不具有有关进程在做什么的信息,只有有关它们是否需要CPU资源的信息。 (Again this is a simplified view of how things work). (同样,这是事情的简化视图)。

So the process scheduler will not help you in re-ordering or coalescing your simulation of read requests. 因此,流程调度程序将无法帮助您重新排序或合并对读取请求的模拟。 You need to implement that logic in your code. 您需要在代码中实现该逻辑。 (Reading about I/O schedulers is a great idea.) (了解I / O调度程序是一个好主意。)

If you do submit real I/O, then doing the re-ordering yourself could improve performance in some situations, and indeed the I/O scheduler's algorithms for optimizing throughput or latency will affect the way your threads are scheduled (for blocking I/O anyway - asynchronous I/O makes it a bit more complicated still). 如果您确实提交了实际的I / O,则在某些情况下自己进行重新排序可以提高性能,并且确实I / O调度程序用于优化吞吐量或延迟的算法会影响线程的调度方式(用于阻塞I / O)无论如何-异步I / O使其变得更加复杂)。

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

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