Why Do Processes Get CPU Time on a Linux OS when Their Nice Level is Set for Unfavorable Scheduling?

Problem scenario
Some processes are a low priority for an OS given their nice level. Why do they eventually run when the OS is active and busy handling processes that have higher priorities? Wouldn't a nice level that indicates a low priority keep the process starved of receiving resources?

There is a round robin scheduling algorithm for the CPU to allocate time. When such processes in the question are not starved, it is because there is no lock primitive blocking the process from the CPU.

Experienced Linux administrators know that processes will complete, even if they have a low priority nice value (one near or equal to 19).

According to the influential book The Linux Programming Interface by Michael Kerrisk (page 734), a low priority nice value will not starve a process of CPU. The kernel will govern the specifics and there is variance in the round robin processing among Linux distributions and their respective kernel (page 734).

Modern kernels have a scheduling algorithm that put a greater weight on the nice value (page 734 of the same book). The nice values are like golf: higher is worse in that a -20 nice value corresponds with the highest priority.

One way to remember how the scheduling algorithm of a Linux kernel works is this: imagine owning a timeshare based on your golf score. Better golfers get lower scores; lower nice values are more important and get more time with the CPU. CPU time is shared in a round robin way.

Resource starvation can happen when there is a lock (such as a mutex or a semaphore) blocking access to code. Blocking access to certain code is a mechanism of synchronization. The elements involved with the control of the flow of a program (the order of execution) are to prevent the entry into certain parts of code under certain conditions. But some applications, even tested ones, do not account for scenarios the software engineer(s) did not think could happen. Therefore sometimes a process is starved from resources. Thorough testing of logic can help a programmer design a solution where no resource starvation happens.

Leave a comment

Your email address will not be published. Required fields are marked *