Page 2 of 2

Re: How does preemptive scheduling work

Posted: Mon Jun 19, 2023 2:12 am
by linguofreak
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.
A couple things here:

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
     }
}
As long as the user doesn't click the "x" button, these programs will loop infinitely, but they won't use any CPU while waiting for something to happen.

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".

Re: How does preemptive scheduling work

Posted: Mon Jun 19, 2023 3:00 am
by devc1
I mean if I try to make a program with an infinite loop like this : while(1);

It will show me 20% cpu usage instead of 100% (total cpu usage), does that mean the idle thread is included with the ready threads ?

Re: How does preemptive scheduling work

Posted: Mon Jun 19, 2023 3:42 am
by linguofreak
devc1 wrote:I mean if I try to make a program with an infinite loop like this : while(1);

It will show me 20% cpu usage instead of 100% (total cpu usage), does that mean the idle thread is included with the ready threads ?
No, it just means you have multiple cores. while(1); , without anything in the loop that waits for an event, will just peg the core that runs it. If you run as many instances of the program as you have cores, it will use 100% CPU. But if you have something in that infinite loop that waits for something to happen, then you could run 100 instances of the program, and as long as they're all waiting at any given moment, they'll contribute zero CPU usage, even if they're all technically infinite looping.

Re: How does preemptive scheduling work

Posted: Mon Jun 19, 2023 5:08 am
by devc1
Yeah I thaught about that, I have 8 threads 4 cores so that explains it.