Search code examples
cprocessglibcnice

How to proper set a limit to process's priority with libc?


I'm using setrlimit to put a limit to the niceness of my program when it runs like a proccess, but it's not working.

int main()
{
   struct rlimit limit;

   getrlimit(RLIMIT_NICE, &limit); 

   limit.rlim_max = 30;
   limit.rlim_cur = 15;

   setrlimit(RLIMIT_NICE, &limit)

    while(1)
        sleep(1);
    return 0;
}

When the proccess is running, non-root account is unable to set priority minor than 0 and root can exceed those limits. Am I missing something?


Solution

  • if you're on linux, please check if

    ulimit -e
    

    returns 0. This is set on my machine:

    $ ulimit -e
    ...
    scheduling priority             (-e) 0
    

    this means, non-root users are only allowed to decrease nice value by this limit. you should be able to modify this limit via /etc/security/limits.conf if you have root access.

    here are some more details: http://www.linuxjournal.com/article/3910