I've tried to implement a multiple thread application where I need to set some thread in real time priority because they cannot be delayed in any case.
I've used the The Linux scheduling APIs (http://manpages.ubuntu.com/manpages/bio ... ler.2.html and http://manpages.ubuntu.com/manpages/bio ... ity.2.html) in the following code that I can call in any thread :
Code: Select all
#include <pthread.h>
#include <sched.h>
#include <sys/resource.h>
#include <cstdio>
#include <cstdint>
static bool THREADPRIO_SetThreadPolicy(int Policy_s16);
bool THREADPRIO_SetProcessPriority(const uint32_t NiceValue_u32)
{
auto ReturnCode_b = false;
if (setpriority(PRIO_PROCESS, 0, NiceValue_u32) != 0)
printf("Error : Could not set priority in thread\n");
else
ReturnCode_b = true;
return ReturnCode_b;
}
bool THREADPRIO_SetThreadRealTimePolicy()
{
const auto ReturnCode_b = THREADPRIO_SetThreadPolicy(SCHED_FIFO);
if (ReturnCode_b)
printf("Error : Could not set real time priority\n");
return ReturnCode_b;
}
bool THREADPRIO_SetThreadIdlePolicy()
{
const auto ReturnCode_b = THREADPRIO_SetThreadPolicy(SCHED_IDLE);
if (ReturnCode_b)
printf("Error : Could not set idle priority\n");
return ReturnCode_b;
}
bool THREADPRIO_SetThreadDefaultPolicy()
{
const auto ReturnCode_b = THREADPRIO_SetThreadPolicy(SCHED_OTHER);
if (ReturnCode_b)
printf("Error : Could not set default priority\n");
return ReturnCode_b;
}
bool THREADPRIO_SetThreadPolicy(int Policy_s16)
{
auto ReturnCode_b = false;
const auto ThisThread_X = pthread_self();
struct sched_param SchedulerParams_X {};
SchedulerParams_X.sched_priority = sched_get_priority_max(Policy_s16);
ReturnCode_b = pthread_setschedparam(ThisThread_X, Policy_s16, &SchedulerParams_X) == 0 ? true : false;
if (ReturnCode_b)
{
auto lPolicy_s16 = 0;
ReturnCode_b = pthread_getschedparam(ThisThread_X, &lPolicy_s16, &SchedulerParams_X) == 0 ? true : false;
if (Policy_s16 != lPolicy_s16)
printf("Error : Priority is not set\n");
}
return ReturnCode_b;
}
Unfortunately, this is not works, when I call for SCHED_IDLE and SCHED_OTHER, I have a return error (Not permitted operation) and when I call it the argument SCHED_FIFO, there is no error code but the command line
Code: Select all
chrt -p <pid>
Also, It is impossible to set manualy the scheduler through the command line
Code: Select all
chrt -o -f -p <priority> <pid>
Code: Select all
chrt: failed to set pid <pid>'s policy: Operation not permitted
Thanks in advance