【问题标题】:Where to implement fifo and lifo in xv6xv6 在哪里实现 fifo 和 lifo
【发布时间】:2020-04-21 14:12:31
【问题描述】:

我目前正在使用 xv6 做作业,我需要使用以下代码样式实现 FIFO 和 LIFO:

#define IFO 1
#if IFO==1
  DO FIFO HERE
#endif


#if IFO==2
  DO LIFO HERE
#endif

我应该修改什么 xv6 文件来实现这些调度策略。我应该做什么的任何提示,我是使用 xv6 的新手,我不太了解我在做什么。此外,作业包括一个名为 sched_test 的额外文件:

#include "types.h"
#include "stat.h"
#include "user.h"
#include <time.h> 

void delay(int number_of_milliseconds){ 
    // Converting time into milli_seconds 
    int milli_seconds = number_of_milliseconds; 

    int start_time = uptime(); 

    while (uptime() < start_time + milli_seconds) ;
} 
int main(){ 
    // Creating first child 
    int n1 = fork(); 
    int count = 0;
    int times = 20; 
    int millisec_to_wait = 5;

    // Creating second child. First child 
    // also executes this line and creates 
    // grandchild. 
    int n2 = fork(); 

    if (n1 > 0 && n2 > 0) 
    { 
        printf(1,"\n0.- parent ID: %d\n",getpid());
        while(count < times){
            printf(1,"0 ");
            delay(millisec_to_wait); 
            count++;
        }
    } 
    else if (n1 == 0 && n2 > 0) 
    { 
        printf(1,"\n1.- first child ID: %d\n",getpid()); 
        while(count < times){
            printf(1,"1 ");
            delay(millisec_to_wait); 
            count++;
        }
    } 
    else if (n1 > 0 && n2 == 0) 
    { 
        printf(1,"\n2.- second child ID: %d\n",getpid()); 
        while(count < times){
            printf(1,"2 ");
            delay(millisec_to_wait); 
            count++;
        }
    } 
    else { 
        printf(1,"\n3.- third child ID: %d\n",getpid()); 
        while(count < times){
            printf(1,"3 ");
            delay(millisec_to_wait); 
            count++;
        }
    } 

    while(wait() >= 0);
    exit(); 
} 

我已经将它包含在 MAKEFILE 中,并执行、make clean、make 和 make qemu。

【问题讨论】:

    标签: c scheduling fifo xv6 lifo


    【解决方案1】:

    您可以在 proc.c 中的 scheduler() 函数中开始您的实现。这是in vanilla xv6 的位置。在基本的 xv6 中,它只是遍历进程表以查找第一个进程。您需要添加自己的数据结构,您将在scheduler() 中使用该数据结构来确定接下来要运行的进程。这是该函数的一个重要部分,并附有一些注释:

        // LOOP OVER SOME DATA STRUCTURE TO FIND A RUNNABLE PROCESS
        acquire(&ptable.lock);
        for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
          if(p->state != RUNNABLE)
            continue;
    
          c->proc = p;
          switchuvm(p);
          p->state = RUNNING;
    
          swtch(&(c->scheduler), p->context);
          switchkvm();
    
          c->proc = 0;
    
          // ADUJST YOUR DATA STRUCTURE TO AFTER RUNNING THE PROCESS
          #if OWNSCHED==1
              // Add to the end of some D/S
          #elif OWNSCHED==2
              // Add to the beginning of some D/S
          #endif
        }
        release(&ptable.lock);
    
    

    您可以向proc.c 中的ptable 结构添加一个数据结构,以跟踪要运行的进程的顺序,然后在scheduler() 中循环它。当然,这意味着您必须修改一些其他功能,例如 allocproc() 才能在适当的位置添加新进程。其余部分取决于一些事情,例如是否允许您使用静态数组来存储进程,或者您是否必须使用链表。

    如果您还没有,我强烈建议您阅读the xv6 book 的第 6 章。当我不得不在 xv6 中实现 MLFQ 时,它真的很有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-19
      • 1970-01-01
      • 1970-01-01
      • 2012-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多