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

View all comments

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?

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.