I'm porting a C++ program that runs on Windows to WSL/Ubuntu. It's multithreaded, and all application threads run at the same priority. However, one thread needs to run at priority+1, and another at priority+2. Round-robin scheduling is also desirable, as is running the entire process at a slightly higher priority than the default.
The Linux target uses POSIX threads, but trying to do the above fails. First, setting the process' priority returns
-1
with
errno
set to
EACCES
(permission denied):
auto err = setpriority(PRIO_PROCESS, 0, -1);
if(err != 0)
{
ReportError(SysThread_ConfigureProcess, "setpriority", errno); }
Later,
pthread_create
returns
EINVAL
(invalid attributes) when
SCHED_RR
is set as the scheduling algorithm. If that code is commented out, the thread gets created:
auto err = pthread_attr_setinheritsched(&attrs, PTHREAD_EXPLICIT_SCHED);
if(err != 0)
{
return ReportError(SysThread_Create, "setinheritsched", err);
}
err = pthread_attr_setschedpolicy(&attrs, SCHED_RR); if(err != 0)
{
return ReportError(SysThread_Create, "setschedpolicy", err);
}
pthread_t thread;
err = pthread_create(&thread, &attrs, EnterThread, (void*) client);
if(err != 0)
{
return ReportError(SysThread_Create, "create", err); }
Finally, setting a thread's priority returns
EPERM
(not permitted):
auto err = pthread_setschedprio((pthread_t) nthread_, PriorityMap[prio]);
if(err != 0)
{
ReportError(SysThread_SetPriority, "setschedprio", err); }
Does anyone know how to do this sort of thing on WSL/Ubuntu?
What I have tried:
I found a post which said you had to give your program the necessary privileges:
sudo setcap cap_sys_nice=+ep <path-to-object-file>
I know next to nothing about arcane Ubuntu commands. But "nice" is actually used to describe Linux priorities, and Ubuntu accepts the command. But then, when I tell VS2022 to Debug the program, it fails with the following message:
Unable to start debugging. Unexpected GDB output from command
"-exec-run". During startup program exited with code 126.