r/AskComputerScience • • 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

14 comments sorted by

16

u/LordNiebs Apr 12 '21

I'm not really familiar with what you are talking about here. Why do you think CPU scheduling is non-deterministic?

However, it's definitely the case that CPU scheduling is non-deterministic from the perspective of a program running on that CPU in a modern computing environment. If your program runs on a modern multi-threaded operating system and/or with lots of other programs running, your program will not be able to deterministically assess when it will run, because a dynamic number of other programs and their instructions will be executed at any given time. There are so many different tasks happening on a computer, and the order of those tasks changes so rapidly that there is a strong element of randomness to it. Perhaps you could deterministically predict CPU scheduling from the perspective of the operating system, but not from that of a program.

3

u/Rit2Strong Apr 12 '21

Oh, so the CPU scheduling algorithm is deterministic hardware/OS wise, in the sense that you can predict it by hand if needed, but in terms of someone coding in Java, for example, there would be so many different tasks that the computer needs to do that it's seen as non-deterministic. Is that right?

4

u/jhaluska Apr 12 '21

Correct, CPU scheduling algorithm is deterministic...

But in practice, hardware is slightly different on ram timings, user inputs, CPU loads, cache validations, tiny hardware timing crystal variations (even due to temperature) to make it nearly impossible to have two identical runs even on the same machine.

4

u/dnswblzo Apr 12 '21

The main part that makes things so non-deterministic is input and output. Input-wise, when you press a key on your keyboard, or make a movement with a mouse or trackpad, or receive a message from the internet, the operating system needs to step in and determine what to do with those events. The operating system needs to use the CPU to do that, so scheduling is affected by these events, which are unpredictable.

5

u/LordNiebs Apr 12 '21

I am not super knowledgeable on OSes or CPU scheduling, but that is my understanding, yes.

For something to be deterministic you need to have full awareness of all of the moving parts. On an OS, that means you need to be aware of every instruction and every bit of data that will be run up to the point where you are trying to predict what will happen. Generally speaking, a computer program would not have access to the vast majority of that information, and if it did that would likely be a major security risk. You could design an OS to run deterministically if there is no input required, but that isn't what normally happens.

Speaking of which, you might be interested in Realtime computing.

1

u/Poddster Apr 12 '21

Note that you can run a computer on a single core, single threaded CPU just like we did before the 00's. The scheduling there was non-deterministic as well :) Factoring in the fact that the CPU schedules things as well doesn't help it become any more non-deterministic.

1

u/darthandroid Apr 12 '21

That is correct.

Software is by definition deterministic; Hardware is by definition non-deterministic (if for no other reason than it might fail at any moment unpredictably).

If you ignore the hardware and consider only a software component (the process scheduling algorithm), it will behave deterministically given a fixed set of inputs (list of processes it has to schedule, process priorities, etc.). If you ignore the software and consider only the hardware, it behaves with true randomness.

Once you start mixing hardware and software in the real world, you end up with a system that is more or less deterministic depending on how much hardware plays a role, or how much software plays a role, but neither fully deterministic or non-deterministic.

For something like a CPU scheduler, there’s a lot of hardware involved. The exact clock speed, memory speed, interactions with a user launching, using, and closing processes, available memory, speed of swap space, etc. Because of this, it’s generally considered “non-deterministic” on any real system when examined.

For something like a basic calculator program, there’s almost no hardware involved. The result returned for 2+2 is pretty deterministic— Though technically not always. Still, it is considered to be “deterministic” for most intents and purposes.

3

u/[deleted] Apr 12 '21

The OS manages the hardware resources. The cpu schedule is influence by many non-deterministic processes that are the result of this hardware. A few examples are response time over the network, user input, moving parts (spinning discs, fans, voltages), software influenced conditions such as memory filling up, and much more.

2

u/pencan Apr 12 '21

Non-determinism comes from several sources. Mostly the I/O, including networking, DRAM timings, devices like hard drives. These timings depend on physical detail or traffic patterns outside of your control. Caching algorithms will also change timing of memory accesses effectively unpredictably.

However, you can get rid of a lot of these elements for certain workloads. Real-time operating systems are able to bound the execution period of a workload by bypassing caches, using fixed latency memories, pinning memory, polling I/O rather than interrupting, etc. But for a general purpose O/S executing arbitrary, non-trivial code it is non-deterministic in what order the threads will execute.

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.

1

u/josh2751 Apr 13 '21

Cpu scheduling is absolutely deterministic. You just don’t know all the variables that affect it.

11

u/[deleted] Apr 12 '21

[removed] — view removed comment

1

u/S-S-R Apr 15 '21

Determinsitic and probabilistic are terms that are widely used with different definitions than the actual mathematical definition. Business and economics especially have a problem with borrowing terms and completely misusing them.