Automatically Adjusting Process Priorities Under Linux

Automatically adjusting process priorities under Linux

If you've settled for a polling solution, most of the features you want to implement already exist in the Automatic Nice Daemon. You can configure nice levels for processes based on process name, user and group. It's even possible to adjust process priorities dynamically based on how much CPU time it has used so far.

Niced process gives their priority to child process

Its not possible to give normal priority to child process of niced process but we can assign normal priority in code before parent forks child this way we can assign normal priority to child process.

Refer setpriority for setting priority for any process from code

changing real time process priority in Linux ..?

Now I got it. Real time tasks(FF/RR) are not governed by penalty/bonus rules. With O(1) scheduler, task with higher priority will be chosen. In my case process B will be scheduled if its priority is greater than process A.

Penalty/bonus is for SCHED_OTHER/SCHED_NORMAL.

Decrease self priority for a short period and then increase it back

Usually only root (or processes with CAP_SYS_NICE privilege) may change their "nice" to higher priority (lower values). But since 2.6.12 version of kernel (http://man7.org/conf/lca2006/Linux_2.6_changes/rlimit_5.html), linux introduced additional way to increasing priority (lowering nice), the RLIMIT_NICE rlimit (man getrlimit). You can check it with ulimit -e in bash, and change by root before switching (su) to usual user or to nobody (example).

So, what can you do to make your process temporary having low priority (higher nice value):

  1. Get the CAP_SYS_NICE privilege from root
  2. Set right RLIMIT_NICE (default is 0, so RLIMIT_NICE is disabled and this is the flaw in linux distributions), either by root ulimit -e VALUE or in system-wide /etc/security/limits.conf, item nice (this config is used by pam_limits.so PAM module, so check, is it called before you start the process. Usually it is called by login, *dm managers, crond and atd. Don't know is it called for processes started by init.d scripts)

Two examples of second variant from brauliobo, allow nice back to 0, system-wide. Add to /etc/security/limits.conf:

*              soft    nice    0 # ranges from -20 to 19

Or using sudo to root to change RLIMIT_NICE for single shell:

sudo bash
ulimit -e 20 # equivalent to 0, as it ranges from 0 to 40
sudo -u youruser bash
# now you can renice back to 0

Without help from root user you can:

  1. Fork second process to do the low-priority work and renice only it. Use inter-process communication to send work items and get results.
  2. You can call sched_yield often when doing low-priority work. This will enable other processes to preempt your program early.

Will the kernel change the nice value of a process?

The linux kernel does not automatically change the nice value - something else did it.

Inside the Linux kernel, does the `nice` priority level of a task get modified anywhere else beside these cases?

I discovered that the default settings of a process are initiated in /source/init/init_task.c

No. That's the definition of the init task itself, it's not used to init[ialize] new tasks. The init task is the first userspace task that is ran by the kernel upon startup, and its task_struct is hardcoded for performance and ease of use. Any other task is created through fork(2)/clone(2).

My reasoning is that if I were to modify these default settings in init_task.c, it should cover all cases

It doesn't cover anything really, just the initial priority of init, which can very well overwrite it itself since it runs as root.


The priority of a task can be changed in different ways, but the kernel should not "automatically" change priority of tasks, at least to my knowledge.

Therefore, if you want to "control" the priority in such a way, your best bet would be to change the code of fork (specifically clone_process()), since forking is the only way to create new processes in Linux.

Other than that, there are other syscalls that can end up modifying the priority of a process. Taking a quick glance at the kernel code, at least sched_setscheduler (source), sched_setparam (source), and setpriority (source).



Related Topics



Leave a reply



Submit