RMS(Rate Monotonic Secheduling)是一种可以提供固定任务周期的调度系统。与普通的随机式的、不可预测的调度系统不同,RMS可以提供固定的、可设置的调度周期,且精度可达到纳秒级。多个RMS调度之间优先级按照周期越小优先级越高的原则。
函数原型:
sched_rms_init()为线程 thread 创建一个rms调度器,sched_rms_destroy()用于删除一个rms调度器。sched_rms_period()启动rms调度器,period为周期。执行sched_rms_period()后会开始计时,直到下次执行sched_rms_period()时,若未到设置周期则阻塞到时间到达,若超过设置周期则会发生溢出错误;
测试代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sched_rms.h>
#include <pthread.h>
int main (int argc, char **argv)
{
int i;
sched_rms_t rms;
struct timespec period;
period.tv_sec = 3;
period.tv_nsec = 0;
if (sched_rms_init(&rms, pthread_self()) < 0) {
perror("sched_rms_init");
return (PX_ERROR);
}
for (i = 0; i < 5; i++) {
if (sched_rms_period(&rms, &period) < 0) {
perror("sched_rms_period");
return (PX_ERROR);
}
system("date");
}
if (sched_rms_destroy(&rms) < 0) {
perror("sched_rms_destroy");
return (PX_ERROR);
}
return (0);
}
运行结果:
若在system("data");之后加入一句sleep(5),则会迫使调度器超时,从而引发溢出超时错误,如下: