[目前,对于我的大学项目,我正在尝试为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现在都在使用中。
我认为您犯了两个错误:
过程上下文位于您的for
循环inside中,应该在以下位置:
schedule()
{
// for ever
for(;;)
{
// select process to run
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++)
{
...
}
// run proc
if (p != 0)
{
...
}
}
您在minP
选择中犯了一个小错误:
if(minP != 0 && p->state == RUNNABLE)
p = minP;
应该是
if(minP != 0 && minP->state == RUNNABLE)
p = minP;
但是由于minP
的state
是必需的RUNNABLE
,并且您在运行它之前测试了它是否不为null,因此可以编写
p = minP;
所以您得到的更正代码可能是:
void
scheduler(void)
{
struct proc *p = 0;
struct cpu *c = mycpu();
c->proc = 0;
for(;;)
{
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;
}
}
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;
}
}
}