In this recipe, we'll be writing a small program to get the round-robin timeslice by using the sched_rr_get_interval() function:
- On a new shell, open a new file called schedGetInterval.cpp. We have to include <sched.h> for the scheduler capabilities, <iostream.h> to log to the standard output, and <string.h> to use the strerror function and translate the errno integer into a readable string:
#include <sched.h>
#include <iostream>
#include <string.h>
int main ()
{
std::cout << "Starting ..." << std::endl;
- To get the round-robin interval, we have to set the scheduler policy for our process:
struct sched_param sched;
sched.sched_priority = 8;
if (sched_setscheduler(0, SCHED_RR, &sched) == -1)
std::cout << "sched_setscheduler failed = "
<< strerror(errno)
<< std::endl;
else
std::cout << "sched_setscheduler, priority set to = "
<< sched.sched_priority << std::endl;
- Now, we can get the interval with the sched_rr_get_interval() function:
struct timespec tp;
int retCode = sched_rr_get_interval(0, &tp);
if (retCode == -1)
{
std::cout << "sched_rr_get_interval failed = "
<< strerror(errno) << std::endl;
return 1;
}
std::cout << "timespec sec = " << tp.tv_sec
<< " nanosec = " << tp.tv_nsec << std::endl;
std::cout << "End ..." << std::endl;
return 0;
}
Let's see how this works under the hood.