进程队列

我们设计了进程队列,用于优化进程唤醒、休眠以及调度的效率.

数据结构

  • 在进程结构体里面添加队列的前向和后向.

  • 添加一个就绪队列和等待队列,队列的头是个空壳.本质上是一个进程结构体指针.是双向链表的链表首.

static接口

插入

对于等待队列和就绪队列,插入是一样的.都是从头部进行插入.

struct proc *prev = duilie;
struct proc *next = duilie;

prev->next = next->prev = p;
p->next = next;
p->prev = prev;

就绪队列删除

从头部取下一个元素.

static struct proc* delete_from_ready_queue(){
  struct proc *del = ready_queue->next;
  delete_a_proc(del);
  // print_queue_data();
  return del;
}

等待队列删除

遍历一遍整个队列,如果chan一样就从等待队列移出.

static void delete_from_waiting_queue(void* chan){
  struct proc* del = waiting_queue->next;
  
  while(del != waiting_queue){
    if(del->chan == chan){
      delete_a_proc(del);
      insert_into_ready_queue(del);
      break;
    }
    else{
      del = del->next;
    }
  }
}

delete_a_proc接口

按照双向链表的做法来执行操作.

static void delete_a_proc(struct proc* p){
  // printf("delete a proc from queue");
  struct proc *prev = p->prev;
  struct proc *next = p->next;
  
  p->next = NULL;
  p->prev = NULL;
  
  if(prev == NULL || next == NULL)
  {
      return ;
  }
  
  prev->next = next;
  next->prev = prev;
}

调用static接口

  • scheduler函数中需要调用删除就绪队列来获得第一个就绪队列.

  • sleep函数需要插入到睡眠队列

  • wakeup和wakeup1函数需要删除睡眠队列并插入到就绪队列中.

Last updated