【问题标题】:XV6- scheduler for loopXV6- 循环调度器
【发布时间】:2014-05-23 16:28:53
【问题描述】:

我正在我的 xv6 中实现一个新的调度程序,为此我需要了解它是如何工作的 1st,我面临一个有线问题,我无法真正理解 for 循环循环如何抛出进程

这是原始代码:

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);

 }
}

所以我尝试了一个简单的事情,我做了 for 循环,第一个应该循环抛出所有 proc 并计算它们而不做任何其他事情,第二个应该循环并像原来的那样运行它们,事情是它没有像我预期的那样工作,发生的事情是它从第一个 for 循环开始运行一个循环,然后从第二个循环运行一个循环,依此类推

void
scheduler(void)
{
struct proc *p;
int FirstCounter=0;
int SecCounter=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++){
    if(p->state != RUNNABLE)
      continue;
    counter++;
    cprintf("1st counter  = %d\n",FirstCounter); 
}
    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;
        cprintf("2nd counter  = %d\n",SecCounter); 
    }


release(&ptable.lock);

} }


输出是这样的,

.
.
.
1st counter  = 14
2nd counter  = 15
1st counter  = 15
2nd counter  = 16
.
.

为什么会这样?

【问题讨论】:

  • 这不可能是代码。 FirstCounter、SecCounter、计数器?!

标签: c for-loop process scheduler xv6


【解决方案1】:

我试过了,得到了同样的结果。

但是,当你打印进程的pid时,你会发现pid = 1 and pid = 2,我认为它们是init和page daemon进程。

我不知道初始化进程和页面守护进程的打印。但是,当你尝试fork()new 进程时,你会得到如你所想的打印结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    相关资源
    最近更新 更多