Why does the process launched by the system not behave the same as when launched interactively?

I have a program that generates a stream in real time with code as follows:

schparam.sched_priority = sched_get_priority_max(SCHED_FIFO);
getrlimit(RLIMIT_RTPRIO , &rlim);
rlim.rlim_cur = schparam.sched_priority;
setrlimit(RLIMIT_RTPRIO , &rlim);
result = pthread_setschedparam(pthread_self(),SCHED_FIFO, &schparam);
if(result != 0 )
    printf("failed to set priority\n");

My system limit by default does not allow scheduled RT threads, so I need to call setrlimit to raise this value. The above code works as needed when I enter the root shell and run the program manually.

However, when I start the program automatically using systemd at startup, the schedule is not set with a permission error. The setrlimit call seems to work, judging by the return value and subsequent getrlimit calls inside the process. But the call to pthread_setschedparam does not seem to understand that the limit has been increased.

Again, all this works great when I run the program manually. What am I missing here?

+3
source share
1 answer

By default, systemd installs LimitRTPRIO=0. You can check this with systemctl show $servicename | grep LimitRTPRIO.

If the soft limit of RLIMIT_RTPRIO is 0, then only the allowed changes are a decrease in priority or a transition to a non-real-time policy.

A change LimitRTPRIOas suggested by Siosm, for example LimitRTPRIO=infinityin your device file, should do the trick.

+1
source

All Articles