一、实验目的
将xv6时间片轮转改变为优先级调度
二、实验内容
为每个进程添加一个优先级值(假设取一个介于 0 到 31 之间的范围)。从就绪列表进行调度时,始终首先调度优先级最高的进程。添加系统调用以更改进程的优先级。进程可以随时更改其优先级。如果优先级低于就绪列表上的任何进程,则必须切换到该进程。为避免进程饥饿,进程优先级可变。如果进程等待,增加其优先级。当它运行时,请将其减小。
三、实验过程
1、修改proc.h
修改PCB,增加priority表示优先级,waitTime表示总等待时间,currentWait当前等待时间,runTime表示运行时间,currentRun表示当前运行时间,turnAroundTime表示总周转时间。
struct proc {
uint sz; // Size of process memory (bytes)
pde_t* pgdir; // Page table
char *kstack; // Bottom of kernel stack for this process
enum procstate state; // Process state
int pid; // Process ID
struct proc *parent; // Parent process
struct trapframe *tf; // Trap frame for current syscall
struct context *context; // swtch() here to run process
void *chan; // If non-zero, sleeping on chan
int killed; // If non-zero, have been killed
struct file *ofile[NOFILE]; // Open files
struct inode *cwd; // Current directory
char name[16]; // Process name (debugging)
int priority;
int waitTime;
int runTime;
int turnAroundTime;
int currentWait;
int currentRun;
};
2、修改proc.c
修改allocproc()
found:
p->state = EMBRYO;
p->pid = nextpid++;
p->priority = 15;
p->waitTime = 0;
p->runTime = 0;
p->turnAroundTime = 0;
p->currentWait = 0;
p->currentRun = 0;
修改scheduler()
void
scheduler(void)
{
struct proc *p;
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);
struct proc * tempProcess = 0;
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
if(p->state != RUNNABLE)
continue;
//遍历一次进程数组,找出优先级最高的进程
for(tempProcess = ptable.proc; tempProcess < &ptable.proc