目前,在我的大学项目中,我正在尝试为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呢?
从技术上讲,两个可用CPU中的每一个都必须占用运行这两个虚拟进程的时间,对吗?
此外,我还使用为优先级调度编写的算法将创建时间设置为优先级。结果,在创建了两个进程之后,我无法运行任何东西,这意味着这两个CPU现在都在使用。
发布于 2020-04-14 14:31:34
我想你犯了两个错误:
for循环中,它应该在后面:调度(){ //永久(;){ //选择要运行的进程(p= ptable.proc;p< &ptable.procNPROC;p++) {.} //运行proc如果(p != 0) {…}
minP时犯了一个小错误:如果(minP != 0 && p->状态== RUNNABLE) p= minP;
应该是
如果(minP != 0&minP->状态== 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();
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;
}
}
}https://stackoverflow.com/questions/61175473
复制相似问题