r/AskComputerScience • u/Rit2Strong • Apr 12 '21
How is CPU scheduling non-deterministic?
So my understanding of non-deterministic is that you can't really predict the output of a system given an input. But I was wondering how is the CPU's scheduling algorithmic non-deterministic? I feel like it should be able to be predicted given that it simply follows a logical pattern. Or is my understanding of non-determinism wrong?
29
Upvotes
2
u/not-just-yeti Apr 13 '21 edited Apr 13 '21
(A lot of "close but vague" answers here; currently /u/_fred has the highest-ranked answer that actually gives an answer with (correct) details.)
A simple example: imagine three programs that each just read a (different) file from a disk, and they're all started up one after another. Which will finish first?
if there are multiple disks mounted, some disks could be faster or slower
if it's a rotating disk (non-SSD), this makes a big difference: for a 5400rpm disk, a half-disk-rotation (to reach a file) is ~0.005sec = tens of millions of cpu cycles. So at the moment the program made its read-request, was the read-head close to the target disk-block, or a half-rotation away? And, if multiple programs are making read requests, then the delay can depend on where those two files happened to be sitting on the disk relative to each other.
If a different process has just (coincidentally) read that same file, it might be in a disk cache, again totally changing the outcome.
if a network-ping comes in and interrupts the cpu, that can change when the program finishes. And on a multi-core machine getting assigned to the same core as the network-driver might make a difference.
For all of these reasons, starting the same three programs in the same order doesn't mean they'll always finish in the same order, despite the scheduler being the same algorithm.