【发布时间】:2020-04-12 17:29:13
【问题描述】:
目前,对于我的大学项目,我正在尝试为 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 现在都在使用中。
【问题讨论】:
标签: operating-system scheduling xv6