当前,我正在运行Ubuntu 16.04,Linux内核版本为4.15,并且我试图找出linux最后期限调度程序的用途。 我遵循official website中发布的示例代码,该示例代码将截止日期和期限设置为30ms。 以下是我的代码:
#include <time.h>
#include <errno.h>
#include <stdint.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <syscall.h>
#include <math.h>
#include <sched.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sched.h>
#include <linux/sched.h>
#include <signal.h>
#include <execinfo.h>
#include <getopt.h>
#include <sys/sysinfo.h>
#define __NR_sched_setattr 314
#define __NR_sched_getattr 315
#define RTIME long long int
#define rt_printk printf
#define SCHED_DEADLINE 6
struct sched_attr {
__u32 size;
__u32 sched_policy;
__u64 sched_flags;
/* SCHED_NORMAL, SCHED_BATCH */
__s32 sched_nice;
/* SCHED_FIFO, SCHED_RR */
__u32 sched_priority;
/* SCHED_DEADLINE (nsec) */
__u64 sched_runtime;
__u64 sched_deadline;
__u64 sched_period;
} sched_attr_t;
int sched_setattr(pid_t pid, const struct sched_attr *attr, unsigned int flags){
return syscall(__NR_sched_setattr, pid, attr, flags);
}
int sched_getattr(pid_t pid,struct sched_attr *attr,unsigned int size, unsigned int flags){
return syscall(__NR_sched_getattr, pid, attr, size, flags);
}
#define gettid() syscall(__NR_gettid) // for gettid
static volatile int done;
void *run_deadline(void *data){
struct sched_attr attr;
int x = 0, ret;
unsigned int flags = 0;
printf("deadline thread start %ld\n", gettid());
attr.size = sizeof(attr);
attr.sched_flags = 0;
attr.sched_nice = 0;
attr.__sched_priority = 0;
attr.sched_policy = SCHED_DEADLINE;
attr.sched_period = 30 * 1000 * 1000;
attr.sched_runtime = 10 * 1000 * 1000;
attr.sched_deadline = 30 * 1000 * 1000;
ret = sched_setattr(0, &attr, flags);
if(ret < 0){
done = 0;
perror("sched_setattr");
exit(-1);
}
while(!done){
x++;
}
printf("x = %d \n", x);
return NULL;
}
int main(){
pthread_t thread;
printf("main thread [%ld] \n", gettid());
pthread_create(&thread, NULL, run_deadline, NULL);
sleep(10);
done = 1;
pthread_join(thread, NULL);
printf("main dies [%ld] \n", gettid());
return 0;
}
我认为,如果正确设置了截止日期参数,则run_deadline()中的循环应该每30毫秒开始一次,x的值将为334,直到程序终止。
kay@IET:~/eclipse-workspace/dlsched/Debug$ sudo ./dlsched
main thread [29485]
deadline thread start 29486
x = 920417116
main dies [29485]
但是,我在循环后面添加了一个“ printf”来打印x的值,结果是“ x = 844426336”。似乎循环未按我设置的那样运行。在使用截止时间调度程序之前,是否需要执行任何程序?
我真的对这个问题感到困惑。谁能给我一些提示,建议或类似的东西?