Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1056485
  • 博文数量: 573
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 66
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-28 16:21
文章分类

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: LINUX

2015-12-04 09:54:15

模拟进程调度

  1. "code" class="cpp">#include   
  2. #include   
  3. #include   
  4. #include   
  5. /*进程控制块数据结构*/  
  6. typedef struct node {  
  7.     char name[10];/*进程名*/  
  8.     int prio;     /*进程优先级*/   
  9.     int round;    /*进程分配的时间片*/   
  10.     int cputime;  /*进程消耗的CUP时间*/  
  11.     int needtime; /*进程需要的CUP时间*/  
  12.     int count;    /*进程运行时间*/  
  13.      char state;   /*进程的状态:'R':运行,'W':等待,'F':结束*/  
  14.      struct node *next;/*指向下一个进程的指针*/       
  15. }PCB;  
  16. PCB *finish,*ready,*tail,*run;/*指向三个队列的队首的指针,tail为就绪队列的队尾指针*/  
  17. int N;/*定义进程的数目*/  
  18. /* 
  19. 函数功能: 将进程就绪队列中第一个放进就绪队列  
  20. 函数原型: void firstin(void) 
  21. */  
  22. void firstin(void)  
  23. {  
  24.     if(ready!=NULL){  
  25.             run=ready;  
  26.             ready=ready->next;  
  27.             run->state='R';  
  28.             run->next=NULL;  
  29.     }else{  
  30.             run=NULL;  
  31.     }  
  32. }  
  33. /* 
  34. 函数功能:输出进程信息的标题函数  
  35. 函数原型:void prt1(char a) 
  36. 函数参数:char a :a=='p'为优先级,=='r'为时间片轮转  
  37. */  
  38. void prt1(char a)  
  39. {  
  40.     if(toupper(a)=='P'){  
  41.         printf(" name  cputime  needtime  priority  state /n");  
  42.     }else{  
  43.         printf(" name  cputime  needtime  count  round  state /n");   
  44.     }      
  45. }  
  46. /* 
  47. 函数功能:输出单个进程信息的函数  
  48. 函数原型:void prt2(char a,PCB *p) 
  49. 函数参数:char a :a=='p'为优先级,=='r'为时间片轮转  
  50.           PCB *p 为指向待输出的进程控制块的指针      
  51. */  
  52. void prt2(char a,PCB *p)  
  53. {  
  54.     if(toupper(a)=='P'){  
  55.         printf("%-10s,%-10d,%-10d,%-10d,%-5c/n",p->name,p->cputime,p->needtime,p->prio,p->state);  
  56.     }else{  
  57.         printf("%-10s,%-10d,%-10d,%-10d,%-10d,%-5c/n",p->name,p->cputime,p->needtime,p->count,p->round,p->state);  
  58.     }  
  59. }  
  60.   
  61. /* 
  62. 函数功能:输出所有进程信息的函数  
  63. 函数原型:void prt(char algo) 
  64. 函数参数:char a :a=='p'为优先级,=='r'为时间片轮转  
  65. */  
  66. void prt(char algo)  
  67. {  
  68.     PCB *p;  
  69.     prt1(algo);  
  70.     if(run!=NULL){  
  71.         prt2(algo,run);  
  72.     }  
  73.     
  74.     p=ready;  
  75.     while(p!=NULL){  
  76.         prt2(algo,p);  
  77.         p=p->next;  
  78.     }  
  79.     
  80.     p=finish;  
  81.     while(p!=NULL){  
  82.         prt2(algo,p);  
  83.         p=p->next;  
  84.     }  
  85.     getchar();  
  86. }  
  87.    
  88. /* 
  89. 函数功能:优先级法调度将进程插入到就绪队列算法  
  90. 函数原型:void insert1(PCB *q) 
  91. 函数参数:PCB *q 待插入的队列进程控制块  
  92.           优先级越高,插入越靠前  
  93. */  
  94. void insert1(PCB *q)  
  95. {  
  96.     PCB *p,*s,*r; /*p,r用来控制就绪队列滚动,S指向插入的队列*/  
  97.     int b; /*b作为插入控制标志的*/   
  98.     s=q;  
  99.     p=ready;  
  100.     r=p;  
  101.     b=1;  
  102.     if(s->prio>=ready->prio){  
  103.         s->next=ready;  
  104.         ready=s;            
  105.     }else{  
  106.         while((p!=NULL)&&b){  
  107.             if(p->prio>=s->prio){  
  108.                 r=p;  
  109.                 p=p->next;  
  110.             }else{  
  111.                     b=0;  
  112.             }  
  113.         }   
  114.         s->next=p;  
  115.         r->next=s;  
  116.     }  
  117. }  
  118.   
  119. /* 
  120. 函数功能:时间片轮转算法调度将进程插入到就绪队列算法  
  121. 函数原型:void insert2(PCB *q) 
  122. 函数参数:PCB *q 待插入的队列进程控制块   
  123. */  
  124.  void insert2(PCB *q)  
  125.  {  
  126.     tail->next=q;  
  127.     tail=q;  
  128.     q->next=NULL;  
  129.  }  
  130. /* 
  131. 函数功能:采用优先级进程调度法时,进程初始化函数  
  132. 函数原型:void pcreate_task(char algo) 
  133. 函数参数:char algo:   
  134.  
  135. */  
  136. void pcreate_task(char algo)  
  137. {  
  138.     PCB *p;  
  139.     int i,time;  
  140.     char na[10];  
  141.     ready=NULL;  
  142.     finish=NULL;  
  143.     run=NULL;  
  144.     for(i=0;i
  145.         p=(PCB*)malloc(sizeof(PCB));  
  146.         printf("Enter the name of process/n");  
  147.         scanf("%s",na);  
  148.         printf("Enter the time of process/n");  
  149.         scanf("%d",&time);  
  150.         strcpy(p->name,na);  
  151.         p->cputime=0;  
  152.         p->needtime=time;  
  153.         p->state='W';  
  154.         p->prio=time;  
  155.     
  156.         if(ready==NULL){  
  157.             ready=p;  
  158.             ready->next=NULL;  
  159.         }else{  
  160.                 insert1(p);  
  161.         }  
  162.         printf("Output the waiting processes information/n");  
  163.         prt(algo);  
  164.     }  
  165.     firstin();  
  166. }  
  167.   
  168. /* 
  169. 函数功能:采用时间片轮转法进程调度法时,进程初始化函数  
  170. 函数原型:void rcreate_task(char algo) 
  171. 函数参数:char algo:  R 
  172.  
  173. */  
  174. void rcreate_task(char algo)  
  175. {  
  176.     PCB *p;  
  177.     int i,time;  
  178.     char na[10];  
  179.     ready=NULL;  
  180.     finish=NULL;  
  181.     run=NULL;  
  182.     for(i=0;i
  183.         p=(PCB*)malloc(sizeof(PCB));  
  184.         printf("Enter the name of process/n");  
  185.         scanf("%s",na);  
  186.         printf("Enter the time of process/n");  
  187.         scanf("%d",&time);  
  188.         strcpy(p->name,na);  
  189.         p->cputime=0;  
  190.         p->needtime=time;  
  191.         p->count=0;  
  192.         p->state='W';  
  193.         p->round=2;  
  194.         if(ready!=NULL){  
  195.             insert2(p);  
  196.         }else{  
  197.                 p->next=ready;  
  198.                 ready=p;  
  199.                 tail=p;  
  200.         }  
  201.         printf("Output the waiting processes information/n");  
  202.         prt(algo);  
  203.     }  
  204.     run=ready;  
  205.     ready=ready->next;  
  206.     run->state='R';  
  207.        
  208. }  
  209. /* 
  210. 函数功能:采用优先级进程调度法时,进程调度函数  
  211. 函数原型:void priority(char algo) 
  212. 函数参数:char algo:进程调度类别标志:'P'优先级 'R'时间片轮转  
  213.  
  214. */  
  215. priority(char algo)  
  216. {  
  217.     while(run!=NULL){  
  218.         run->cputime+=1;  
  219.         run->needtime-=1;  
  220.         run->prio-=3;  
  221.         if(run->needtime==0){  
  222.                 run->next=finish;  
  223.                 finish=run;  
  224.             run->state='F';  
  225.             run=NULL;  
  226.                 firstin();          
  227.         }else{  
  228.                 if((ready!=NULL)&&(run->prioprio)){  
  229.                     run->state='W';  
  230.                     insert1(run);  
  231.                     run=NULL;  
  232.                     firstin();  
  233.                 }  
  234.         }  
  235.    
  236.         prt(algo);  
  237.      }  
  238. }  
  239.   
  240. /* 
  241. 函数功能:采用时间片轮转法进程调度法时,进程调度函数  
  242. 函数原型:void roundrun(char algo) 
  243. 函数参数:char algo:  R 
  244. */  
  245. void roundrun(char algo)  
  246. {  
  247.     while(run!=NULL){  
  248.         run->cputime=run->cputime+1;  
  249.         run->needtime=run->needtime-1;  
  250.         run->count=run->count+1;  
  251.         if(run->needtime==0){  
  252.                 run->next=finish;  
  253.             finish=run;  
  254.             run->state='F';  
  255.             run=NULL;  
  256.             if(ready!=NULL){  
  257.                     firstin();  
  258.                 }           
  259.         }else{  
  260.                 if(run->count==run->round){  
  261.                     run->count=0;   
  262.                     if(ready!=NULL){          
  263.                         run->state='W';  
  264.                         insert2(run);  
  265.                         firstin();  
  266.                 }  
  267.                 }  
  268.         }  
  269.         prt(algo);  
  270.     }  
  271. }  
  272.   
  273. /*main 函数*/  
  274.   
  275. int main()  
  276. {  
  277.     char algo;  
  278.     printf("Choose the type of attemper P:priority R:timeround/n");  
  279.     scanf("%c",&algo);  
  280.     printf("Please enter the number of processes N:/n");  
  281.     scanf("%d",&N);  
  282.     if((algo=='P')||(algo=='p')){  
  283.         pcreate_task(algo);  
  284.         priority(algo);  
  285.      
  286.     }else if((algo=='r')||(algo=='R')){  
  287.         rcreate_task(algo);  
  288.         roundrun(algo);  
  289.      
  290.     }  
  291. }  

  292.   

  293.   
  294.   
  295.      
阅读(362) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~