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):
- Get the CAP_SYS_NICE privilege from root
- 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
, itemnice
(this config is used bypam_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:
- Fork second process to do the low-priority work and renice only it. Use inter-process communication to send work items and get results.
- 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
Can a Program Read Its Own Elf Section
Can Linux Flock(Fd, Lock_Ex|Lock_Nb) Fail Spuriously
Linux Patch Ignoring Line Numbers
Force Netcat to Send Messages Immediately (Without Buffering)
Firefox Not Closing After Selenium Tests Are Run
Problems Building Libcurl 7.21.2 on Ubuntu 11.10 (Hiphop)
Kubernetes Pods Terminated - Exit Code 137
Autoconf Check for Program and Fail If Not Found
Efficiently Read The Average Color of The Screen Content Rendered by Xbmc
Cannot Run 32-Bit Apps on 64-Bit Linux
How Does Docker Share Resources
Default Font Size of Javafx Under Linux Is Larger as on Windows
Match Specific Column with Grep Command
How to Build for Linux 32-Bit with Go1.6.2
Svn Checkout Fails with "Chunk Delimiter Was Invalid" - What Can Be Done
How to Move Old File to Another Folder