Finding idle CPU cycles to utilize them.
I was finding out a way to detect idle CPU cycles to process something, so that my process does not hog up the CPU. Additionally I was trying to do this on a Linux box. After a lot of searching(googling) here and there, I found a link on Google Groups that said using nice 19 to set a processes priority to the minimum would only schedule the process when the CPU is idle. Here is the link
But this was not something I was looking for. I wanted a way to do it through my program. And voila! I found an API call that sets priority of a process. Its call setpriority(). Its very simple to use. And guess what I even experimented with it to confirm it actually works...
What I did was wrote a generic program that takes the priority as an argument and starts the process with that priority. Needless to mention, this API fails to change priority if the user spawning the process is not root. I started three processes with priorities -20, 0 and +19 which translate to high, medium and low priorities respectively. I observed the behavior of the three processes using top command and what I observed was that the process that had highest priority hogged up CPU to the maximum extent and so on with the one with least priority with very less CPU utilisation. But there is still something that I keep thinking. Is it accurate? Because doesn't detecting that the CPU is idle require some CPU cycles by itself?