26 ms·
My favorite bit from queuing theory is the observation that if you execute the shortest tasks first, the average wait time for any one task is minimized.
by hinkley 7y ago
My favorite bit from queuing theory is the observation that if you execute the shortest tasks first, the average wait time for any one task is minimized.
- amelius 7y agoCurious, does Linux take this into account when it schedules processes? (Of course, it can't know the execution time of a process beforehand, but it can deprioritize long-running processes, since the ETA for a long-running process is larger than for a short-running process).
- Filligree 7y agoOf a sort. It down-prioritizes processes which were previously descheduled by timer, and up-prioritizes those which are waiting for I/O. This generally has the desired effect of prioritizing interactive work. If you mean prioritization based on total CPU time then no, I don't think it does that, but the above does approximate prioritization based on a (very small) sliding window of CPU use.
- hinkley 7y agoI wonder if that policy is purely empirical or if the people who decided to favor I/O had a more sophisticated understanding of the system. Since a lot of algorithms have a log(n) component to them, either expressly or due to CPU physics, calling a function twice as often with half as much data may reduce call time a little bit, even a hair. I could see someone making a design decision based on a 2% disparity between two alternatives.
- lapinot 7y agoAre you sure? Aren't you talking about the average wait time for all tasks combined (sum of wait times) instead of the wait time for any one task. Obviously the optimal wait time any one task is 0, if you start right away. Intuitively i'd say if you sort the queue by size and always process the shortest task, you may indefinitely stall some long tasks.
- isaacg 7y agoYou're right, it's average wait time over all tasks. However, processing the shortest tasks first doesn't delay the long tasks much, if length is reasonably variable, because the short tasks are almost negligible to the long tasks, and the long take don't need to wait on even longer tasks. If long tasks are stalling indefinitely under this policy, then under FCFS the queue length with grow without bound, and that's not really much better.
- hinkley 7y agoYes, the average wait time, full stop. Now that I'm rereading, I'm not even sure what 'average wait time for one task' was supposed to mean. Averages are aggregate data about multiple samples.
- hinkley 7y agoI think that depends on whether your queue is a fixed data set or a stream of new entries. I learned queuing theory in a class about multimedia, and I came away with a good theoretical understanding but my practical understanding was rubbish. When synchronizing two streams of tasks, the queue is not finite, but the pattern is fixed. For A/V it might be 22 frames per second, best effort, and exactly one second of audio per second. But any queue with even a vague notion of deadlines will only let so many tasks 'cut in line'. At some point the undesirable task gets scheduled, even in the face of a constant stream of other work. Or it gets dropped. From what I understand, in a realtime system a low priority, long task might just be aborted. Because it's never a good time to run it.
- jacques_chester 7y agoYou can go further. There's a result that if you know how much processing time is left for a job, and can pre-empt it, then the most effective scheduling rule is "Shortest Remaining Processing Time" or SRPT. Harchol-Balter calls this the All-Can-Win Theorem, because it counterintuitively shows that jobs with more time to go being interrupted by jobs with less time to go will be better off. https://www.cs.cmu.edu/~harchol/all-can-win.pdf https://www.cs.cmu.edu/~harchol/all-can-win.pdf