Option 2: in your code, set your running process ID (PID) to a desired scheduler policy and priority. I have provided 5 thorough demos related to all this. See "Demo 1" through "Demo 5" inside function set_scheduler()
in my sleep_nanosleep_minimum_time_interval.c test file:
int retcode;
{
const struct sched_param priority_param =
{
.sched_priority = 1,
};
retcode = sched_setscheduler(0, SCHED_RR, &priority_param);
if (retcode == -1)
{
printf("ERROR: in file %s: %i: Failed to set scheduler. "
"errno = %i: %s.\n",
__FILE__, __LINE__, errno, strerror(errno));
if (errno == EPERM)
{
printf(" You must use `sudo` or run this program as root to "
"have proper privileges!\n");
}
}
else
{
printf("`sched_setscheduler()` successful.\n");
}
retcode = mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT);
if (retcode == -1)
{
printf("ERROR: in file %s: %i: Failed to lock memory into RAM. "
"errno = %i: %s.\n",
__FILE__, __LINE__, errno, strerror(errno));
if (errno == EPERM)
{
printf(" You must use `sudo` or run this program as root to "
"have proper privileges!\n");
}
}
else
{
printf("`mlockall()` successful.\n");
}
}
{
const int new_priority = 2;
const struct sched_param priority_param =
{
.sched_priority = new_priority,
};
retcode = sched_setparam(0, &priority_param);
if (retcode == -1)
{
printf("ERROR: in file %s: %i: Failed to set priority. "
"errno = %i: %s.\n",
__FILE__, __LINE__, errno, strerror(errno));
if (errno == EPERM || errno == EINVAL)
{
printf(" You must use `sudo` or run this program as root to "
"have proper privileges!\n");
}
}
else
{
printf("`sched_setparam()` successful.\n");
}
}
{
pthread_t this_thread = pthread_self();
const struct sched_param priority_param =
{
.sched_priority = 1,
};
retcode = pthread_setschedparam(this_thread, SCHED_RR, &priority_param);
if (retcode != 0)
{
printf("ERROR: in file %s: %i: Failed to set pthread scheduler. "
"retcode = %i: %s.\n",
__FILE__, __LINE__, retcode, strerror(retcode));
if (retcode == EPERM)
{
printf(" You must use `sudo` or run this program as root to "
"have proper privileges!\n");
}
}
else
{
printf("`pthread_setschedparam()` successful.\n");
}
retcode = mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT);
if (retcode == -1)
{
printf("ERROR: in file %s: %i: Failed to lock memory into RAM. "
"errno = %i: %s.\n",
__FILE__, __LINE__, errno, strerror(errno));
if (errno == EPERM)
{
printf(" You must use `sudo` or run this program as root to "
"have proper privileges!\n");
}
}
else
{
printf("`mlockall()` successful.\n");
}
}
{
pthread_t this_thread = pthread_self();
const int priority = 3;
retcode = pthread_setschedprio(this_thread, priority);
if (retcode != 0)
{
printf("ERROR: in file %s: %i: Failed to set pthread priority. "
"retcode = %i: %s.\n",
__FILE__, __LINE__, retcode, strerror(retcode));
if (retcode == EPERM || retcode == EINVAL)
{
printf(" You must use `sudo` or run this program as root to "
"have proper privileges!\n");
}
}
else
{
printf("`pthread_setschedprio()` successful.\n");
}
}
{
retcode = mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT);
if (retcode == -1)
{
printf("ERROR: in file %s: %i: Failed to lock memory into RAM. "
"errno = %i: %s.\n",
__FILE__, __LINE__, errno, strerror(errno));
if (errno == EPERM)
{
printf(" You must use `sudo` or run this program as root to "
"have proper privileges!\n");
}
}
else
{
printf("`mlockall()` successful.\n");
}
pthread_attr_t pthread_attr;
retcode = pthread_attr_init(&pthread_attr);
if (retcode != 0)
{
printf("ERROR: `pthread_attr_init()` failed. "
"retcode = %i: %s.\n",
retcode, strerror(retcode));
}
int scheduler_policy = SCHED_RR;
retcode = pthread_attr_setschedpolicy(&pthread_attr, scheduler_policy);
if (retcode != 0)
{
printf("ERROR: `pthread_attr_setschedpolicy()` failed. "
"retcode = %i: %s.\n",
retcode, strerror(retcode));
}
const struct sched_param priority_param =
{
.sched_priority = 1,
};
retcode = pthread_attr_setschedparam(&pthread_attr, &priority_param);
if (retcode != 0)
{
printf("ERROR: `pthread_attr_setschedparam()` failed. "
"retcode = %i: %s.\n",
retcode, strerror(retcode));
}
retcode = pthread_attr_setinheritsched(&pthread_attr,
PTHREAD_EXPLICIT_SCHED);
if (retcode != 0)
{
printf("ERROR: `pthread_attr_setinheritsched()` failed. "
"retcode = %i: %s.\n",
retcode, strerror(retcode));
}
pthread_t new_thread;
retcode = pthread_create(&new_thread, &pthread_attr,
dummy_pthread_action, "new_thread");
if (retcode != 0)
{
printf("ERROR: `pthread_create()` failed. "
"retcode = %i: %s.\n",
retcode, strerror(retcode));
if (retcode == EPERM)
{
printf(" You must use `sudo` or run this program as root to "
"have proper privileges!\n");
}
}
retcode = pthread_attr_destroy(&pthread_attr);
if (retcode != 0)
{
printf("ERROR: `pthread_attr_destroy()` failed. "
"retcode = %i: %s.\n",
retcode, strerror(retcode));
}
const char* return_message;
retcode = pthread_join(new_thread, (void**)&return_message);
if (retcode != 0)
{
printf("ERROR: `pthread_join()` failed. "
"retcode = %i: %s.\n",
retcode, strerror(retcode));
}
else
{
printf("`pthread_join()` successful: return_message = \"%s\"\n",
return_message);
}
}