I'm trying to change the nice value through a publicly available code but I'm getting a Permission Denied (EPERM) message.
The program fails when executed either as a normal user or with sudo.
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
int
main(void)
{
int pol;
struct sched_param sp;
struct rlimit rl;
pol = sched_getscheduler(getpid());
if (pol == -1) {
perror("sched_getscheduler");
exit(EXIT_FAILURE);
}
if (sched_getparam(0, &sp) == -1) {
perror("sched_getparam");
exit(EXIT_FAILURE);
}
printf("%ld: %-5s ", (long)getpid(),
(pol == SCHED_OTHER) ? "OTHER" :
(pol == SCHED_RR) ? "RR" :
(pol == SCHED_FIFO) ? "FIFO" :
#ifdef SCHED_BATCH /* Linux-specific */
(pol == SCHED_BATCH) ? "BATCH" :
#endif
#ifdef SCHED_IDLE /* Linux-specific */
(pol == SCHED_IDLE) ? "IDLE" :
#endif
"???");
printf("%2d\n", sp.sched_priority);
if (setpriority(PRIO_PROCESS, 0, -10) == -1) {
perror("setpriority");
exit(EXIT_FAILURE);
}
printf("New Nice value is %d\n", getpriority(PRIO_PROCESS, 0));
#ifdef _POSIX_PRIORITY_SCHEDULING
printf("Setting scheduler to real-time RR and current process priority to %d\n", sched_get_priority_max(SCHED_RR));
sp.sched_priority=sched_get_priority_max(SCHED_RR);
printf("sp.sched prio is %d\n", sp.sched_priority);
if (sched_setscheduler(getpid(), SCHED_RR, &sp) == -1) {
perror("sched_setscheduler");
exit(EXIT_FAILURE);
}
#endif
I'm using Ubuntu 20.04.
I have tried to modify /etc/security/limits.conf but with no success.
rlim_cur is 0. If i try to modify and set
rl.rlim_cur=(rlim_t) -20;
I get an ivalid argument message.
If I write:
rl.rlim_cur=(rlim_t) 20;
I get a Permission denied message. (After this setting I invoke setrlimit).
Does this have to do with capabilities?
Any help/guidance is greatly appreciated.
(As you may have already imagined the code for changing the scheduling policy to SCHED_RR fails also, perhaps the answer to this post solves this too, otherwise it may be the subject of a different post...)