Search code examples
cunixcross-platformprocess-management

Change priority of the current process in C


On Windows I can do:

HANDLE hCurrentProcess = GetCurrentProcess();

SetPriorityClass(hCurrentProcess, ABOVE_NORMAL_PRIORITY_CLASS);

How can I do the same thing on *nix?


Solution

  • Try:

    #include <sys/time.h>
    #include <sys/resource.h>
    
    int main(){
        setpriority(PRIO_PROCESS, 0, -20);
    }
    

    Note that you must be running as superuser for this to work.

    (for more info, type 'man setpriority' at a prompt.)