首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >xv6的FCFS实现

xv6的FCFS实现
EN

Stack Overflow用户
提问于 2020-04-12 17:29:13
回答 1查看 1.5K关注 0票数 0

目前,在我的大学项目中,我正在尝试为xv6实现FCFS和优先级调度算法。我受够了优先事项之一,现在正努力使FCFS发挥作用。下面是我对代码所做的修改:

代码语言:javascript
复制
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呢?

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

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

EN

回答 1

Stack Overflow用户

发布于 2020-04-14 14:31:34

我想你犯了两个错误:

  1. 进程上下文在您的for循环中,它应该在后面:

调度(){ //永久(;){ //选择要运行的进程(p= ptable.proc;p< &ptable.procNPROC;p++) {.} //运行proc如果(p != 0) {…}

  • 您在选择minP时犯了一个小错误:

如果(minP != 0 && p->状态== RUNNABLE) p= minP;

应该是

如果(minP != 0&minP->状态== RUNNABLE) p= minP;

但是,由于minPstate是必要的RUNNABLE,并且在运行它之前测试它是否为null,所以可以编写

P= minP;

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

代码语言:javascript
复制
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;
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61175473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档