structproc { uint sz; // Size of process memory (bytes) pde_t* pgdir; // Page table char *kstack; // Bottom of kernel stack for this process enumprocstatestate;// Process state int pid; // Process ID structproc *parent;// Parent process structtrapframe *tf;// Trap frame for current syscall structcontext *context;// swtch() here to run process void *chan; // If non-zero, sleeping on chan int killed; // If non-zero, have been killed structfile *ofile[NOFILE];// Open files structinode *cwd;// Current directory char name[16]; // Process name (debugging) uint ctime; // Process creation time FCFS int flag; // to judge if FCFS };
这便是进程的数据结构,我们添加
1 2
uint ctime; // Process creation time FCFS int flag; // to judge if FCFS
// Loop over process table looking for process to run. acquire(&ptable.lock); structproc *minP =0; for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
if(p->state != RUNNABLE){continue;}
// ignore init and sh processes from FCFS if(p->flag){ //scheduling if(p->pid > 1){ if (minP != 0){ if(p->ctime < minP->ctime){minP = p;} } else {minP = p;} } if(minP != 0 && minP->state == RUNNABLE) { p = minP; //cprintf("%d\n", minP->pid); } //find the minP as the target process } else{ if(minP != 0 && minP->state == RUNNABLE){ p = minP; } } //run new target process if (p!=0){ //p = minP;//the process with the smallest creation time c->proc = p; switchuvm(p); p->state = RUNNING; //cprintf("%d\n", p->pid); 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);