CPU调度算法

两种进程调度算法:1)优先数调度;2)循环轮转调度

 

①本程序用两种算法对五个进程进行调度,每个进程可有三个状态,并假设初始状态为就绪状态。

②为了便于处理,程序中的某进程运行时间以时间片为单位计算。各进程的优先数或轮转时间数以及进程需运行的时间片数的初始值均由用户给定。

③在优先数算法中,优先数可以先取值为98,进程每执行一次,优先数减3CPU时间片数加1,进程还需要的时间片数减1。在轮转算法中,采用固定时间片(即:每执行一次进程,该进程的执行时间片数为已执行了2个单位),这时,CPU时间片数加2,进程还需要的时间片数减2,并排列到就绪队列的尾上。

④对于遇到优先数一致的情况,采用FIFO策略解决。

 

 

#include<stdio.h>

#include <dos.h>

#include<stdlib.h>

#include<conio.h>

#include<iostream.h>

#define P_NUM 5

#define P_TIME 50

enum state{

       ready,

       execute,

       block,

       finish

};

 

struct pcb{

       char name[4];

       int priority;

       int cputime;

       int needtime;

       int count;

       int round;

       state process;

       pcb * next;

};

pcb * get_process();

pcb * get_process(){

       pcb *q;

       pcb *t;

       pcb *p;

       int i=0;

       cout<<"input name and time"<<endl;

 

       while (i<P_NUM){

              q=(struct pcb *)malloc(sizeof(pcb));

              cin>>q->name;

              cin>>q->needtime;

              q->cputime=0;

              q->priority=P_TIME-q->needtime;

              q->process=ready;

              q->next=NULL;

              if (i==0){

                     p=q;

                     t=q;

              }

              else{

                     t->next=q;

                     t=q;

              }

              i++;

       }  //while

       return p;

}

void  display(pcb *p){

       cout<<"name"<<"    "<<"cputime"<<"    "<<"needtime"<<"    "<<"priority"<<"    "<<"state"<<endl;

       while(p){

              cout<<p->name;

              cout<<"        ";

              cout<<p->cputime;

              cout<<"          ";

              cout<<p->needtime;

              cout<<"          ";

              cout<<p->priority;

              cout<<"          ";

              switch(p->process){

                     case ready:cout<<"ready"<<endl;break;

                     case execute:cout<<"execute"<<endl;break;

                     case block:cout<<"block"<<endl;break;

                     case finish:cout<<"finish"<<endl;break;

              }

              p=p->next;

       }

}

 

int process_finish(pcb *q){

       int bl=1;

       while(bl&&q){

              bl=bl&&q->needtime==0;

              q=q->next;

       }

       return bl;

}

 

void cpuexe(pcb *q){

       pcb *t=q;

       int tp=0;

       while(q){

              if (q->process!=finish){

                     q->process=ready;

                     if(q->needtime==0){

                            q->process=finish;

                     }

              }

              if(tp<q->priority&&q->process!=finish){

                     tp=q->priority;

                     t=q;

              }

              q=q->next;

       }

       if(t->needtime!=0){

              t->priority-=3;

              t->needtime--;

              t->process=execute;

              t->cputime++;

       }

}

 

void priority_cal(){

       pcb * p;

       clrscr();

       p=get_process();

       int cpu=0;

       clrscr();

       while(!process_finish(p)){

              cpu++;

              cout<<"cputime:"<<cpu<<endl;

              cpuexe(p);

              display(p);

              sleep(2);

              clrscr();

       }

       printf("All processes have finished,press any key to exit");

       getch();

}

 

void display_menu(){

       cout<<"CHOOSE THE ALGORITHM:"<<endl;

       cout<<"1 PRIORITY"<<endl;

       cout<<"2 ROUNDROBIN"<<endl;

       cout<<"3 EXIT"<<endl;

}

pcb * get_process_round(){

       pcb *q;

       pcb *t;

       pcb *p;

       int i=0;

       cout<<"input name and time"<<endl;

 

       while (i<P_NUM){

              q=(struct pcb *)malloc(sizeof(pcb));

              cin>>q->name;

              cin>>q->needtime;

              q->cputime=0;

              q->round=0;

              q->count=0;

              q->process=ready;

              q->next=NULL;

              if (i==0){

                     p=q;

                     t=q;

              }

              else{

                     t->next=q;

                     t=q;

              }

              i++;

       }  //while

       return p;

}

 

void cpu_round(pcb *q){

       q->cputime+=2;

       q->needtime-=2;

       if(q->needtime<0) {

              q->needtime=0;

       }

       q->count++;

       q->round++;

       q->process=execute;

 

}

 

pcb * get_next(pcb * k,pcb * head){

       pcb * t;

       t=k;

       do{

        t=t->next;

       }

       while (t && t->process==finish);

 

 

       if(t==NULL){

              t=head;

 

              while (t->next!=k && t->process==finish){

                     t=t->next;

              }

       }

       return t;

}

void set_state(pcb *p){

       while(p){

              if (p->needtime==0){

                     p->process=finish;

 

              }

              if (p->process==execute){

                     p->process=ready;

              }

              p=p->next;

       }

}

void display_round(pcb *p){

       cout<<"NAME"<<"  "<<"CPUTIME"<<"  "<<"NEEDTIME"<<"  "<<"COUNT"<<"  "<<"ROUND"<<"  "<<"STATE"<<endl;

       while(p){

              cout<<p->name;

              cout<<"      ";

              cout<<p->cputime;

              cout<<"     ";

              cout<<p->needtime;

              cout<<"         ";

              cout<<p->count;

              cout<<"        ";

              cout<<p->round;

              cout<<"       ";

              switch(p->process){

                     case ready:cout<<"ready"<<endl;break;

                     case execute:cout<<"execute"<<endl;break;

                     case finish:cout<<"finish"<<endl;break;

              }

              p=p->next;

       }

}

 

void round_cal(){

       pcb * p;

       pcb * r;

       clrscr();

       p=get_process_round();

       int cpu=0;

       clrscr();

       r=p;

       while(!process_finish(p)){

              cpu+=2;

              cpu_round(r);

              r=get_next(r,p);

              cout<<"cpu "<<cpu<<endl;

              display_round(p);

              set_state(p);

              sleep(5);

              clrscr();

       }

}

 

void main(){

       display_menu();

       int k;

       scanf("%d",&k);

       switch(k){

                     case 1:priority_cal();break;

                     case 2:round_cal();break;

                     case 3:break;

                     display_menu();

                     scanf("%d",&k);

       }

       }

 

 

### CPU调度算法概述 CPU调度是操作系统中的核心功能之一,用于决定哪个进程可以占用处理器资源以及如何管理这些资源。常见的调度目标包括最大化吞吐量、最小化等待时间和提高公平性。 #### 1. FCFS(First-Come, First-Served) FCFS是一种简单的非抢占式调度算法,其中最先到达的进程会被优先执行。尽管其实现简单,但在某些情况下可能导致较长的平均等待时间[^4]。 ```python def fcfs(processes): current_time = 0 for process in processes: start_time = max(current_time, process['arrival']) finish_time = start_time + process['burst'] process['completion'] = finish_time current_time = finish_time ``` #### 2. SJF(Shortest Job First) SJF也是一种非抢占式的调度方法,其策略是最短的任务被优先安排运行。这种方法能够减少整体完成时间,但可能引发饥饿现象,即长时间等待的大任务无法得到处理。 #### 3. SRTN(Shortest Remaining Time Next) SRTN是对SJF的一种改进版本,采用抢占机制。如果新到来的任务所需的时间少于当前正在运行的任务剩余时间,则中断后者以执行前者。 #### 4. HRRN(Highest Response Ratio Next) HRRN通过计算响应比率来选择下一个要执行的任务,从而平衡了短任务的优势和长任务的需求。这种方式有助于缓解饥饿问题并提升系统的效率。 #### 5. RR(Round Robin) 在时间片轮转调度中,每个进程都被赋予固定大小的时间片段去访问CPU。一旦该时间段结束或者进程完成了自己的工作周期,控制权就会传递给下一个排队等候的进程[^3]。 ```python time_quantum = 2 # 时间片长度设定为2单位时间 while any(p['remaining'] > 0 for p in processes): for process in processes: if process['remaining'] > time_quantum: process['remaining'] -= time_quantum elif process['remaining'] > 0: process['remaining'] = 0 ``` #### 6. HPF(Highest Priority First) 此算法基于优先级的概念,“重要”的事情获得更高的优先级别以便更快地被执行。需要注意的是,在实际应用过程中可能会遇到相同优先级的情况,这时通常辅以其他标准来进行决策[^2]。 #### 7. MFQS(Multi-Level Feedback Queue Scheduler) 多级反馈队列调度器结合了几种不同的调度技术优点于一体。它设置了多个不同级别的队列,并允许动态调整进程所在的位置依据它们的行为特征。 ### 结论 每一种调度算法都有各自的优缺点及适用场景,具体选用哪一类取决于特定的应用需求和技术约束条件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值