Advanced Programming: Process priorities and scheduling

Process priorities (Nice values)

Under this term there is model called round Robbin time-sharing in which the process is permitted to use the CPU for a brief period of time. This time is defined as quantum. The two important requirements that the Round Robin model satisfies:

  • Fairness: every process gets its time with the CPU
  • Responsiveness: The waiting time is not that long

The algorithm allows to every process to use the CPU but when its time ends voluntarily gives up the CPU by putting itself to sleep. Every process has an attribute which indicates the priority. The smallest the number the highest the priority. This is called the nice value.

In the case a fork is executed and a child process is created the nice value will be determined by the maximum existent priority value (which is the lowest level priority), minus the nice value of its father.

The changes between processes regarding their nice values are sometimes changed between them, this can be done by the system calls setpriority() and getpriority(), it takes the arguments which and who. This stands for identifiers of the processes whose priority is to be retrieved. One process cant change the priority of another as long has the privileges. Meanwhile the unprivileged one do not have the permission to change another but to change itself. As long as it is inside of the formula parameters which is: 20 – (father priority), where the range goes from +19 (lowest) to -20 (highest).

Real-time Process Scheduling

This real time process algorithm allows to adequate performance between processes that run on the system. External inputs may interfere with so the real time application must provide some space of time left. This being quite small or almost a fraction of a second. Satisfied thanks to the high priority process to obtain time of the CPU.

Every turn that the process takes on use fro the CPU must me total, meaning that have to have exclusive access to the CPU until the task or time (turn) is done. Being able to process in the precise order that the process are scheduled.

The susv3, a real time process scheduling API. This API allows to work on a scheme of round robin policies. This policies work under the scheme of highest priority which has precedence over the lower priority. This scheme is defined by the priority level which in Linux is determined from 1 (lowest) to 99 (highest). In the event where two process priority levels are equal between a set of process, the order of execution is determined by the queue. This means that are executed on the order in which they came first.

Real time applications are hard to imagine, at least on Linux. Due to the round robin turn times assign to every process, that is why is hard to implemented without an overhead requirement over the system.

The shed_RR policy determines whether a process can continue using the CPU slice over different situations like an error, segmentation fault or reaches the end of its slice, and even with those events the process can end if before these previous events terminates or its preempted by a higher priority process.

The Shed_FIFO policy, very similar to the shed_RR policy but there is no time slice. Voluntarily gave up the CPU or terminates, preempted by higher priority process.

The shed_IDLE and the shed_BATCH are nonstandard but the shed_BATCH are for jobs that are no frequently often. Meanwhile the shed_IDLE provides functionality for low lever priority process Intended for low priority jobs, only if no other job requires de the CPU.

Real Time Process Scheduling API

The API that allows to run multi tasking process have various system calls. In which control process policies and priorities.

The sched_get_priority_min() and sched_get_priority_max() returns the priority range for a scheduling policy. Both retrieves the information by specifying the scheduling policy.

The sched_setscheduler() method changes both important parameters: scheduling policy and the priority of the process, if the pid is specified as 0 the attributes of the calling process are changed. The policy argument is very important because determines the scheduling policy of the process. So far we have the following policies:

  • SCHED_FIFO: Real-time first in, first out.
  • SCHED_RR: Real-time round robin.
  • SCHED_OTHER: Standard round robin time sharing.
  • SCHED_BATCH: Like SHED_OTHER but with batch execution.
  • SCHED_IDLE: for low priority jobs as long as no process uses the CPU, priority even lower than nice value +19.

CPU Affinity

Most of the Linux environments works on a multiprocessor CPU, this means that is unlikely that a process run on the same CPU all the time, that is why you need a recollection and spacing or in other words move you data into the cache (this is the space work), first al the cache’s data must be invalidated. This means that the memory will be either flushed or relocated into the main memory. Only if it is modified. This process of invalidation cost time. This may impact on the Linux performance. That is why tries to ensure soft CPU affinity for a process. The cache memory is very important, this is the space work where all the changes and operations are being held, the ranges are from 32 to 128 bytes.

The CPU affinity tries for raise up the performance and the impacts that invalidation may cause. Or that multiple process can share the same CPU without missed data on the cache. And even save a CPU just for critical applications. This measure is implemented by isolcpus, this is the kernel boot option used to isolate one CPU from the scheduling algorithms.

The nonstandard system calls used for modify and retrieve the hard CPU affinity of a process are called:

  • sched_setaffinity(pid, len, *set)
    • Sets the CPU affinity for the process, if pid is 0 the affinity is changed.
    • For change the affinity in a multi thread process we simply change the CPU affinity in a specific thread.
    • The len parameter should specify the number of bytes in the set argument.
    • In the case that none of the CPU’s not correspond that the one indicated fails with the error EINVAL.
  • sched_getaffinity(pid , len , *set)
    • Returns the cpu_set_t structure pointed by set, should be able to indicate the number of bytes in this structure
    • The set is a type cpu_set_t pointer.
    • No need of permission to retrieve any affinity on the system.

A child process can be created from the fork() and its CPU affinity mask is preserved trough the execution. This previous system calls are Linux-specific.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at

Up ↑

Create your website at
Get started
%d bloggers like this: