Chinaunix首页 | 论坛 | 博客
  • 博客访问: 490896
  • 博文数量: 23
  • 博客积分: 398
  • 博客等级: 一等列兵
  • 技术积分: 850
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-03 22:18
文章分类

全部博文(23)

文章存档

2013年(9)

2012年(14)

分类: LINUX

2012-10-08 00:00:00

      前段时间看操作系统处理机调度一节时发现轮转调度算法中有一个调度算法(多级反馈队列调度算法)比较有
意思,而且其性能也比较好,看操作系统有时光看的话,看着看着就会逐渐感觉枯燥,我确实不想陷入无趣的学习中去,于是想提高自己的动手能力,于是自己写一个小程序模拟一下这个算法,
一开始感觉没什么思路,后来想想也没什么难的,就下定决心自己弄一个出来。
     现在贴出来供大家参考一下,有什么不合里的希望读者帮忙指正,谢谢!


     多级反馈队列调度算法原理描述如下:

  1、设置多个就绪队列,并给队列赋予不同的优先级数,第一个最高,依次递减。
  2、赋予各个队列中进程执行时间片的大小,优先级越高的队列,时间片越小。
  3、当一个新进程进入内存后,首先将其放入一个对列末尾,如果在一个时间片
      结束时尚未完成,将其转入第二队列末尾。
  4、当一个进程从一个对列移至第n个队列后,便在第n个队列中采用时间片轮转执行完。
  5、仅当时间片空闲时,才调度第二个队列中的进程。
在低优先级的队列中的进程在运行时,又有新到达的作业,
      那么在运行完这个时间片后,CPU马上分配给新到达的作业()。但本程序还没实现抢占式。
 
   
    
    
 list.zip    (从linux 内核2.6 中移出来的附件,如果需要可以下载下来与下面的程序放在同一目录下)

   好了,不多说看看代码再说^—^ ,我花了一点时间在代码中加入些许注释,希望对你阅读有帮助!
  

