Linux – way to really prioritize processes or make Linux respect their priorities

arch linuxlinuxpriorityprocess

I know there are process priorities like in other operating systems, going from -20 (most prio) to 19 (less prio) but Linux seems to ignore them.

Right now I was building the kernel in the background (although make processes have priority 0) and since it took quite some time I decided to watch something. So I opened a pretty demanding H264 video (~30% of CPU time of a Core2 2.6GHz) in VLC only to find out there was tearing, frames lost, visual artifacts (resulting from the previous I presume), although audio seemed to be fine.

So I decided to change the priority of VLC using renice, concretely seeing that PulseAudio had -11 I decided to put it on par so I did sudo renice -11 -p VLC_PROC_#.

Same thing kept happening, so I went on and set it to -20 but I still kept seeing visual artifacts.

So I wonder, why Linux didn't actually prioritize a -20 process over some 0 processes and give it anything it needed? Is there any way to really prioritize processes in Linux?

In case it matters, I'm running a 64-bit Arch here, XFCE as desktop environment.

EDIT: The kernel compilation was performed in /tmp which I have as tmpfs so its sources and all were already in RAM. RAM usage didn't even reach 60% and there was no paging operations in place.

The scenario detailed above is just a test case, I'm more interested in why Linux performed how it did and if there's any way to get real priorities.

Best Answer

renice does affect the priority of a process. But as you've experienced, just because a process has higher priority doesn't imply that it will have all the resources it needs. A higher priority merely gives the process a bigger chance to grab resources.

renice only affects CPU time. So it only has an effect if two or more processes are competing for CPU time. If the limiting factor is not CPU time but I/O bandwidth, the nice value has no impact. Maybe in your case the compilation is using a lot of disk bandwidth and vlc isn't able to read the data fast enough from the disk. Try ionice instead or in addition to nice.

If you do this often, you'll get better results if the video and the compilation are on separate disks. Also, you may get better results if you preload the video into the disk cache (cat /path/to/video.file >/dev/null, or tail -c +456m | head -c 123m /path/to/video.file >/dev/null to read 123MB starting at offset 456MB) — but unless you have a lot of RAM the compilation is likely to claim the cache space back. If you want to be sure to have the video in memory, make a ramdisk and copy the video to it.

Related Question