What is the priority of background processes in linux environment
All processes running at the same nice value will get an equal cpu-timeslice.
Here is a simple test that launches 2 processes, both performing the exact same operations. One is launched in the background and the other in the foreground.
dd if=/dev/zero of=/dev/null bs=1 &dd if=/dev/zero of=/dev/null bs=1
The relevant extract from subsequently running the top
command
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1366 root 20 0 1576 532 436 R 100 0.0 0:30.79 dd 1365 root 20 0 1576 532 436 R 100 0.0 0:30.79 dd
Next, if both the processes are restricted to the same CPU,
taskset -c 0 dd if=/dev/zero of=/dev/null bs=1 &taskset -c 0 dd if=/dev/zero of=/dev/null bs=1
Again the relevant extract from subsequently running the top
command shows
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1357 root 20 0 1576 532 436 R 50 0.0 0:38.74 dd 1358 root 20 0 1576 532 436 R 50 0.0 0:38.74 dd
both the processes compete for CPU-timeslice and are equally prioritised.
Finally,
kill -SIGINT 1357 &kill -SIGINT 1358 &kill -SIGINT 1365 &kill -SIGINT 1366 &
results in similar amounts of data copied and throughput.
25129255+0 records in25129255+0 records out25129255 bytes (25 MB) copied, 34.883 s, 720 kB/s
Slight discrepancies in output may occur in the throughput due to differences in the exact moment the individual processes respond to the break-signal and stop running.
However also note that
sched_autogroup_enabled
exists.
if enabled, sched_autogroup_enabled
ensures that the fairness in distributing cpu-timeslice is now performed between individual shells. By distributing cpu equally amongst the various active shells.
Thus if a shell launches 1 process A,
and another shell launches 2 processes B and C,
then the CPU execution timeslice will typically be distributed as
A <-- 50% <---- shell1 50% B <-- 25% <-. C <-- 25% <--`- shell2 50%
(though all 3 processes A, B & C are running at the same nice level.)
The process priorities in Linux kernel is given by NICE values.Refer to the link http://en.wikipedia.org/wiki/Nice_(Unix)
The nice values (ranging between -20 to +19) define the process priorities, -20 being the highest priority task. Usually the user-space processes are given default nice values of '0'. You can check the nice values for the running processes on your shell using the below command.
ps -alF S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD0 S 1039 1268 16889 0 80 0 - 11656 poll_s pts/8 00:00:08 vim0 S 1047 1566 17683 0 80 0 - 2027 wait pts/18 00:00:00 arm-linux-andro0 R 1047 1567 1566 21 80 0 - 9143 ? pts/18 00:00:00 cc10 R 1031 1570 15865 0 80 0 - 2176 - pts/24 00:00:00 ps0 R 1031 17357 15865 99 80 0 - 2597 - pts/24 00:03:29 top
So from above output if you see the 'NI' column shows your nice values. When i tried running a background process, that too got a nice value of '0' (top is that process with PID 17357). That would mean, it will also be queued up for like a foreground process and will be scheduled likewise.