c - xv6 调度程序 - proc 从不!= 0

标签 c scheduler xv6

基本上,我正在为实现多级优先级队列的 xv6 内核实现一个调度程序。我有一个严重的问题,我不明白,我类(class)的助教不明白,我已经错过了这个项目的最后期限,所以现在帮助我不会给我任何加分 - 但我想知道为什么我有以下行为......

首先,这是我为 xv6 更改的原始调度程序(为了比较 - 这不是我的实现):

// Per-CPU process scheduler.
// Each CPU calls scheduler() after setting itself up.
// Scheduler never returns.  It loops, doing:
//  - choose a process to run
//  - swtch to start running that process
//  - eventually that process transfers control
//      via swtch back to the scheduler.
void
scheduler(void)
{
  struct proc *p;

  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++){
      if(p->state != RUNNABLE)
        continue;

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

      // Process is done running for now.
      // It should have changed its p->state before coming back.
       proc = 0;
    }
    release(&ptable.lock);

  }
}

新的调度器的思路是这样的:在ptable里面有一个数组,里面填充了proc结构体。我将此过程数组中的每个元素称为“p”,它们包含基本信息(例如它们拥有的“门票”数量或状态等)。我需要在一个时间片内运行所有高优先级 (HP) p,然后将它们的优先级更改为低。当没有 HP 过程时,我“随机”选择一个 LP 过程并运行它两个时间片。我的算法如下:

scheduler()
    for(;;) //scheduler NEVER completes
        //information gathering
        for (entire proc array) //goes over it once
            gather how many HP and LP procs
            count total HP and LP tickets in each proc (for lottery)

        if #HP > 1 //randomly choose a HP proc
            hold HP lottery, run one HP proc //afterwards, 
            int rand = random() % num_HP_tickets
            for (entire array)
                curr_index_of_tickets += p->num_tickets;
                if (curr_index_of_tickets >= rand) //we found the right p!
                    run p for one time slice
        else if #HP == 1
            find and run the one HP proc
        else if #HP < 1 //then no HP procs! Time to run LP
            if #LP > 1
                hold LP lottery, run one LP proc for two time slices, similar to above
            else if #LP == 1
                find the LP proc, run it

这就是问题所在...我的 proc 似乎总是等于 0。它从来没有看到 p 中的信息,也没有从 p 收集 proc 信息,等等。我不知道为什么。

我用大量的打印输出语句进行了测试。我会先在这里发布它的输出:

entered scheduler
entered INFORMATION loop: iteration 0
Proc is 0
Proc is 0 even after proc gets p from for loop
Information 0: found 0 HP procs
Information 0: found 0 LP procs
Num HP: 0
Num LP: 0
Num HP Tickets: 0
Num LP Tickets: 0
This concludes loop #: 0
entered INFORMATION loop: 1
Proc is 0
entered INFORMATION loop: 2
Proc is 0
entered INFORMATION loop: 3
Proc is 0
etc.....

再一次,不知道为什么这不起作用...我确定有多个错误,而且我有一大堆打印出来的语句只是为了看看哪里出了问题。这也需要相当多的调试工作,所以我对是否有人有答案我不太乐观......为此目的,以及这些警告,这是我的整个调度程序功能。抱歉,长度...

