OSDev.org

The Place to Start for Operating System Developers
It is currently Mon Apr 29, 2024 4:31 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 19 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: How does preemptive scheduling work
PostPosted: Mon Jun 19, 2023 2:12 am 
Offline
Member
Member

Joined: Wed Mar 09, 2011 3:55 am
Posts: 509
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:
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".


Top
 Profile  
 
 Post subject: Re: How does preemptive scheduling work
PostPosted: Mon Jun 19, 2023 3:00 am 
Offline
Member
Member

Joined: Fri Feb 11, 2022 4:55 am
Posts: 435
Location: behind the keyboard
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 ?


Top
 Profile  
 
 Post subject: Re: How does preemptive scheduling work
PostPosted: Mon Jun 19, 2023 3:42 am 
Offline
Member
Member

Joined: Wed Mar 09, 2011 3:55 am
Posts: 509
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.


Top
 Profile  
 
 Post subject: Re: How does preemptive scheduling work
PostPosted: Mon Jun 19, 2023 5:08 am 
Offline
Member
Member

Joined: Fri Feb 11, 2022 4:55 am
Posts: 435
Location: behind the keyboard
Yeah I thaught about that, I have 8 threads 4 cores so that explains it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ]  Go to page Previous  1, 2

All times are UTC - 6 hours


Who is online

Users browsing this forum: FrankRay78, Google [Bot], SemrushBot [Bot] and 21 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group