C++/Linux 中的线程排序

发布于 2024-11-03 04:10:07 字数 302 浏览 0 评论 0原文

我目前正在用 C++ 模拟硬盘驱动器 IO,并使用 pthread 线程和互斥体在磁盘上进行读取。

不过,我正在尝试通过对线程进行排序来优化阅读时间。问题是我的磁盘当前正在读取一个扇区,并且一堆读取请求到达,其中任何一个都将被执行。我想要的是对它们进行排序,以便接下来执行最接近扇区的请求。

这样,虚拟硬盘驱动器的磁头就不会过度移动。

我的问题是:使用 Linux 进程优先级系统是否是确保最接近的读取请求在其他请求之前执行的好方法?如果没有,我能依靠什么来做到这一点?

PS:抱歉我的英语。

感谢您的帮助。

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.

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? If not, what could I rely on to do this?

PS: Sorry for my english.

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事与诗 2024-11-10 04:10:07

依赖进程优先级方案的确切行为很少是一个好主意,特别是在像 Linux 这样的通用操作系统上,因为它们并不能真正保证您任何特定的行为。如果某个东西引用了内存中的某个地址或某个 I/O 调用,导致它暂时搁置,那么将某个东西设置为最高优先级将无济于事 - 然后操作系统将运行一些优先级较低的进程,而你会感到不愉快惊讶。

如果您想确定磁盘 I/O 请求的完成顺序,或者要模拟这一点,您可以创建一个线程来保存待处理 I/O 列表并要求一次执行一个请求,按照它控制的顺序。

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. 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.

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.

失眠症患者 2024-11-10 04:10:07

Linux 内核中的 I/O 调度程序可以重新排序和合并读取(以及在某种程度上写入),以便它们的排序对磁盘更有利,就像您所描述的那样。这会影响进程调度程序(它也负责线程),因为等待 I/O 的线程也会“重新排序”——它们的读或写请求按照磁盘为它们提供服务的顺序完成,而不是按照顺序他们在其中提出了要求。 (这是实际发生情况的一个非常简化的视图。)

但是如果您正在模拟磁盘 I/O,即如果您实际上没有执行真正的 I/O,则根本不涉及 I/O 调度程序。只有进程调度程序。并且进程调度程序不知道您正在“模拟”硬盘 - 它没有关于进程正在做什么的信息,只是关于它们是否需要 CPU 资源的信息。 (这又是事物如何运作的简化视图)。

因此,进程调度程序不会帮助您重新排序或合并读取请求的模拟。您需要在代码中实现该逻辑。 (阅读有关 I/O 调度程序的内容是个好主意。)

如果您确实提交了真实的 I/O,那么您自己进行重新排序可以在某些情况下提高性能,并且实际上 I/O 调度程序用于优化吞吐量或延迟的算法将提高性能。影响线程的调度方式(无论如何都会阻塞 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. 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. (This is a very simplified view of what really happens.)

But if you're simulating disk I/O, i.e. if you're not actually doing real I/O, the I/O scheduler isn't involved at all. 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. (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.)

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文