FCFS implemention for xv6



目前,为了我的大学项目,我正在尝试为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现在都在使用中。

我认为您犯了两个错误:

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

相关内容

  • 没有找到相关文章

最新更新