Why processes are deprived of CPU for TOO long while busy looping in Linux kernel?
The schedule()
function simply invokes the scheduler - it doesn't take any special measures to arrange that the calling thread will be replaced by a different one. If the current thread is still the highest priority one on the run queue then it will be selected by the scheduler once again.
It sounds as if your kernel thread is doing very little work in its busy loop and it's calling schedule()
every time round. Therefore, it's probably not using much CPU time itself and hence doesn't have its priority reduced much. Negative nice values carry heavier weight than positives, so the difference between a -5 and a 0 is quite pronounced. The combination of these two effects means I'm not too surprised that user space processes miss out.
As an experiment you could try calling the scheduler every Nth iteration of the loop (you'll have to experiment to find a good value of N for your platform) and see if the situation is better - calling schedule()
too often will just waste lots of CPU time in the scheduler. Of course, this is just an experiment - as you have already pointed out, avoiding busy loops is the correct option in production code, and if you want to be sure your thread is replaced by another then set it to be TASK_INTERRUPTIBLE
before calling schedule()
to remote itself from the run queue (as has already been mentioned in comments).
Note that your kernel (2.6.18) is using the O(1) scheduler which existed until the Completely Fair Scheduler was added in 2.6.23 (the O(1) scheduler having been added in 2.6 to replace the even older O(n) scheduler). The CFS doesn't use run queues and works in a different way, so you might well see different behaviour - I'm less familiar with it, however, so I wouldn't like to predict exactly what differences you'd see. I've seen enough of it to know that "completely fair" isn't the term I'd use on heavily loaded SMP systems with a large number of both cores and processes, but I also accept that writing a scheduler is a very tricky task and it's far from the worst I've seen, and I've never had a significant problem with it on a 4-8 core desktop machine.
Why is wall time(elapsed time ) less than cpu time for parallel programs?
wall time: <--------------> 16
CPU 1: ## #############
CPU 2: ##########
CPU time: 25
Related Topics
How to Execute Shell Builtin from Scala
Apache Httpd VS. Tomcat 7: Port 80 VS. Port 8080
Bash Script That Creates a Directory Structure
Make "Git Pull" Ask for Confirmation When Pulling Different Branch
Yocto Bitbake Script Not Displaying Echo Statement
Bash Variables: Case Sensitive or Not
Git Clone Fails: Server Certificate Verification Failed
Splitting Bulk Text File Every N Line
How to Determine Thread Local Storage Model Used by a Library on Linux
Using Netcat to Pipe Unix Socket to Tcp Socket
Getting Following Error After The Command Sudo Apt-Get Update on Ubuntu 16.04
Install Library in Home Directory
How to Send Esc/Pos Commands to Thermal Printer in Linux
How to Provide Extend-On-Write Functionality for Memory Mapped Files in Linux
How to Get The System Home Directory in Cmake on Linux
Command Line Arguments to Docker Run
What Would Be a Pratical Example of Sysroot and Prefix Options for Qt