【问题标题】:FCFS implemention for xv6xv6 的 FCFS 实现
【发布时间】:2020-04-12 17:29:13
【问题描述】:

目前,对于我的大学项目,我正在尝试为 xv6 实现 FCFS 和优先级调度算法。我已经完成了优先级的工作,现在正试图让 FCFS 发挥作用。以下是我对代码所做的修改:

void
scheduler(void)
{
  struct proc *p = 0;

  struct cpu *c = mycpu();
  c->proc = 0;

  for(;;)
  {
      // Enable interrupts on this processor.
      sti();

      // Loop over process table looking for process to run.
      acquire(&ptable.lock);
      for(p = ptable.proc; p < &ptable.proc[NPROC]; p++)
      {

            struct proc *minP = 0;

            if(p->state != RUNNABLE)
              continue;

            // ignore init and sh processes from FCFS
            if(p->pid > 1)
            {
              if (minP != 0){
                // here I find the process with the lowest creation time (the first one that was created)
                if(p->ctime < minP->ctime)
                  minP = p;
              }
              else
                  minP = p;
            }

            // If I found the process which I created first and it is runnable I run it
            //(in the real FCFS I should not check if it is runnable, but for testing purposes I have to make this control, otherwise every time I launch
            // a process which does I/0 operation (every simple command) everything will be blocked
            if(minP != 0 && p->state == RUNNABLE)
                p = minP;

          if(p != 0)
          {

            // Switch to chosen process.  It is the process's job
            // to release ptable.lock and then reacquire it
            // before jumping back to us.
            c->proc = p;
            switchuvm(p);
            p->state = RUNNING;

            swtch(&(c->scheduler), p->context);
            switchkvm();

            // Process is done running for now.
            // It should have changed its p->state before coming back.
            c->proc = 0;
          }
        }

        release(&ptable.lock);
  }
}

现在,我想问的是,当我运行两个虚拟进程(按照约定, foo.c 生成子进程进行无用计算,消耗时间)每个生成一个子进程时,为什么我是ps还能跑吗?

从技术上讲,必须占用 2 个可用 CPU 中的每一个来运行两个虚拟进程,对吗?

此外,我使用我为优先级调度编写的算法将创建时间设置为优先级。事实证明,在创建两个进程后,我无法运行任何东西,这意味着两个 CPU 现在都在使用中。

【问题讨论】:

    标签: operating-system scheduling xv6


    【解决方案1】:

    我认为你犯了两个错误:

    1. 进程上下文在你的for循环内部,它应该在:

      schedule()
      {
          // for ever
          for(;;) 
          {
               // select process to run
               for(p = ptable.proc; p < &ptable.proc[NPROC]; p++)
               {                 
                  ...
               }
      
               // run proc
               if (p != 0)
               {                 
                  ...
               }
          }
      
    2. 你在minP选择中犯了一个小错误:

      if(minP != 0 && p->state == RUNNABLE)  
          p = minP;
      

      应该是

      if(minP != 0 && minP->state == RUNNABLE)
         p = minP;
      

      但由于minPstate 是必需的RUNNABLE,并且您在运行它之前测试它不为空,您可以编写

         p = minP; 
      

    所以你更正的代码可能是:

    void
    scheduler(void)
    {
        struct proc *p = 0;
        struct cpu *c = mycpu();
        c->proc = 0;
    
        for(;;)
        {
            sti();
    
            struct proc *minP = 0;
    
            // Loop over process table looking for process to run.
            acquire(&ptable.lock);
            for(p = ptable.proc; p < &ptable.proc[NPROC]; p++)
            {
                if(p->state != RUNNABLE)
                    continue;
    
                    // ignore init and sh processes from FCFS
                    if(p->pid > 1)
                    {
                        if (minP != 0) {
                            // here I find the process with the lowest creation time (the first one that was created)
                            if(p->ctime < minP->ctime)
                                minP = p;
                        }
                        else
                            minP = p;
                    }
    
            }
            p = minP;
            release(&ptable.lock);
    
            if(p != 0)
            {
    
                c->proc = p;
                switchuvm(p);
                p->state = RUNNING;
    
                swtch(&(c->scheduler), p->context);
                switchkvm();
    
                c->proc = 0;
            }
        }
    }
    

    【讨论】:

    • 这对我来说是第一次!我在 C 中遇到缩进错误!无论如何要修复它?只是一个小的更新更改,我认为 minP 必须在 for 循环之外声明以防止任何声明错误!
    • @ShabbirKhandwala 确实,minP 必须在 forloop 之前声明。关于 indentation (?) 很奇怪,错误信息是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-31
    相关资源
    最近更新 更多