void
scheduler(void)
{
  struct proc *p;

 for(;;){

   //TODO: Remove statement
   cprintf("entered scheduler\n");

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

    // Loop over process table looking for process to run.
    acquire(&ptable.lock);

    //keeps track of number of procs (to be used for lottery RNG)
    int num_LP_t = 0;
    int num_HP_t = 0;
    int num_HP = 0;
    int num_LP = 0;
    int rand = 0;
    int curr_tickets = 0;

    //goes through once to complete information gathering for HP and LP queue

    //TODO: remove i - for testing only
    int i = -1;
    for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){

       i++;
      //TODO: Remove statement
       cprintf("entered INFORMATION loop: %d\n", i);
       if (proc == 0) cprintf("Proc is 0\n");      


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

      // Switch to chosen process.  It is the process's job
      // to release ptable.lock and then reacquire it
      // before jumping back to us.
      //TODO: uncomment? proc = p;

      if (proc == 0) cprintf("Proc is 0 even after proc gets p in INFORMATION LOOP\n");
      cprintf("Information %d: found %d HP procs\n", i, num_HP);
      cprintf("Information %d: found %d LP procs\n", i, num_LP);
      cprintf("Num HP: %d\n", num_HP);
      cprintf("Num LP: %d\n", num_LP);
      cprintf("Num HP Tickets: %d\n", num_HP_t);
      cprintf("Num LP Tickets: %d\n", num_LP_t);
      cprintf("This concludes loop #: %d\n", i);


      if (p->priority_level == 1){
          num_HP++;
          num_HP_t += p->num_tickets;
      }
      if (p->priority_level == 0){
          num_LP++;
          num_LP_t += p->num_tickets;
      }

    }//end information loop

  cprintf("Begin HP Queue:\n");

    if (num_HP > 1){
  cprintf("HP Queue had: %d procs to run\n", num_HP);
        rand = random() % num_HP_t;
  cprintf("We choose our random to be: %d\n", rand);
        for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){

            if (p->state != RUNNABLE) continue;
            //TODO: uncomment? proc = p;
            if (proc == 0) cprintf("Proc is 0 even after setting proc = p in HP queue\n");  

            if (p->priority_level == 1){
  cprintf("Found a HP Proc while searching for rand, currently at: %d\n", curr_tickets);
                curr_tickets += p->num_tickets;
                if (curr_tickets >= rand){
  if (proc == 0)
  cprintf("proc is 0 while in HP queue 1\n");
                    //proc = p;
                    switchuvm(p);   
                p->state = RUNNING;
  /*cprintf("Proc Info:");
  cprintf("uint sz: %d", proc->sz);     
  cprintf("enum procstate state: %s", proc->state);       
  cprintf("volatile int pid: %d", proc->pid);  
  cprintf("int killed: %d", proc->killed);           
  cprintf("int priority_level: %d", proc->priority_level);*/
                    swtch(&cpu->scheduler, proc->context);
                    switchkvm();
                    if (p->state == RUNNABLE){
  cprintf("HP process still runnable after 1 TS.\nHere is the updated information\n");
                        p->priority_level = 0;
                        num_HP--;
                        num_LP++;
                        num_HP_t -= p->num_tickets;
                        num_LP_t += p->num_tickets;
      cprintf("Num HP: %d\n", num_HP);
      cprintf("Num LP: %d\n", num_LP);
     cprintf("Num HP Tickets: %d\n", num_HP_t);
      cprintf("Num LP Tickets: %d\n", num_LP_t);
                    }
                }
            }
        }    
    }

    else if (num_HP == 1){
        for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){

            if (p->state != RUNNABLE) continue;
            //TODO: Uncomment? proc = p;
            if (proc == 0) cprintf("Proc is 0 even after setting proc = p in HP == 1 queue\n"); 

            if (p->priority_level == 1){
                //proc = p;
                switchuvm(p);   
            p->state = RUNNING;
                swtch(&cpu->scheduler, proc->context);
                switchkvm();
                if (p->state == RUNNABLE){
                    p->priority_level = 0;
                    num_HP--;
                    num_LP++;
                    num_HP_t -= p->num_tickets;
                    num_LP_t += p->num_tickets;
                }
            }
        }
     }//end else num_HP = 1

    else if (num_HP < 1){

        if (num_LP > 1){
           rand = random() % num_LP_t;
            for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){

                if (p->state != RUNNABLE) continue;
                //TODO: uncomment? proc = p;

            if (proc == 0) cprintf("Proc is 0 even after setting proc = p in LP > 1 queue\n"); 
               if (p->priority_level == 0){
                    curr_tickets += p->num_tickets;
                    if (curr_tickets >= rand) {
                        //proc = p;
                        switchuvm(p);   
                    p->state = RUNNING;
                        swtch(&cpu->scheduler, proc->context);
                        switchkvm();
                        if (p->state != RUNNABLE){
                            num_LP--;
                            num_LP_t -= p->num_tickets;
                        }
                    }
                }
            }
        }

        else if (num_LP == 1){

           for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){

                if (p->state != RUNNABLE) continue;
                //TODO: Uncomment? proc = p;
            if (proc == 0) cprintf("Proc is 0 even after setting proc = p in LP == 1 queue\n"); 

                if (p->priority_level == 0){
                    //proc = p;
                    switchuvm(p);   
                p->state = RUNNING;
                    swtch(&cpu->scheduler, proc->context);
                    switchkvm();
                    if (p->state != RUNNABLE){
                        num_LP_t -= p->num_tickets;
                    }               
                }
            }
        }
    }//end lp
    proc = 0;
    release(&ptable.lock);
    }//end outer for loop (;;)
}// end scheduler

最佳答案

注释掉 proc=p。显而易见的事情是显而易见的。

关于c - xv6 调度程序 - proc 从不!= 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22006642/

相关文章:

c++ - 如何读取资源管理器文件夹地址?

c - 将字符串结尾字符添加到文件会损坏它..?

在循环中调用 pthread_cancel() 显示内存使用量增加

c# - 如何手动触发 Hangfire 作业?

linux - Ubuntu 中 SCHED_FIFO 的问题

linux - git --- 如何在 linux 上安排 `git push`?

c - Unix(在 C 中)尝试为 XV6 编写 tail

c - 如何在 xv6 中为内核级线程实现克隆

在 qemu-system-misc 包中找不到 qemu-system-riscv64

c - Linux 中 chattr 的替代方法