操作系统实验 Lab4 xv6系统优先级调度

一、实验目的
将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
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值