我正在尝试在xv6中实现MLFQ和彩票调度程序的混合。我遇到的问题是我正在创建一个函数来计算队列中具有高优先级的进程总数以及它们的票证总和。当我在循环中打印no_of_tickets时,它会打印正确的值。但是当我在返回值之前打印它时。它打印零并进入永久循环。 这是我的代码
int no_of_processes = 0;
int count(int q_rank)
{
struct proc *p; int no_of_tickets = 0;
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
if(p->queue_rank==q_rank)
{
if(p->state != RUNNABLE)
continue;
no_of_tickets = (no_of_tickets + (p->tickets));
no_of_processes++;
}
}
return no_of_tickets;
}
struct pstat pstat;
void
scheduler(void)
{
struct proc *p;
struct proc *p1;
int f;
for(f=0; f<NPROC; f++)
{
pstat.hticks[f]=0;
pstat.lticks[f]=0;
pstat.inuse[f]=0;
}
for(;;){
int h_index=-1;
int l_index=-1;
sti();
acquire(&ptable.lock);
for(p1 = ptable.proc; p1 < &ptable.proc[NPROC]; p1++){
int no_of_tickets = count(1);
cprintf("%d", no_of_tickets);
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
if(p->queue_rank == 1)
{
h_index++;
if(p->state != RUNNABLE)
continue;
p->queue_rank=0;
(p->hticks)++;
pstat.inuse[h_index]=1;
pstat.pid[h_index]=p->pid;
pstat.hticks[h_index]=p->hticks;
run(p);
}
}
l_index++;
if((p1->state != RUNNABLE) || (p1->queue_rank != 0))
continue;
(p1->lticks)++;
pstat.lticks[l_index]=p1->lticks;
run(p1);
if(p1->state != ZOMBIE)
{
run(p1);
}
}
release(&ptable.lock);
}
}