http://www.itwire.com/content/view/16778/1141/
| Just what makes Linux tick |
|
| by David M
Williams |
| Thursday, 21 February 2008 |
|
Page 1 of 3
Last
time, we spoke about the Linux process scheduler and how it runs in the
shadows swapping processes in and out of a running state so everyone
gets a stab at the CPU. Today we’ll go over how the kernel keeps track
of time and just what it means to do something in a jiffy.
Just to recap, last
time we
saw that every process has a priority assigned to. The scheduler will
bump this priority up and down as time goes on and depending on how
responsive the process is.
If the task stops often because it is waiting for input or output (I/O)
– such as user input on a keyboard, or for a disk drive to send data –
it will have its priority increased over time. This is because the
kernel considers the application to be behaving nicely; each time it
stops while waiting for I/O it voluntarily yields control back to the
scheduler allowing another process to run for a while.
By contrast, if the process is very computationally intense – crunching
lots of numbers without much pause for I/O – then the kernel will be
forced to pre-empt the process periodically to allow another task an
opportunity to run. The priority of pre-empted processes will be
dropped down as time goes on.
This all helps maintain a responsive system. The program interacting
with the user gets more chance to run and this ensures the user has a
positive experience with it.
All this said, you might want your computational process to finish
quickly because you really need the results. In this case, if you are
the superuser, you can also influence its priority by giving it a
negative nice value. This nice value is subtracted from the base
priority assigned by the scheduler, so a negative value increases the
priority because subtracting a negative number causes addition. Regular
non-privileged users may only set a positive nice value, which lowers
the priority of their jobs – hence the keyword “nice,” you’re doing
others a favour.
Linux also caters for real-time tasks which must respond immediately
upon receipt of an event. To support this Linux offers a range of
priorities which are above that normally allowed. These are not
adjusted over time and remain fixed for the life of that process.
So, how does the scheduler know when it is time to pre-empt a process?
The answer is that the kernel keeps track of how long every single
process has been running. In fact, it stores three time values. The
first is the real time, as measured by humans like you and I. The next
two will be much lower values, and are the amount of time that has
actually been spent executing the process, ie how much time the CPU has
spent on it. The first of these two is the user time count and the
second is the system time count, or sys. This sys value measures the
execution time of system calls.
Let’s illustrate. Please read on.
|
|