4

I understand that, for scheduling purposes, Linux processes have a "nice" value and a real-time priority value and that these can be explicitly altered with the nice and chrt commands. If the user does not explicitly set the real-time priority of a process, how is it set?

5

To quote Robert Love:

The scheduler does not magically know whether a process is interactive. It requires some heuristic that is capable of accurately reflecting whether a task is I/O-bound or processor-bound. The most indicative metric is how long the task sleeps. If a task spends most of its time asleep it is I/O-bound. If a task spends more time runnable than sleeping, it is not interactive. This extends to the extreme; a task that spends nearly all the time sleeping is completely I/O-bound, whereas a task that spends nearly all its time runnable is completely processor-bound.

To implement this heuristic, Linux keeps a running tab on how much time a process is spent sleeping versus how much time the process spends in a runnable state. This value is stored in the sleep_avg member of the task_struct. It ranges from zero to MAX_SLEEP_AVG, which defaults to 10 milliseconds. When a task becomes runnable after sleeping, sleep_avg is incremented by how long it slept, until the value reaches MAX_SLEEP_AVG. For every timer tick the task runs, sleep_avg is decremented until it reaches zero.

So, I believe the kernel decides the scheduling policy based on the above heuristics. As far as I know, for real time processes, the scheduling policy could either be SCHED_FIFO or SCHED_RR. Both the policies are similar except that SCHED_RR has a time slice while SCHED_FIFO doesn't have any time slice.

However, we can even change the real time process scheduling as well. You could refer to this question on how to change the real time process scheduling.

References

http://www.informit.com/articles/article.aspx?p=101760&seqNum=2

1

As a component of the kernel, the process scheduler, makes the decisions to divide up processor time between processes on a given system using a priority-based scheduling algorithm. Starting with an initial base or default priority of zero (I believe) the scheduler can dynamically increase or decrease priority to fulfill scheduling objectives based on whether the process is I/O or CPU bound. An I/O process spending time waiting on I/O operations priority will be dynamically increased whereas CPU bound process that is continually eating up its time-slice (how long a task can run until it is preempted) will be dynamically lowered.

Depending on the type of application (interactive e.g. browser / ide etc.) the scheduler uses policies to set the priority and will set accordingly. Interactive get higher priority as the end user expects response from these etc..

  • Yes, but how does the scheduler decide into which category a process falls? For example, does it determine that a process is interactive if it frequently waits on keyboard input? What if it gets its data from the disk or network? That process would be I/O bound but not interactive. – Ellen Spertus Sep 21 '14 at 19:17
  • Actually, an IDE would be mostly I/O bound but because it needs to respond to user input it will get the higher priority - so yes. With regards to an I/O bound, non interactive process the time-slice allocated will be obviously smaller and IDE will be allowed to preempt such a process. – cherrysoft Sep 21 '14 at 19:33
  • I wasn't referring to an IDE. I was thinking instead of something like a web crawler. It is I/O bound but not interactive. – Ellen Spertus Sep 21 '14 at 22:26
  • An example of something waiting on keyboard input would be an IDE, hence my response. A batch process will often be penalised by the scheduler as they do not require human interaction. Does this fully answer your question? – cherrysoft Sep 22 '14 at 0:32
  • It doesn't fully answer my question, but the other answer does well enough. (Neither acknowledges that not all I/O bound processes are interactive.) Thank you anyway for your help. – Ellen Spertus Sep 22 '14 at 0:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.