Sign up ×

Is there a way to prioritise X applications with focus, preferably both in terms of CPU and I/O?

Something like all X applications are niced and ioniced down in priority, but when they gain focus they get niced and ioniced back up to normal levels.

I don't want to reinvent the wheel, so I was hoping someone had already developed an application that does this.

share|improve this question
This requires the window manager to know the process ID of the X11 client it handles. I think I've not yet seen any WM able to do this (unfortunately), not sure if it's possible. (It fails because the X11 client might actually not be a local process at all, right?) –  sr_ Feb 16 '12 at 17:26
@sr_ What process created this X11 window? –  Gilles Feb 16 '12 at 21:57
@Gilles: thank you :) –  sr_ Feb 17 '12 at 8:27

1 Answer 1

I wrote a small script in perl to do what you ask:

It works by getting all the current X11 window IDs using the "xlsclients -l" command, in turn getting the window's PID with xprop. It then uses xprop -root to get the currently focused window, then loops through all the PIDs to change their nice value to 5 (keeping the currently focused one at 0.)

It then enters an infinite loop that re-checks the focused window every two seconds. If the window focus has changed, it changes the old one's nice value to 5 and the new one to 0. Also, every 40 seconds it redoes what it did in the first paragraph so new processes can be prioritized.

This script has several flaws, the most obvious of which is the fact that xprop sometimes fails to find the currently focused window (this seems to happen only if you're switching between windows really fast.) Also, if you're running a window from a network location, the PID might not correlate to the actual PID on your system. That said, on average the script will work (perl can handle a few errors and still run)

I also think it would be faster if I wrote this in c, but I have no skill in using the X11 library, so bash-filled perl it is!

share|improve this answer

Your Answer


