Process Priority: Difference between revisions

From COMP15212 Wiki
pc>Yuron
No edit summary
 
gravatar W81054ch [userbureaucratinterface-adminsysopPHRhYmxlIGNsYXNzPSJ0d3BvcHVwIj48dHI+PHRkIGNsYXNzPSJ0d3BvcHVwLWVudHJ5dGl0bGUiPkdyb3Vwczo8L3RkPjx0ZD51c2VyPGJyIC8+YnVyZWF1Y3JhdDxiciAvPmludGVyZmFjZS1hZG1pbjxiciAvPnN5c29wPGJyIC8+PC90ZD48L3RyPjwvdGFibGU+] (talk | contribs)
m (1 revision imported)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{#set: Priority=3 | Summary=Sometimes, some processes may be more important or urgent than others.  Priority schemes can provide a greater share of resources (particularly processor time) for selected tasks.  This can be particularly important in real-time systems.}}<!--
{{#set: Priority=3 | Summary=Sometimes, some processes may be more important or urgent than others.  Priority schemes can provide a greater share of resources (particularly processor time) for selected tasks.  This can be particularly important in real-time systems.}}<!--
-->{{Path|Processes|11}}<!--
-->{{Path|Processes|10}}<!--
-->{{#invoke:Dependencies|add|Scheduler,4|Processes,3}}
-->{{#invoke:Dependencies|add|Scheduler,4|Processes,3}}
Periodically, the [[scheduler]] has to select the next
Periodically, the [[scheduler]] has to select the next process to run.  Time-slicing with a simple round-robin algorithm
process to run.  Time-slicing with a simple round-robin algorithm
would give an equal share of processor time to all processes which are not blocked.  This can be <em>too</em> simplistic to be useful.  It is often useful to be able to assign <strong>priority</strong> to processes so that some are favoured over others.
would give an equal share of processor time to all processes which are
not blocked.  This can be <em>too</em> simplistic to be useful.  It is often
useful to be able to assign <strong>priority</strong> to processes so that some are
favoured over others.


An example could be a <em>background</em> job, which is processor intensive
An example could be a <em>background</em> job, which is processor intensive but which the user feels has no great urgency (e.g. mining Bitcoins); this might have a lower priority than (e.g.) a job playing videos which has a strict timing requirement, so that it leaves adequate resource for that whilst occupying any gaps in between.
but which the user feels has no great urgency (e.g. mining Bitcoins); this might have a lower priority than (e.g.) a job playing videos which has a strict timing requirement, so that it leaves adequate resource for that whilst occupying any gaps in between.


A process’ priority may be set by the user (or the system) in various ways.  It may be constant or calculated dynamically.  For example, a process which <strong>blocks</strong> rather than being time-sliced may have its priority temporarily increased when it can run again; this allows <strong>I/O bound</strong> processes to run to the next ‘blockage’ quickly and thus make progress without impeding other processes very much.
A process’ priority may be set by the user (or the system) in various ways.  It may be constant or calculated dynamically.  For example, a process which <strong>blocks</strong> rather than being time-sliced may have its priority temporarily increased when it can run again; this allows <strong>I/O bound</strong> processes to run to the next ‘blockage’ quickly and thus make progress without impeding other processes very much.


Priority can be <em>applied</em> in different ways too.  In a [[Real_Time|real-time
Priority can be <em>applied</em> in different ways too.  In a [[Real_Time|real-time system]] it probably dictates that the highest priority ‘ready’ process runs whilst it can.  (Equal priority processes may or may not be time-sliced.)  In an interactive system, this approach could prevent lower priority processes proceeding at all, so the strategy may be different: for example the priority could determine how often the process receives a time-slice or it could vary  the <em>length</em> of the time-slice.
system]] it probably dictates that the highest priority ‘ready’ process runs whilst it can.  (Equal priority processes may or may not be time-sliced.)  In an interactive system, this approach could prevent lower priority processes proceeding at all, so the strategy may be different: for example the priority could determine how often the process receives a time-slice or it could vary  the <em>length</em> of the time-slice.


[[Image:priority_timeslicing.png|link=|alt=Priority time-slicing]]
[[Image:priority_timeslicing.png|link=|alt=Priority time-slicing]]
<blockquote>
<blockquote>
<strong>Practical</strong>: try running the Unix utility <code>top</code>.  This displays the processes which have been occupying the processor recently,
<strong>Practical</strong>: try running the Unix utility <code>top</code>.  This displays the processes which have been occupying the processor recently, together with each process’ priority (maybe labelled “PR”).  Confusingly, lower numbers indicate higher priority. You may also spot a <code>nice</code> value (“NI”). “nice” is a user-set value which is used to alter (usually reduce) the priority of that process, causing it to demand less system time.  In Unix it varies from -20 (very ‘nasty’) to 19 (very nice); an ordinary ‘user’ cannot go below 0; negative values are for the system/‘root’. The utility “nice” on its own simply echoes the current (inherited) <code>nice</code> (0 by default); with a parameter it increases the ‘niceness’.  If you have a couple of Unix ‘shell’ windows, run <code>top</code> in one and try some experiments in the other. (An infinite loop program might help this demonstration: <code>forever.c</code> would do – it can be downloaded [[Media:forever.c|here]].) Prefixing a command line with “nice” will set the appropriate value.
together with each process’ priority (maybe labelled “PR”).  Confusingly, lower numbers indicate higher priority.
You may also spot a <code>nice</code> value (“NI”).
“nice” is a user-set value which is used to alter (usually reduce) the priority of that process, causing it to demand less system time.  In Unix it varies from -20 (very ‘nasty’) to 19 (very nice); an ordinary ‘user’ cannot go below 0; negative values are for the system/‘root’.
The utility “nice” on its own simply echoes the current
(inherited) <code>nice</code> (0 by default); with a parameter it increases the
‘niceness’.  If you have a couple of Unix
‘shell’ windows, run <code>top</code> in one and try some
experiments in the other. (An infinite loop program might help this
demonstration: <code>forever.c</code> would do – it can be downloaded [[Media:forever.c|here]].)
Prefixing a command line with “nice” will set the
appropriate value.
 
 
It may be easier to use two shells for this: one to play in and one
It may be easier to use two shells for this: one to play in and one
Line 40: Line 23:
</blockquote>
</blockquote>
<blockquote>
<blockquote>
If you run several processes at different ‘nice’
If you run several processes at different ‘nice’ settings you should see the CPU allocation divide appropriately. On a single processor machine this is quickly obvious.  As you are <em>probably</em> using a multicore processor you will need to start
settings you should see the CPU allocation divide appropriately.
several processes (more than you have ‘cores’) before anything is apparent because, at first, there will be a ‘spare’ core for each new job.
On a single processor machine this is quickly obvious.  As you are
<em>probably</em> using a multicore processor you will need to start
several processes (more than you have ‘cores’) before
anything is apparent because, at first, there will be a
‘spare’ core for each new job.
 
 
Reminders:
Reminders:
Line 55: Line 33:
* you can kill a <em>process</em> with <code>k</code> within <code>top</code>.
* you can kill a <em>process</em> with <code>k</code> within <code>top</code>.
</blockquote>
</blockquote>
In some scheduling algorithms – particularly in <em>interactive systems</em>
In some scheduling algorithms – particularly in <em>interactive systems</em> – the effective priority may be dynamic.  For instance, ‘[https://en.wikipedia.org/wiki/Aging_(scheduling) ageing]’ may raise the effective priority if a process has not been run for a while, eventually guaranteeing it some sort of share.
– the effective priority may be dynamic.  For instance,
‘[https://en.wikipedia.org/wiki/Aging_(scheduling) ageing]’
may raise the effective priority if a process has not been run for a
while, eventually guaranteeing it some sort of share.


==== Priority inversion ====
==== Priority inversion ====
This is a rather obscure topic, which is included here because it
This is a rather obscure topic, which is included here because it tends to get mentioned a lot.  It is particularly applicable to real-time systems.
tends to get mentioned a lot.  It is particularly applicable to
real-time systems.


A “priority inversion” is a circumstance where a high
A “priority inversion” is a circumstance where a high priority task is prevented from running by a lower priority task.  The
priority task is prevented from running by a lower priority task.  The
scenario:
scenario:


Line 74: Line 45:
# Before the low priority task can release the mutex a medium priority task occupies the processor.
# Before the low priority task can release the mutex a medium priority task occupies the processor.


The high priority task is suspended until the low priority task frees
The high priority task is suspended until the low priority task frees the mutex.  Normally this would happen quickly but the low priority task isn’t allowed the chance.  The medium priority task – which would normally be pre-empted – hogs the urgently needed resource.
the mutex.  Normally this would happen quickly but the low priority
task isn’t allowed the chance.  The medium priority task – which would
normally be pre-empted – hogs the urgently needed resource.


[[Image:priority_inversion.png|link=|alt=Priority Inversion]]
[[Image:priority_inversion.png|link=|alt=Priority Inversion]]
Line 83: Line 51:
For a famous example of this problem, look up the “[https://www.rapitasystems.com/blog/what-really-happened-to-the-software-on-the-mars-pathfinder-spacecraft Mars Pathfinder]” (1997).
For a famous example of this problem, look up the “[https://www.rapitasystems.com/blog/what-really-happened-to-the-software-on-the-mars-pathfinder-spacecraft Mars Pathfinder]” (1997).


Solutions exist, such as <strong>priority inheritance</strong>, where the priority of
Solutions exist, such as <strong>priority inheritance</strong>, where the priority of the process holding a mutex is temporarily boosted to the priority of anything waiting for that mutex, until it exits the critical section.
the process holding a mutex is temporarily boosted to the priority of
anything waiting for that mutex, until it exits the critical section.


Plenty more discussion and other solutions are ‘out there’…
Plenty more discussion and other solutions are ‘out there’…
----
----
 
{{BookChapter|5.3.4, 5.6.2, 5.7.2, 6.8.2|211-214, 229-230, 239-242, 284}}
{{PageGraph}}
{{PageGraph}}
{{Category|Processes}}
{{Category|Processes}}

Latest revision as of 10:03, 5 August 2019

On path: Processes 1: Processes • 2: Context • 3: Process Control Block (PCB) • 4: Multi Threading • 5: Threads • 6: Interprocess Communication • 7: Process Scheduling • 8: Scheduler • 9: Process States • 10: Process Priority
Depends on SchedulerProcesses

Periodically, the scheduler has to select the next process to run. Time-slicing with a simple round-robin algorithm would give an equal share of processor time to all processes which are not blocked. This can be too simplistic to be useful. It is often useful to be able to assign priority to processes so that some are favoured over others.

An example could be a background job, which is processor intensive but which the user feels has no great urgency (e.g. mining Bitcoins); this might have a lower priority than (e.g.) a job playing videos which has a strict timing requirement, so that it leaves adequate resource for that whilst occupying any gaps in between.

A process’ priority may be set by the user (or the system) in various ways. It may be constant or calculated dynamically. For example, a process which blocks rather than being time-sliced may have its priority temporarily increased when it can run again; this allows I/O bound processes to run to the next ‘blockage’ quickly and thus make progress without impeding other processes very much.

Priority can be applied in different ways too. In a real-time system it probably dictates that the highest priority ‘ready’ process runs whilst it can. (Equal priority processes may or may not be time-sliced.) In an interactive system, this approach could prevent lower priority processes proceeding at all, so the strategy may be different: for example the priority could determine how often the process receives a time-slice or it could vary the length of the time-slice.

Priority time-slicing

Practical: try running the Unix utility top. This displays the processes which have been occupying the processor recently, together with each process’ priority (maybe labelled “PR”). Confusingly, lower numbers indicate higher priority. You may also spot a nice value (“NI”). “nice” is a user-set value which is used to alter (usually reduce) the priority of that process, causing it to demand less system time. In Unix it varies from -20 (very ‘nasty’) to 19 (very nice); an ordinary ‘user’ cannot go below 0; negative values are for the system/‘root’. The utility “nice” on its own simply echoes the current (inherited) nice (0 by default); with a parameter it increases the ‘niceness’. If you have a couple of Unix ‘shell’ windows, run top in one and try some experiments in the other. (An infinite loop program might help this demonstration: forever.c would do – it can be downloaded here.) Prefixing a command line with “nice” will set the appropriate value.   It may be easier to use two shells for this: one to play in and one to watch the results.

  • nice -4 escape should show the application running in top.
    This probably won’t have much actual effect unless your computer is already busy.
    ^C might be useful now.
  • nice -2 bash will start a daughter shell
    nice should now echo “2”
    nice -4 escape should show the application running at niceness 6
    ^C
    exit will return you to your original shell

If you run several processes at different ‘nice’ settings you should see the CPU allocation divide appropriately. On a single processor machine this is quickly obvious. As you are probably using a multicore processor you will need to start several processes (more than you have ‘cores’) before anything is apparent because, at first, there will be a ‘spare’ core for each new job.   Reminders:

  • you can stop the shell waiting for a job with ‘&’

   e.g. nice -3 escape&

  • you can find detached jobs in a shell with jobs
  • you can kill a job with kill %<n> (where ‘<n>’ is the job number.)
  • you can kill a process with k within top.

In some scheduling algorithms – particularly in interactive systems – the effective priority may be dynamic. For instance, ‘ageing’ may raise the effective priority if a process has not been run for a while, eventually guaranteeing it some sort of share.

Priority inversion

This is a rather obscure topic, which is included here because it tends to get mentioned a lot. It is particularly applicable to real-time systems.

A “priority inversion” is a circumstance where a high priority task is prevented from running by a lower priority task. The scenario:

  1. A low priority task acquires a mutex.
     
  2. A high priority task pre-empts, tries for the same mutex and blocks.
     
  3. Before the low priority task can release the mutex a medium priority task occupies the processor.

The high priority task is suspended until the low priority task frees the mutex. Normally this would happen quickly but the low priority task isn’t allowed the chance. The medium priority task – which would normally be pre-empted – hogs the urgently needed resource.

Priority Inversion

For a famous example of this problem, look up the “Mars Pathfinder” (1997).

Solutions exist, such as priority inheritance, where the priority of the process holding a mutex is temporarily boosted to the priority of anything waiting for that mutex, until it exits the critical section.

Plenty more discussion and other solutions are ‘out there’…


Also refer to: Operating System Concepts, 10th Edition: Chapter 5.3.4, 5.6.2, 5.7.2, 6.8.2, pages 211-214, 229-230, 239-242, 284


Articles on Processes
About this resource • Atomicity • Containers • Context • Context Switching • Daemons • Fork Unix • Hypervisor • Idle • Interprocess Communication • Multi Threading • Mutual exclusion • Pipes • Pointer Arithmetic • Process Control Block (PCB) • Process Priority • Process Scheduling • Process States • Processes • Queues • Queues Extra • Race Conditions • Real Time • Resources • Scheduler • Signal and Wait • Sleep • Starvation • Synchronisation • Thrashing • Threads • Unix Signals