Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 703
thelsdj Avatar asked Aug 27 '08 06:08

thelsdj


People also ask

How do I set priority in C++?

After (or before) SetPriorityClass, you must set the individual thread priority to achieve the maximum possible. Additionally, another security token is required for realtime priority class, so be sure to grab it (if accessible). SetThreadPriority is the secondary API after SetPriorityClass.


1 Answers

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.)

like image 67
Silas Snider Avatar answered Sep 25 '22 14:09

Silas Snider