Guest User

Untitled

a guest
Dec 11th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. #include <time.h>
  2. #include <errno.h>
  3. #include <stdint.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <pthread.h>
  7. #include <linux/kernel.h>
  8. #include <linux/types.h>
  9. #include <syscall.h>
  10. #include <math.h>
  11. #include <sched.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/types.h>
  14. #include <sys/mman.h>
  15. #include <sched.h>
  16. #include <linux/sched.h>
  17. #include <signal.h>
  18. #include <execinfo.h>
  19. #include <getopt.h>
  20. #include <sys/sysinfo.h>
  21.  
  22. #define __NR_sched_setattr 314
  23. #define __NR_sched_getattr 315
  24.  
  25. #define RTIME long long int
  26. #define rt_printk printf
  27.  
  28. #define SCHED_DEADLINE 6
  29.  
  30. struct sched_attr {
  31. __u32 size;
  32.  
  33. __u32 sched_policy;
  34. __u64 sched_flags;
  35.  
  36. /* SCHED_NORMAL, SCHED_BATCH */
  37. __s32 sched_nice;
  38.  
  39. /* SCHED_FIFO, SCHED_RR */
  40. __u32 sched_priority;
  41.  
  42. /* SCHED_DEADLINE (nsec) */
  43. __u64 sched_runtime;
  44. __u64 sched_deadline;
  45. __u64 sched_period;
  46. } sched_attr_t;
  47.  
  48. int sched_setattr(pid_t pid, const struct sched_attr *attr, unsigned int flags){
  49.  
  50. return syscall(__NR_sched_setattr, pid, attr, flags);
  51. }
  52.  
  53.  
  54. int sched_getattr(pid_t pid,struct sched_attr *attr,unsigned int size, unsigned int flags){
  55.  
  56. return syscall(__NR_sched_getattr, pid, attr, size, flags);
  57. }
  58.  
  59.  
  60. #define gettid() syscall(__NR_gettid) // for gettid
  61.  
  62. static volatile int done;
  63.  
  64. void *run_deadline(void *data){
  65.  
  66. struct sched_attr attr;
  67. int x = 0, ret;
  68. unsigned int flags = 0;
  69.  
  70. printf("deadline thread start %ldn", gettid());
  71.  
  72. attr.size = sizeof(attr);
  73. attr.sched_flags = 0;
  74. attr.sched_nice = 0;
  75. attr.__sched_priority = 0;
  76.  
  77. attr.sched_policy = SCHED_DEADLINE;
  78. attr.sched_period = 30 * 1000 * 1000;
  79. attr.sched_runtime = 10 * 1000 * 1000;
  80. attr.sched_deadline = 30 * 1000 * 1000;
  81.  
  82. ret = sched_setattr(0, &attr, flags);
  83.  
  84. if(ret < 0){
  85. done = 0;
  86. perror("sched_setattr");
  87. exit(-1);
  88. }
  89.  
  90. while(!done){
  91. x++;
  92. }
  93.  
  94. printf("x = %d n", x);
  95. return NULL;
  96. }
  97.  
  98. int main(){
  99.  
  100. pthread_t thread;
  101.  
  102. printf("main thread [%ld] n", gettid());
  103. pthread_create(&thread, NULL, run_deadline, NULL);
  104. sleep(10);
  105. done = 1;
  106. pthread_join(thread, NULL);
  107. printf("main dies [%ld] n", gettid());
  108.  
  109. return 0;
  110. }
  111.  
  112. kay@IET:~/eclipse-workspace/dlsched/Debug$ sudo ./dlsched
  113. main thread [29485]
  114. deadline thread start 29486
  115. x = 920417116
  116. main dies [29485]
Add Comment
Please, Sign In to add comment