Chinaunix首页 | 论坛 | 博客
  • 博客访问: 149881
  • 博文数量: 54
  • 博客积分: 2517
  • 博客等级: 少校
  • 技术积分: 540
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-13 18:52
文章分类
文章存档

2011年(2)

2010年(11)

2009年(41)

我的朋友

分类: LINUX

2009-09-13 19:45:58

最近闲来无事看了下linux内核调度算法部分代码写几个算法出来玩玩,过几天有空会把其他几个算法发上来
 
/*模拟实现LINUX进程调度的静态优先级算法和时间片轮转算法,当时间片用
完时采用降优先级处理,让低优先级进程先运行,当一轮调度用完时重新分配时间片。。*/
#include
#include
#include
 
 
#define RUN      1
#define SLEEP      0
#define READY      2
 
#define SIG_WAKE 25

struct OSPCB
{
 int PcbName ; /*进程名字*/
 int ReqCount;   /*进程执行计数*/
 int RunTime;    /*进程执行时间数*/
 int Prority;    /*进程优先级*/
 int PcbStatus;  /*进程状态*/
 int PcbTime;    /*进程时间片*/
 struct OSPCB* prev;
 struct OSPCB *next;
};

struct RunQueue  /*模拟CPU调度运行队列*/
{
 struct OSPCB *PointerHead; /*指向运行进程链表头*/
 int PcbNumber;    /*CPU每次调度计数器*/
};
struct SleepQueue /*模拟CPU调度睡眠队列*/
{
 struct OSPCB *PointerHead;/*指向睡眠进程链表头*/
 int PcbNumber; /*CPU每次调度计数器*/
};
 
//void InitPcb(struct OSPCB *pcb);
int Schedule(struct RunQueue *queue);
int *WakeUpPcb(struct SleepQueue *queue);

static void *SignallCallFunction(void);
 
int main(void)
{
 int i,ret;
 struct OSPCB *Newpcb,*p;
 struct ProcessQueue *NewQueue;
 int a[4][4] = {{1,1,0,1},{2,2,0,2},{3,3,0,3},{4,4,0,4}};

 NewQueue = (struct ProcessQueue *)malloc(sizeof(struct ProcessQueue));
 NewQueue->PointerHead = NULL;
 NewQueue->PcbNumber = 0;
 
 for(i = 0; i < 4;i++) /*进程初始化*/
 {
  Newpcb = (struct  OSPCB *)malloc(sizeof(struct OSPCB));
  Newpcb->PcbName   = a[i][0];
  Newpcb->ReqCount  = a[i][1];
  Newpcb->RunTime   = a[i][2];
  Newpcb->Prority   = a[i][3];
  Newpcb->PcbStatus = READY;
  Newpcb->PcbTime   = 3;

  InitPcb(Newpcb);
  
  if(NewQueue->PointerHead == NULL)
  {
   NewQueue->PointerHead = Newpcb;
  }else{
   p->next = Newpcb;
   Newpcb->prev = p;
  }
  
  p =  Newpcb;
  NewQueue->PcbNumber++;
 }
#if 0
 for(p = NewQueue->PointerHead; p != NULL; p = p->next)
 {
  printf("process name = %d\n",p->PcbName);
 }
#endif

 Schedule(NewQueue);/*进入进程调度*/
 return 0;
}
 

void InitPcb(struct OSPCB *pcb)
{
 pcb->prev = NULL; 
 pcb->next = NULL;
}
 
struct  OSPCB  *WakeUpPcb(struct SleepQueue *queue);
{
 
 struct OSPCB *pcb,PreRunPcb;
 
 PreRunPcb = queue->PointerHead;
 
 for(pcb = queue->PointerHead; pcb !=NULL; pcb = pcb->next)
 {
  if (pcb->Prority > PreRunPcb->Prority)
  {
   PreRunPcb = pcb;
  }
 }  
 return PreRunPcb;
}

int Schedule(struct  RunQueue *queue,struct SleepQueue *queue_t ) /*进程调度*/
{
 struct OSPCB *pcb,*pcb_t,*CurrRun;
 int value,SechNumber = 8;
#if  1  //模拟进程队了惴ㄐ
// printf("%s\n",__FUNCTION__);
 for(pcb = queue->PointerHead;pcb !=NULL;pcb = pcb->next)
 {
  if(pcb->PcbTime  == 0)
  {
//   pcb->Prority +=4;
   if(CurrRun == queue->PointerHead)
   {
    queue->PointerHead = CurrRun->next;
   }else if (CurrRun->next != NULL){
    CurrRun->prev->next = CurrRun->next;
    CurrRun->next->prev = CurrRun->prev;
   }else{
    CurrRun->prev->next = NULL;
   }
   
   if( queue_t->PointerHead == NULL)
   {
     queue_t->PointerHead = pcb;
   }
   for(pcb_t = queue_t->PointerHead; pcb_t->next != NULL; pcb_t = pcb_t->next)
   {
   }
   pcb_t ->next = pcb;
   pcb->PcbStatus = SLEEP;
   
   
  }
  pcb->PcbTime = 3;
 }
 
 while(queue->PointerHead != NULL)
 {
  for(pcb = queue->PointerHead;pcb !=NULL;pcb = pcb->next)
  {
   if(pcb == queue->PointerHead)
   {
    CurrRun = pcb;
   }else{
    if(CurrRun->Prority < pcb->Prority)
     CurrRun = pcb;    
   }
   CurrRun->PcbStatus = RUN;
  }
  SechNumber--;
  CurrRun->ReqCount--;
  CurrRun->PcbTime--;
  printf("present process  = %d CurrRun->ReqCount = %d\n",CurrRun->PcbName,CurrRun->ReqCount);
  pthread_create();
  if(CurrRun->PcbTime == 0)
  {
   CurrRun->Prority -=4;
  }
  if(CurrRun->ReqCount == 0)
  {
   if(CurrRun == queue->PointerHead)
   {
    queue->PointerHead = CurrRun->next;
   }else if (CurrRun->next != NULL){
    CurrRun->prev->next = CurrRun->next;
    CurrRun->next->prev = CurrRun->prev;
   }else{
    CurrRun->prev->next = NULL;
   }   
//   printf("Run process name = %d  Reqcount = %d Sechedule count = %d\n",CurrRun->PcbName,CurrRun->ReqCount,SechNumber);
  }
  
  if(SechNumber == 0)
  {
   Schedule(queue);
  }
 } 
#endif
 return 0;
}
阅读(1949) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

dolinux2009-11-09 09:37:28

int Schedule(struct RunQueue *queue); int *WakeUpPcb(struct SleepQueue *queue); 函数声明与下面的函数实现不相同。 在main函数中Schedule的调用时参数貌似也是有问题的。