Re: How does preemptive scheduling work
Posted: Mon Jun 19, 2023 2:12 am
A couple things here:devc1 wrote:Well, but if I now have one thread running an infinite loop, the thread is tecnically always ready so the cpu will be used at 100% which will increase its temp.
1) It is a well known result in computer science that it is mathematically impossible to write an algorithm that will accurately determine, in every possible case, whether a program that takes no user input --- except the data that is given to it when it starts --- will run forever in an infinite loop or whether it will eventually stop. You can determine write algorithms that will work for certain programs, but there are no algorithms that will work for *all* programs.
2) When you consider that a program might receive unpredictable user input, and might decide to continue running or stop depending on that, it is even less possible to determine whether a program will run forever or stop. All the OS knows is whether a program is currently able to proceed or whether it's waiting for something to happen.
3) Not every program that runs in an infinite loop uses 100% CPU. Many programs (including the vast majority of GUI apps), will behave something like this:
Code: Select all
while(true) //loop forever
{
waitForEvent(); //wait for something to happen
handleEvent(); //do something
if (exitRequested) //if the user clicks the "x" button
{
exit(); //close the program
}
}
4) Not every program that is guaranteed to finish after a known amount of time will finish in a reasonable amount of time, and such programs may use 100% CPU until they complete. A program that solves the Tower of Hanoi puzzle without user input takes twice as long for every disk added to the puzzle. With a relatively small number of disks (a bit over 100), even the fastest CPU cores today will never manage to solve the puzzle before every star in the universe burns out, and that's assuming that each iteration can be completed in only one instruction! (Which is unlikely). And the core that's stuck running the puzzle will be pegged the whole time.
In light of the above points, whether a program is performing useful work is something the user needs to determine, and whether a user is abusing the system (by using it to run a 256-disk Tower of Hanoi puzzle, for example) is something for the administrator to determine.
And if it's a personal system and the user is the administrator and decides to run a 256-disk Tower of Hanoi puzzle on each core of their laptop while it's on battery, that's not a scheduler problem, that's a PEBKAC, and the solution to that problem is "you can't fix stupid".