点击(此处)折叠或打开

  1. /* reaction_list.c */
  2. // list.h用户态使用,模拟多级反馈队列调度算法(只是还没实现优先级抢占式分配)
  3. // 由于使用了list.h 所以很多操作不必重新编码,大大减少工作量
  4. // 多级反馈队列调度算法更具体描述请翻阅有关操作系统书籍

  5. #include <stdio.h>
  6. #include <math.h>    // 编译时请使用 -lm 参数
  7. #include <pthread.h>  // 编译时用  -lpthread 参数
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include "list.h" // 该文件从内核中移出来,并放到程序所在目录下

  12. struct reaction_queue
  13. {
  14.     int time_slice; //队列i的时间片
  15.     struct list_head list_r; //队列的双向头指针
  16. };

  17. //相当于task_struct的简单结构
  18. struct process_task
  19. {
  20.     char pro_name[30];// 作业名称
  21.     int pt_ID; // 作业ID
  22.     int need_times; // 作业需要执行的时间
  23.     struct list_head list_node; // 链接队列的指针节点
  24. };

  25. //#define PRINT_INFO 1

  26. #define LEVELS 10 // 设置队列数量

  27. FILE *Out = NULL;

  28. struct reaction_queue rl[LEVELS]; // 指向队列首的数组
  29. int G_pro_ID = 0; // 为载入的作业分配ID

  30. void reaction_init(struct reaction_queue p[], int n)
  31. {
  32.     int i;

  33.     for (i = 0; i < n; i++) {
  34.         p[i].time_slice = pow(2, i+1); // 各队列时间片2^i
  35.         INIT_LIST_HEAD(&p[i].list_r);
  36.         //printf("Init %d Node\n",i);
  37.     }    
  38. //    puts("Init OK");
  39. }

  40. // p节点初始化
  41. void process_struct_init(struct process_task *p)
  42. {
  43.     memset(p->pro_name, 0, sizeof(p->pro_name)/sizeof(char));
  44.     p->need_times = 0;
  45.     INIT_LIST_HEAD(&p->list_node);
  46.     //以下2个宏只能用于定义并初始化,所以这里不能用    
  47. //    struct list_head ls = LIST_HEAD_INIT(ls);LIST_HEAD(lp);
  48. }

  49. // 根据p所在的队列级别,运行相应的时间片(这里以睡眠模拟运行)
  50. struct process_task *dequeue_running(struct list_head *p, int time_slice)
  51. {
  52.     int sleep_time = 0;
  53.     struct process_task *q = NULL;

  54.     // 首先通过p指向的成员list_node获得p所在的结构体指针(即struct process_task*
  55.     q = list_entry(p, struct process_task, list_node);
  56.     if(q->need_times < time_slice) {
  57.         sleep_time = q->need_times;
  58.         q->need_times = 0;
  59.     }
  60.     else {
  61.         sleep_time = time_slice;
  62.         q->need_times -= time_slice;
  63.     }
  64.     // 将其运行信息写入文件中
  65.     fprintf(Out,"%s \t %d\t\t %d\n", q->pro_name, time_slice, \
  66.             q->need_times);
  67.     sleep(sleep_time);

  68.     return q;
  69. }

  70. // 该函数没有使用,若要使用可以将PRINT_INFO宏的注释去掉
  71. void pro_print()
  72. {
  73.     int i;
  74.     struct list_head *lp, *lshead;
  75.     struct process_task *p = NULL;

  76.     #ifdef PRINT_INFO
  77.     for (i = 0; i < LEVELS; i++) {
  78.         lshead = &rl[i].list_r;
  79.         if (!list_empty(lshead)) {
  80.             printf("rl[%d] ID pro_name left_time----\n", i);
  81.             list_for_each(lp, lshead) {
  82.                 p = list_entry(lp, struct process_task, list_node);
  83.             //    printf("rl[%d] ID pro_name left_time----\n", i);
  84.                 printf("\t %-4d %-9s %d\n", p->pt_ID, p->pro_name, \
  85.                             p->need_times);
  86.             }
  87.             puts("--------------------------------");
  88.         }
  89.         else {
  90.             printf("rl[%d] is NULL\n",i);
  91.         }
  92.     }
  93.     #endif
  94. }
  95. // 反馈队列调度(先来先服务,当然这里没实现抢占式调度),主要利用list.h中的函数
  96. void levels_schedule()
  97. {
  98.     int i;
  99.     struct list_head *lp, *lshead;
  100.     struct process_task *p = NULL;
  101. //start:
  102. //    system("gnome-terminal\n");
  103.     for (;;) {
  104.         
  105.         for (i = 0; i < LEVELS; i++) {
  106.             lshead = &rl[i].list_r;
  107.             if (!list_empty(lshead)) {
  108.                 lshead = lshead->next;
  109.             //    printf("run\n");
  110.                 p = dequeue_running(lshead,rl[i].time_slice);
  111.                 if (p->need_times > 0) { // 时间片用完,但还没完成
  112.                     int next = (i+1)%LEVELS;
  113.                     lp = &rl[next].list_r;
  114.                     list_move_tail(lshead, lp);    // 将其移至队尾    
  115.                 }
  116.                 else {
  117.                     fprintf(Out, "\nID=%d %s quit\n",p->pt_ID, p->pro_name);
  118.                     list_del(lshead); // 执行完了,撤销该作业
  119.                     free(p);
  120.                 }
  121.                 break;
  122.             }        
  123.         }
  124.         
  125.         if (LEVELS == i) { // 本不应该退出的,但在这里只是模拟
  126.             fprintf(Out,"No process is running, quit!\n");
  127.             break;    
  128.         }
  129.     }
  130. }

  131. // 加载作业插入到就绪队列0
  132. void load_pro_thread()
  133. {
  134.     char str[50];
  135.     int time;
  136.     struct list_head *first_head = &rl[0].list_r;

  137.     for (; ;) {
  138.         printf("Input process: pro_name need_times:");
  139.         scanf("%s %d", str, &time);
  140.         if (0 == time)
  141.             break;
  142.         struct process_task *new = (struct process_task *)malloc( \
  143.                 sizeof(struct process_task));
  144.         if (NULL == new) {
  145.             printf("Memory malloc failed\n");
  146.             exit(1);
  147.         }
  148.         //printf("pro_name: %s need_time: %d\n", str, time);
  149.         process_struct_init(new);
  150.         strcpy(new->pro_name, str);
  151.         new->need_times = time;
  152.         new->pt_ID = ++G_pro_ID;
  153.         list_add_tail(&new->list_node, first_head);
  154.         
  155.         getchar();
  156.         pro_print();
  157.     }
  158.     printf("\n");
  159. }

  160. int main()
  161. {
  162.     int i;
  163.     pthread_t th_id;

  164.     //把调度信息写入文件方便观看
  165.     Out = fopen("list_reaction.txt","w");
  166.     if (!Out) {
  167.         return -1;
  168.     }
  169.     fprintf(Out, "pro_name time_slice need_timei\n");

  170.     reaction_init(rl, LEVELS); //初始化全部队列为空
  171.     // 启动一线程加载将要运行的作业,将其插入到队列下标为0的队列中
  172.     if(pthread_create(&th_id, NULL, (void *)load_pro_thread, NULL)) {
  173.         printf("Thread create failed\n");
  174.         exit(1);
  175.     }
  176.     sleep(5);//等一下以便装入第一个作业
  177. //    pro_print();
  178.     levels_schedule();//主线程用于调度
  179.     fclose(Out);
  180.     
  181.     return 0;
  182. }
 
   编译链接代码: gcc reaction_list.c -o reaction_list -lm -lpthread 执行 ./reaction_list 然后根据提示装载
“作业”,待所有“作业”完成“退出后,可以在 list_reaction.txt 这个文件中看到“作业”执行的相关信息
        注:本博客的文章除注明有“转载”字样的外,均为原创,欢迎转载,请注明文字出处,谢谢!
阅读(5145) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~