操作系统处理机调度算法的实现c语言源代码
计算机操作系统课程设计源代码《处理机调度算法的实现源代码》
《处理机调度算法的实现》源代码#include "stdio.h"#include <stdlib.h>#include "time.h"#include <math.h>typedef struct{int id;int reach;float service;int prior;int finish;float turnover;float cum_turnover;}process;process p[20];int sort[20];int wait[20];int n;int alltime=0;char c;void in(){printf("请输入要建立的进程数:\n");scanf("%d",&n);int i;int j;int temp;srand(time(0));for(i=0;i<n;i++){printf("\n进程%d号:\n",i);printf("进程uid:");scanf("%d",&p[i].id);p[i].reach=rand()%10+1;printf("到达时间:%d\n",p[i].reach);p[i].service=rand()%10+1;printf("服务时间:%f\n",p[i].service);if(c=='c'){p[i].prior=rand()%10+1;printf("优先权:%d\n",p[i].prior);}else p[i].prior=0;p[i].finish=0;p[i].turnover=p[i].service;p[i].cum_turnover=p[i].prior;sort[i]=i;alltime=alltime+p[i].service;}for(i=1;i<n;i++){for(j=0;j<n-1;j++){if(p[sort[j]].reach>p[sort[j+1]].reach){temp=sort[j];sort[j]=sort[j+1];sort[j+1]=temp;}}}alltime=alltime+p[sort[0]].reach;}void out(){int i;if(c=='c'){printf("进程uid\t到达时间\t服务时间\t优先权\t完成时间\t周转时间\t带权周转时间\n");for(i=0;i<n;i++)printf("%d\t%d\t\t%f\t%d\t%d\t\t%f\t%f\n",p[i].id,p[i].reach,p[i].service,p[i].prior,p[i].finish,p[i].turnover,p[i].cum_turnover );}else {printf("进程uid\t到达时间\t服务时间\t完成时间\t周转时间\t带权周转时间\n");for(i=0;i<n;i++)printf("%d\t%d\t\t%f\t%d\t\t%f\t%f\n",p[i].id,p[i].reach,p[i].service,p[i].finish,p[i].turnover,p[i].cum_turnover);}}void timeslice(){int q;int i;int j;int front=-1;int rear=-1;int t=1;int v;printf("\n请输入时间片:\n");scanf("%d",&q);for(i=0;i<alltime;i++){for(j=0;j<n;j++){if((i==p[sort[j]].reach-1)&&(front!=(rear+1)%20)){rear=(rear+1)%20;wait[rear]=sort[j];sort[j]=-1;}}if((front!=rear)||(front>-1)){if(t==1){front=(front+1)%20;v=wait[front];}if(t<=q){p[v].turnover--;if(p[v].turnover==0){t=0;p[v].finish=i+2;p[v].turnover=p[v].finish-p[v].reach;p[v].cum_turnover=p[v].turnover/p[v].service;if(front==rear){front=-1;rear=-1;}}if((t==q)&&(p[v].turnover!=0)){rear=(rear+1)%20;wait[rear]=v;t=0;}t++;}}}}//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//短作业优先void sjf(){int top=-1;//作用于wait[]int i;int j;int k;int l;int temp;int v=-1;//处理机正在进行处理的进程号for(i=0;i<=alltime;i++){for(j=0;j<n;j++){//将到达的进程放进等待栈if((i==p[sort[j]].reach-1)&&(top<19)){top++;wait[top]=sort[j];for(k=1;k<=top;k++){//将等待栈按进程服务时间从大到小进行排列for(l=0;l<top;l++){if(p[wait[l]].service<p[wait[l+1]].service){temp=wait[l];wait[l]=wait[l+1];wait[l+1]=temp;}//相同服务时间的先到达的先处理if(p[wait[l]].service==p[wait[l+1]].service){if(p[wait[l]].reach < p[wait[l+1]].reach){temp=wait[l];wait[l]=wait[l+1];wait[l+1]=temp;}}}}}}if((v==-1)&&(top>=0)){//出栈一个进程v=wait[top];top--;}if(v>-1){//处理机运行,服务时间自减、这里的"turnover"指的是服务时间p[v].turnover--;if(p[v].turnover==0){p[v].finish=i+2;p[v].turnover=p[v].finish-p[v].reach;p[v].cum_turnover=p[v].turnover/p[v].service;v=-1;}}}} //动态优先级void fpf(){int top=-1;//作用于wait[]int i,j,k,l,temp;int v=-1;//处理机正在进去处理的进程号for(i=0;i<=alltime;i++){//优先权加1if((i>0)&&(top>-1)){for(j=0;j<=top;j++){p[wait[j]].cum_turnover++;}}for(j=0;j<n;j++){//将到达的进程放进等待栈if((i==p[sort[j]].reach-1)&&(top<19)){top++;wait[top]=sort[j];for(k=1;k<=top;k++){//将等待栈按进程优先权从小到大进行排列for(l=0;l<top;l++){if(p[wait[l]].cum_turnover>p[wait[l+1]].cum_turnover){temp=wait[l];wait[l]=wait[l+1];wait[l+1]=temp;}//相同优先权的先到达的先处理if(p[wait[l]].cum_turnover==p[wait[l+1]].cum_turnover){if(p[wait[l]].reach < p[wait[l+1]].reach){temp=wait[l];wait[l]=wait[l+1];wait[l+1]=temp;}}}}}}if((v==-1)&&(top>=0)){//出栈一个进程v=wait[top];top--;}if(v>-1){//处理机运行,服务时间自检,这里的"turnver"指的是服务时间p[v].turnover--;if(p[v].turnover==0){p[v].finish=i+2;p[v].turnover=p[v].finish-p[v].reach;p[v].cum_turnover=p[v].turnover/p[v].service;v=-1;}}}}//主函数int main(){while(1){printf("\n\n请选择调度算法\na:时间片轮法\nb:短作业优先\nc:动态优先级\n");scanf("%s",&c);in();switch(c){case 'a':timeslice();break;case 'b':sjf();break;case 'c':fpf();break;}out();}return 0;}。
操作系统进程调度算法(c语言实现)
操作系统进程调度算法(c语⾔实现)进程调度算法⼀、先来先服务(FCFS)基本思想:先到达的进程先进⼊就绪队列,先进⾏调度的原则。
⾮抢占⽅式。
⼆、短作业优先(SJF)基本思想:根据进程中的执⾏时间,选取执⾏时间最短的作业优先调度;可有抢占或⾮抢占⽅式。
三、优先权⾼者优先(HPF)基本思想:系统根据作业的优先权进⾏作业调度,每次选取优先权⾼的作业优先调度。
作业的优先权通常⽤⼀个整数表⽰,也叫做优先数。
可有抢占或⾮抢占⽅式。
四、时间⽚轮转(RR)基本思想:系统将所有的就绪进程按先来先服务的原则,排成⼀个队列,每次调度时,把CPU分配给队⾸进程,并令其执⾏⼀个时间⽚。
时间⽚结束之后,将该进程加到就绪队列队尾;然后再把处理机分配给就绪队列中新的⾸进程。
各程序的实现算法(1)FCFS先来先服务算法思想:①⾸先将输⼊的进程放⼊⼀个进程数组中,然后根据进程的到达时间进⾏排序(冒泡排序)。
将最先到达的进程放⼊进程就绪队列中。
②当队列不空时,从队头取出⼀个进程来执⾏,直⾄此进程执⾏完,并将在此进程执⾏期间到达的进程依次加⼊进程就绪队列。
③如果队列为空,但进程数组中仍存在未到达的进程,这时将要到达进程加⼊进程就绪队列。
void FCFS(program pro[],int num){printf("进程到达时间服务时间开始时间完成时间周转时间带权周转时间\n");sortWithEnterTime(pro,num);//按照到达顺序排序programQueue* queue =(programQueue*)malloc(sizeof(programQueue));Queueinit(queue);//初始化进程就绪队列EnterQueue(queue,&pro[0]);//将第⼀个进程放⼊队列int time = pro[0].enter_time;int pronum=1;//记录当前已进⼊的进程float sum_T_time =0,sum_QT_time =0;while(queue->size>0){//当队列不为空program* curpro =poll(queue);//从进程队列中取出进程if(time<curpro->enter_time)//如果此进程的进⼊时间⼤于此时的时间,需要将时间转换到此进程的到达时间time = curpro->enter_time;int done_time = time+curpro->running_time;//记录完成时间int T_time = done_time - curpro->enter_time;//记录周转时间sum_T_time += T_time;float QT_time = T_time /(curpro->running_time+0.0);//记录带权周转sum_QT_time += QT_time;for(int tt = time;tt<=done_time&&pronum<num;tt++){//模拟进程的执⾏过程if(tt>=pro[pronum].enter_time){//程序执⾏时有程序到达则进⼊程序队列EnterQueue(queue,&pro[pronum]);pronum++;}}printf("%s\t%d\t%d\t%d\t%d\t%d\t%.2f\n",curpro->name,curpro->enter_time,curpro->running_time,time,done_time,T_time,QT_time);//输出结果time += curpro->running_time;if(queue->size==0&&pronum<num){//防⽌出现前⼀个进程执⾏完到下⼀个进程到达之间⽆进程进⼊EnterQueue(queue,&pro[pronum]);pronum++;}}printf("平均周转时间为%.2f\t平均带权周转时间为%.2f\n",sum_T_time/(num+0.0),sum_QT_time/(num+0.0));}(2)短作业优先(SJF)算法思想:①⾸先也是按进程的到达时间进⾏排序。
操作系统实验处理机调度C语言实现
#include<stdio.h>#include<stdlib.h>#include <conio.h>#include<math.h>#define N 20#define MAX 100typedef struct PCB //pcb进程控制块定义{int num[N]; //进程序号char name[10]; //进程名char state; //进程状态int tijiaotime; //进程到达时间int runtime; //进程开始时间int finishtime; //进程结束时间int needtime; //服务时间int pro;//进程优先级struct PCB *next; //链接指针指向下个作业的}pcb;struct PCB *head_input;struct PCB *head_run;struct PCB *head_run_pre;unsigned long current; //记录系统当前时间的变量int time=10000,n; //计时器pcb *head=NULL,*p,*q;void getInfo() //创建进程{int num;printf("\n请输入要建立的进程个数:");scanf("%d",&n);for(num=0;num<n;num++){p=(pcb *)malloc(sizeof(pcb));if(head==NULL) {head=p;q=p;}printf("依次输入:\n进程号进程名到达时间服务时间 \n");scanf("%s\t%s\t%d\t%d",&p->num,&p->name,&p->tijiaotime,&p->needtime);if(p->tijiaotime < time) time=p->tijiaotime;q->next=p;p->runtime=0;p->finishtime=0;p->next=NULL;p->state='W';q=p;}}// *********************1.先来先服务调度算法******************************* void run_fcfo(pcb *p1)//定义先来先到服务的算法{time = p1->tijiaotime > time? p1->tijiaotime:time;p1->runtime=time;printf("\n现在时间是%d,开始运行进程%s\n",time,p1->name);time+=p1->needtime;p1->state='F';p1->finishtime=time;printf("进程名开始时间所需时间结束时间\n");printf("%s %d %d %d ",p1->name,p1->runtime,p1->needtime,p1->finishtime); }void fcfo()//定义运行进程函数{int i,j,t;for(j=0;j<n;j++){p=head;t=10000;for(i=0;i<n;i++) //找到当前未完成的进程{if(p->tijiaotime<t && p->state=='W'){t=p->tijiaotime;q=p; //标记当前未完成的进程}p=p->next;}run_fcfo(q);}}// ************************2.优先级调度服务算法************************************int readydata(){ //建立就绪队列if(head_input->next==NULL){return 0;}struct PCB *p1=head_input->next,*pmax,*p2;int maxpro=0xffff;pmax=p1;p2=head_input;while(p1!=NULL){if(p1->pro<maxpro){maxpro=p1->pro;head_run_pre=p2;pmax=p1;}p2=p1;p1=p1->next;}head_run=pmax;head_run_pre->next=head_run->next;return 1;}void runprocess() //运行进程函数{head_run->runtime-=10;head_run->pro++;struct PCB *p1,*p2;printf("时间片的大小 %d",current);current+=10;printf(" %s 开始\n",head_run->name);printf("时间片的大小 %d",current);printf(" %s 结束\n",head_run->name);if(head_run->runtime<=0){//判断进程是否运行结束}else{p1=head_input;p2=head_input->next;p1->next=head_run;head_run->next=p2;}}int readyprocess(){while(1){if(readydata()==0)return 0;else runprocess();}}void Init(){head_input=new PCB;head_input->next=NULL;current=0;int numpro;printf("请重新输入要建立的进程个数:");scanf("%d",&numpro);printf("请依次输入进程名运行时间优先级\n");for(int i=0;i<numpro;i++){struct PCB *p1=new PCB;scanf("%s",p1->name);scanf("%d",&p1->runtime);scanf("%d",&p1->pro);p1->state='C';p1->next=NULL;struct PCB *p2=head_input->next;head_input->next=p1;p1->next=p2;}}// ************************3.时间片轮转调度服务算法************************************ void shijianpian(){ int b,i,X,t,k;int a[MAX];//存放进程的剩余时间int cnt[MAX];//存放进程调度次数printf("请输入进程数:");scanf("%d",&X);printf("\n请输入时间片t大小:");scanf("%d",&t);printf("\n请依次输入各个进程的服务时间");for(i=0;i<X;i++){scanf("%d",&a[i]);cnt[i]=0;}printf("被调度进程\t进程调度次数 \t本次运行时间结果\t剩余时间\n");k=1;while(k){for(i=0;i<X;i++){if(a[i]!=0)if(a[i]>=t){a[i]-=t;b+=t;cnt[i]=cnt[i]+1;printf("\n\t%d\t\t%d\t\t%d\t\t%d",i+1,cnt[i],b,a[i]);}else{b=b+a[i];cnt[i]=cnt[i]+1;a[i]=0;printf("\n\t%d\t\t%d\t\t%d\t\t%d",i+1,cnt[i],b,a[i]);}else continue;}for(i=0;i<X;i++)if(a[i]!=0){ k=1;break;}else continue;if(i>=X)k=0;}}void main(){printf(" *******************************");printf("\n 1. 按先来先到服务调度的算法模拟\n"); printf(" *******************************");getInfo();fcfo();printf("\n *******************************");printf("\n 2. 按优先级调度的算法模拟\n");printf("\n *******************************\n"); Init();readyprocess();printf("\n *******************************");printf("\n 3. 按时间片轮转调度的算法模拟\n");printf(" *******************************\n"); shijianpian();printf(" \n");}。
调度算法C语言实现
调度算法C语言实现调度算法是操作系统中的重要内容之一,它决定了进程在系统中的运行方式和顺序。
本文将介绍两种常见的调度算法,先来先服务(FCFS)和最短作业优先(SJF),并用C语言实现它们。
一、先来先服务(FCFS)调度算法先来先服务(FCFS)调度算法是最简单的调度算法之一、它按照进程到达的先后顺序进行调度,即谁先到达就先执行。
实现这个算法的关键是记录进程到达的顺序和每个进程的执行时间。
下面是一个用C语言实现先来先服务调度算法的示例程序:```c#include <stdio.h>//进程控制块结构体typedef structint pid; // 进程IDint arrivalTime; // 到达时间int burstTime; // 执行时间} Process;int maiint n; // 进程数量printf("请输入进程数量:");scanf("%d", &n);//输入每个进程的到达时间和执行时间Process process[n];for (int i = 0; i < n; i++)printf("请输入进程 %d 的到达时间和执行时间:", i);scanf("%d%d", &process[i].arrivalTime,&process[i].burstTime);process[i].pid = i;}//根据到达时间排序进程for (int i = 0; i < n - 1; i++)for (int j = i + 1; j < n; j++)if (process[i].arrivalTime > process[j].arrivalTime) Process temp = process[i];process[i] = process[j];process[j] = temp;}}}//计算平均等待时间和平均周转时间float totalWaitingTime = 0; // 总等待时间float totalTurnaroundTime = 0; // 总周转时间int currentTime = 0; // 当前时间for (int i = 0; i < n; i++)if (currentTime < process[i].arrivalTime)currentTime = process[i].arrivalTime;}totalWaitingTime += currentTime - process[i].arrivalTime;totalTurnaroundTime += (currentTime + process[i].burstTime) - process[i].arrivalTime;currentTime += process[i].burstTime;}//输出结果float avgWaitingTime = totalWaitingTime / n;float avgTurnaroundTime = totalTurnaroundTime / n;printf("平均等待时间:%f\n", avgWaitingTime);printf("平均周转时间:%f\n", avgTurnaroundTime);return 0;```以上程序实现了先来先服务(FCFS)调度算法,首先根据进程的到达时间排序,然后依次执行每个进程,并计算总等待时间和总周转时间。
操作系统进程调度C语言代码
操作系统进程调度C语言代码#include <stdio.h>#define MAX 20//进程控制块typedef struct PCBchar name[10]; // 进程名int AT; // 到达时间int BT; // 服务时间int Pri; // 优先数int FT; // 完成时间int WT; //等待时间int RT; // 响应时间int position; // 第几号进程int flag; // 用来判断进程是否执行过}PCB;//进程调度void schedule(PCB a[], int n, int alg)int i, j, k, flag, temp;int count = 0;int pri_max = 0;float ATAT = 0.0;float AWT = 0.0;float ART = 0.0;PCBt;//各种算法的调度if (alg == 1)printf("采用先来先服务调度:\n"); //根据到达时间执行for (i = 0; i < n; i++)for (j = i + 1; j < n; j++)if (a[i].AT > a[j].AT)t=a[i];a[i]=a[j];a[j]=t;}//按到达时间依次执行for (i = 0; count != n; i++)for (j = 0; j < n; j++)//查找第一个到达时间小于等于当前时间的进程if (a[j].AT <= i && a[j].flag == 0)//记录运行时间a[j].BT--;//如果运行完成,记录完成时间、等待时间、响应时间if (a[j].BT == 0)a[j].FT=i+1;a[j].WT = a[j].FT - a[j].AT - a[j].Pri;a[j].RT=a[j].WT;a[j].flag = 1;count++;}elsebreak;}}}else if (alg == 2)printf("采用最短服务时间优先(非抢占)调度:\n");for (i = 0; count != n; i++)//找出服务时间最短的进程,并将其放置到最前面for (j = 0; j < n; j++)。
计算机操作系统课程设计源代码《通用处理机调度演示程序源代码》
《通用处理机调度演示程序源代码》//短作业优先算法#define MaxNum 100#include <stdio.h>#include <stdlib.h>#include <string.h>struct Process_struct{int Number; //进程编号char Name[MaxNum]; //进程名称int ArrivalTime; //到达时间int ServiceTime; //开始运行时间int FinishTime; //运行结束时间int WholeTime; //运行时间int run_flag; //调度标志int order; //运行次序double WeightWholeTime; //周转时间double AverageWT_FCFS,AverageWT_SJF; //平均周转时间double AverageWWT_FCFS,AverageWWT_SJF; //平均带权周转时间}Process[MaxNum];int N; //实际进程个数int SJF(); //短作业优先int SJF(){ //短作业优先算法int temp_time=0; //当期那时间int i=0,j;int number_schedul,temp_counter; //进程编号,当前已执行进程个数float run_time;run_time=Process[i].WholeTime;j=1;while((j<N)&&(Process[i].ArrivalTime==Process[j].ArrivalTime)) //判断是否有两个进程同时到达{if(Process[j].WholeTime<Process[i].WholeTime){run_time=Process[i].WholeTime;i=j;}j++;}//查找下一个被调度的进程//对找到的下一个被调度的进程求相应的参数number_schedul=i;Process[number_schedul].ServiceTime=Process[number_schedul].ArrivalTim e;Process[number_schedul].FinishTime=Process[number_schedul].ServiceTime +Process[number_schedul].WholeTime;Process[number_schedul].run_flag=1;temp_time=Process[number_schedul].FinishTime;Process[number_schedul].order=1;temp_counter=1;while(temp_counter<N){for(j=0;j<N;j++){if((Process[j].ArrivalTime<=temp_time)&&(!Process[j].run_flag)){run_time=Process[j].WholeTime;number_schedul=j;break;}}for(j=0;j<N;j++){if((Process[j].ArrivalTime<=temp_time)&&(!Process[j].run_flag))if(Process[j].WholeTime<run_time){run_time=Process[j].WholeTime;number_schedul=j;}}//查找下一个被调度的进程//对找到的下一个被调度的进程求相应的参数Process[number_schedul].ServiceTime=temp_time;Process[number_schedul].FinishTime=Process[number_schedul].ServiceTime +Process[number_schedul].WholeTime;Process[number_schedul].run_flag=1;temp_time=Process[number_schedul].FinishTime;temp_counter++;Process[number_schedul].order=temp_counter;}return 0;}int Pinput(); //进程参数输入int Poutput(); //调度结果输出int Pinput() //进程参数输入{int i;printf("请输入进程个数:\n");scanf("%d",&N);for(i=0;i<N;i++){printf("----------------------------------------\n");printf("请输入一个进程:\n",i+1);printf("请输入进程名称:\n");scanf("%s",Process[i].Name);printf("请输入到达时间:\n");scanf("%d",&Process[i].ArrivalTime);printf("请输入服务时间:\n");scanf("%d",&Process[i].WholeTime);Process[i].ServiceTime=0;Process[i].FinishTime=0;Process[i].WeightWholeTime=0;Process[i].order=0;Process[i].run_flag=0;}return 0;}int Poutput() //调度结果输出{int i;float turn_round_time=0,f1,w=0;printf(" 进程名称到达时间运行时间开始运行时间结束时间执行顺序周转时间带权周转时间\n");for(i=0;i<N;i++){Process[i].WeightWholeTime=Process[i].FinishTime-Process[i].ArrivalTime;f1=Process[i].WeightWholeTime/Process[i].WholeTime;turn_round_time+=Process[i].WeightWholeTime;w+=f1;printf("时刻%d :",Process[i].ServiceTime,Process[i].Name);printf(" %s %d %d %d %d %d %f %f\n",Process[i].Name,Process[i].ArrivalTime,Process[i].WholeTime,Process[i].Ser viceTime,Process[i].FinishTime,Process[i].order,Process[i].WeightWholeTime,f1);}printf("average_turn_round_timer=%f\n",turn_round_time/N);printf("weight_average_turn_round_timer=%f\n",w/N);return 0;}//时间片轮转算法typedef struct node{char name[20];int round;int cputime;int arrivetime;int needtime;char state;int count;struct node *next;}PCB;PCB *ready=NULL,*run1=NULL,*finish=NULL; int num,i;void GetFirst();void Output();void InsertTime(PCB *in);void InsertFinish(PCB *in);void TimeCreate();void RoundRun();void GetFirst() /*取得第一个就绪队列节点*/ {run1 = ready;if(ready!=NULL){run1 ->state = 'R';ready = ready ->next;run1 ->next = NULL;}void Output() /*输出队列信息*/{PCB *p;p = ready;printf("进程名\tcpu时间\t需要时间\t进程状态\t计数器\n");while(p!=NULL){printf("%s\t%d\t%d\t\t%c\t\t%d\n",p->name,p->cputime,p->needtime,p->stat e,p->count);p = p->next;}p = finish;while(p!=NULL){printf("%s\t%d\t%d\t\t%c\t\t%d\n",p->name,p->cputime,p->needtime,p->stat e,p->count);p = p->next;}p = run1;while(p!=NULL){printf("%s\t%d\t%d\t\t%c\t\t%d\n",p->name,p->cputime,p->needtime,p->stat e,p->count);p = p->next;}}void InsertTime(PCB *in) /*将进程插入到就绪队列尾部*/{PCB *fst;fst = ready;if(ready == NULL){in->next = ready;ready = in;}else{while(fst->next != NULL){fst = fst->next;}in ->next = fst ->next;fst ->next = in;}}void InsertFinish(PCB *in) /*将进程插入到完成队列尾部*/ {PCB *fst;fst = finish;if(finish == NULL){in->next = finish;finish = in;}else{while(fst->next != NULL){fst = fst->next;}in ->next = fst ->next;fst ->next = in;}}void TimeCreate() /*时间片输入函数*/{PCB *tmp;int i;printf("输入进程名字到达时间进程所需时间时间片大小:\n");for(i = 0;i < num; i++){if((tmp = (PCB *)malloc(sizeof(PCB)))==NULL){perror("malloc");exit(1);}scanf("%s %d %d %d",tmp->name,&(tmp->arrivetime),&(tmp->needtime),&(tmp ->round));getchar();tmp ->cputime = 0;tmp ->state ='W';// tmp->prio=0;tmp ->count = 0;InsertTime(tmp);}}void RoundRun() /*时间片轮转调度算法*/{int flag = 1;GetFirst();while(run1 != NULL){Output();while(flag){run1->cputime++ ;run1->needtime--;run1->count++;if(run1->needtime == 0){run1 ->state = 'F';InsertFinish(run1);flag = 0;}else if(run1->count == run1->round){run1->state = 'W';run1->count=0;InsertTime(run1);flag = 0;}}flag = 1;GetFirst();}}//先来先服务算法typedef struct PCB //定义进程控制块{char ID[3]; //进程号char name[10]; //进程名char state; //运行状态int arrivetime; //到达时间int starttime; //进程开始时间int finishtime; //进程结束时间int servicetime; //服务时间float turnaroundtime;//周转时间float weightedturnaroundtime;//带权周转时间struct PCB *next; //指向下个进程}pcb;int time; //计时器int n; //进程个数pcb *head1=NULL,*p,*q; //进程链表指针void run_fcfs(pcb *p1) //运行未完成的进程{time = p1->arrivetime > time? p1->arrivetime:time;p1->starttime=time;printf("\n现在时间是%d,开始运行作业%s\n",time,p1->name);time+=p1->servicetime;p1->state='T';p1->finishtime=time;p1->turnaroundtime=p1->finishtime-p1->arrivetime;p1->weightedturnaroundtime=p1->turnaroundtime/p1->servicetime; printf("ID 到达时间开始时间服务时间完成时间周转时间带权周转时间\n"); printf("%s%6d%10d%10d%8d%10.1f%10.2f\n",p1->ID,p1->arrivetime,p1->starttime,p1->servicetime,p1->finishtime, p1->turnaroundtime,p1->weightedturnaroundtime);}void fcfs() //找到当前未完成的进程{int i,j;p=head1;for(i=0;i<n;i++){if(p->state=='F'){q=p; //标记当前未完成的进程run_fcfs(q);}p=p->next;}}void getInfo() //获得进程信息并创建进程{int num;printf("\n作业个数:");scanf("%d",&n);for(num=0;num<n;num++){p=(pcb *)malloc(sizeof(pcb));printf("依次输入:\nID 进程名到达时间服务时间\n");scanf("%s\t%s\t%d\t%d",&p->ID,&p->name,&p->arrivetime,&p->servicetime); if(head1==NULL) {head1=p;q=p;time=p->arrivetime;}if(p->arrivetime < time) time=p->arrivetime;q->next=p;p->starttime=0;p->finishtime=0;p->turnaroundtime=0;p->weightedturnaroundtime=0;p->next=NULL;p->state='F';q=p;}}//静态优先权优先算法int SequenceNumber=1; //进程编号int ProcessAmount; //进程数量int StartCount=0; //调入内存进程计数struct PCB1{ //进程控制块int No; //进程号char name[16]; //进程名int enterMemoryTime;//进入内存时间int serviceTime; //服务时间int priority; //优先级struct PCB1 *next;};struct PCB1 *head,*thisP,*newP; //动态接收进程及详细struct PCB1 *readyHead; //动态存储就绪队列//函数声明int getch();int timeOfData(FILE *);void ListAllPCB();void Menu();void printPCB(struct PCB1 *,int,int,int,double);void printPCBP(struct PCB1 *);void printField();void printFieldP();void release(struct PCB1 *);void releaseR(struct PCB1 *);void setNewPCB(char *,int,int,int);struct PCB1 * runTheProcess();void newPCB(){ //建立PCBnewP=(struct PCB1 *)malloc(sizeof(struct PCB1));if(head==NULL){ //判断头节点是否为空head=newP; //为空,头节点指向新开辟的内存}else{thisP=head;while(thisP->next!=NULL){thisP=thisP->next;}thisP->next=newP; //遍历单链表,找到最后一个元素}thisP=newP;thisP->No=SequenceNumber;SequenceNumber++;printf("进程号%d\n",thisP->No);printf("输入进程名:");scanf("%s",thisP->name);printf("输入优先级:");scanf("%d",&thisP->priority);printf("输入进入内存时间:");scanf("%d",&thisP->enterMemoryTime);printf("输入服务时间:");scanf("%d",&thisP->serviceTime);thisP->next=NULL;}void buildProcess(){ //创建进程int i=0;printf("输入进程数量:");scanf("%d",&ProcessAmount);while(i<ProcessAmount){newPCB();i++;}}void readyList(struct PCB1 *pcb){ //单链表的形式创建就绪队列newP=(struct PCB1 *)malloc(sizeof(struct PCB1));if(readyHead==NULL){readyHead=newP;}else{thisP=readyHead;while(thisP->next!=NULL){thisP=thisP->next;}thisP->next=newP;}thisP=newP;strcpy(thisP->name,pcb->name);thisP->No=pcb->No;thisP->priority=pcb->priority;thisP->enterMemoryTime=pcb->enterMemoryTime;thisP->serviceTime=pcb->serviceTime;thisP->next=NULL;}struct PCB1 * callMemory(){ //调入内存,返回调入内存的链表节点int at; //到达时间struct PCB1 *markP;if(head==NULL){printf("程序没有找到。
进程调度算法实现代码(操作系统)
进程调度算法实现//数据:进程,队列结构//处理流程://1 初始化--进程队列结构(包括:就绪队列,等待队列,运行队列)等必要的数据结构init(); //2 进入无限循环,反复调度队列#define MAX 5#include<stdio.h>#include<stdlib.h>int total_time=20;int time_slice=3;typedef struct process { // 进程控制块char pname[10];int WaitTime;int BurstTime;int priority; // 数字越小优先级越高struct process *next;}PROCESS;//typedef struct process PROCESS;PROCESS * in_queue(PROCESS *head,PROCESS *p); //声明PROCESS *init() //进程初始化{int i=0;char a;PROCESS *head_new; //队列的队头head_new=(struct process*)malloc(sizeof(struct process));if(!head_new) exit(1);head_new=NULL;do{struct process *s;printf("initialize the process:\n");s=(struct process *)malloc(sizeof(struct process));if(!s) exit(1);printf("please input the pname:WaitTime: BurstTime: priority:\n");scanf("%c",&(s->pname));scanf("%d",&(s->WaitTime));scanf("%d",&(s->BurstTime));scanf("%d",&(s->priority));s->next=NULL;in_queue(head_new,s);i++;printf("do u want to insert process more ?? 'Y'or'N'\n");printf("----------------------------------------'\n");scanf("%c",&a);scanf("%c",&a);// if(a=='Y'||a=='y') continue;// else if(a=='N'||a=='n') break;}while((i<MAX) &&(a=='Y'||a=='y'));return head_new;}///////////////////////////////////////////////////////////PROCESS *in_queue(PROCESS *head,PROCESS *p) //入队函数 {if(head==NULL){head=p;p->next=NULL;}else{p->next=head;head=p;}// printf("the process insert into the mothball queue :\n");return head;}//////////////////////////////////////////////////////////////*void new_queue() //后备队列先来先服务方式进入就绪{return *head_new;}*/PROCESS *FCFS_process(){PROCESS *p,*q,*a; //a用来记录选中结点的前一个结点q=p=init(); //这里是不是有个问题??while(p->next!=NULL){a=p;if(p->WaitTime>=q->WaitTime){q=p;p=p->next;}}q->WaitTime--;if(q->WaitTime==0) //如果等待时间为0则把该进程从后备队列中移除{a->next=p->next;free(p);}return q; //选择等待时间最久的)}//////////////////////就绪队列,入口函数为就绪队列的头指针/////////////int count=0;PROCESS *ready_queue(PROCESS *head) //就绪队列优先级进入运行4道 {PROCESS *p;while(count<4){p=FCFS_process();p->next=head->next;head=p;count++;printf("the process has inserted into the ready queue :\n"); }return head;}//insert_ready() //PROCESS *high_priority(PROCESS *P) //选择优先级最高的进程 {PROCESS *q,*p; //问题,入口形参中q=p;while(p->next!=NULL){if(q->priority>p->priority)q=p;p=p->next;}return q;}PROCESS *pick_ready(PROCESS *a) //从就绪队列中选择进程运行 {PROCESS *p=ready_queue(a);PROCESS *b=high_priority(p);return b;}void run(PROCESS *a) //运行一个时间片{while((time_slice>0)&&(a->BurstTime>0)) //指针用-> 变量用. {printf("the process %c is runing",a->pname);printf("the process BurstTime time is %d ",a->WaitTime);printf("the process BurstTime time is %d ",a->BurstTime);printf("the process priority is %d ",a->priority);a->BurstTime--;time_slice--;}a->priority--;total_time--;/*if(a->runtime>0)return a;else return NULL;*/}void main(){PROCESS *p;PROCESS *head=init();while(total_time!=0) {ready_queue(head); p=pick_ready(head);if(p->BurstTime==0) {p=p->next;free(p);count--;}run(p);time_slice=3;}}原文已完。
c语言实现进程调度算法
c语言实现进程调度算法进程调度算法是操作系统中的一个重要组成部分,用于决定在多道程序环境下,选择哪个进程来占用CPU并执行。
C语言是一种通用的编程语言,可以用于实现各种进程调度算法。
这里我将分别介绍三种常见的进程调度算法:先来先服务调度算法(FCFS)、最短作业优先调度算法(SJF)和轮转法调度算法(RR),并给出用C语言实现的示例代码。
首先,我们来看先来先服务调度算法(FCFS)。
此算法根据到达时间的先后顺序,按照先来后到的顺序进行处理。
下面是基于C语言的先来先服务调度算法实现示例代码:```c#include<stdio.h>struct Process};void FCFS(struct Process proc[], int n)for (int i = 1; i < n; i++)}printf("进程号到达时间服务时间完成时间等待时间周转时间\n");for (int i = 0; i < n; i++)}for (int i = 0; i < n; i++)}int maiint n;printf("请输入进程数:");scanf("%d", &n);struct Process proc[n];for (int i = 0; i < n; i++)printf("请输入进程%d的到达时间和服务时间(用空格分隔):", i + 1);}FCFS(proc, n);return 0;```其次,我们来看最短作业优先调度算法(SJF),该算法选择执行时间最短的进程先执行。
下面是基于C语言的最短作业优先调度算法实现示例代码:```c#include<stdio.h>struct Process};void SJF(struct Process proc[], int n)for (int i = 0; i < n; i++)for (int j = 0; j < i; j++)}shortest_job = i;for (int j = i + 1; j < n; j++)shortest_job = j;}}}for (int i = 1; i < n; i++)}printf("进程号到达时间服务时间完成时间等待时间周转时间\n");for (int i = 0; i < n; i++)}for (int i = 0; i < n; i++)}int maiint n;printf("请输入进程数:");scanf("%d", &n);struct Process proc[n];for (int i = 0; i < n; i++)printf("请输入进程%d的到达时间和服务时间(用空格分隔):", i + 1);}SJF(proc, n);return 0;```最后,我们来看轮转法调度算法(RR),该算法分配一个时间片给每个进程,当时间片用完后,将CPU分配给下一个进程。
操作系统进程调度优先级算法C语言模拟
操作系统进程调度优先级算法C语言模拟```cstruct Processint pid; // 进程IDint priority; // 优先级};```接下来,我们使用一个简单的示例来说明操作系统进程调度优先级算法的模拟实现。
假设有5个进程需要调度执行,它们的初始优先级和运行时间如下:进程ID,优先级,已运行时间--------,--------,------------P1,4,2P2,3,4P3,1,6P4,2,1P5,5,3首先,我们需要将这些进程按照优先级排序,以得到调度队列。
可以使用冒泡排序算法实现,代码如下:```cvoid bubbleSort(struct Process *processes, int n)for (int i = 0; i < n - 1; i++)for (int j = 0; j < n - i - 1; j++)if (processes[j].priority > processes[j + 1].priority)struct Process temp = processes[j];processes[j] = processes[j + 1];processes[j + 1] = temp;}}}``````c#include <stdio.h>void bubbleSort(struct Process *processes, int n);int maistruct Process processes[] = {{1, 4, 2}, {2, 3, 4}, {3, 1, 6}, {4, 2, 1}, {5, 5, 3}};int n = sizeof(processes) / sizeof(struct Process);bubbleSort(processes, n);printf("初始调度队列:\n");printf("进程ID\t优先级\t已运行时间\n");for (int i = 0; i < n; i++)}//模拟进程调度printf("\n开始模拟进程调度...\n");int finished = 0;while (finished < n)struct Process *current_process = &processes[0];printf("执行进程 P%d\n", current_process->pid);finished++;printf("进程 P%d 执行完毕\n", current_process->pid);} else}bubbleSort(processes, n);}printf("\n所有进程执行完毕,调度队列的最终顺序为:\n"); printf("进程ID\t优先级\t已运行时间\n");for (int i = 0; i < n; i++)}return 0;```以上代码中,我们使用了一个变量`finished`来记录已完成的进程数量,当`finished`等于进程数量`n`时,所有进程执行完毕。
操作系统五种进程调度算法的代码
操作系统五种进程调度算法的代码一、先来先服务(FCFS)调度算法先来先服务(FCFS)调度算法是操作系统处理进程调度时比较常用的算法,它的基本思想是按照进程的提交时间的先后顺序依次调度进程,新提交的进程会在当前运行进程之后排队,下面通过C语言代码来实现先来先服务(FCFS)调度算法:#include <stdio.h>#include <stdlib.h>//定义进程的数据结构struct Processint pid; // 进程标识符int at; // 到达时间int bt; // 执行时间};//进程调度函数void fcfs_schedule(struct Process *processes, int n)int i, j;//根据进程的到达时间排序for(i = 0; i < n; i++)for(j = i+1; j < n; j++)if(processes[i].at > processes[j].at) struct Process temp = processes[i]; processes[i] = processes[j];processes[j] = temp;//获取各个进程执行完毕的时间int ct[n];ct[0] = processes[0].at + processes[0].bt; for(i = 1; i < n; i++)if(ct[i-1] > processes[i].at)ct[i] = ct[i-1] + processes[i].bt;elsect[i] = processes[i].at + processes[i].bt; //计算各个进程的周转时间和带权周转时间int tat[n], wt[n], wt_r[n];for(i = 0; i < n; i++)tat[i] = ct[i] - processes[i].at;wt[i] = tat[i] - processes[i].bt;wt_r[i] = wt[i] / processes[i].bt;printf("P%d:\tAT=%d\tBT=%d\tCT=%d\tTAT=%d\tWT=%d\tWT_R=%f\n", processes[i].pid, processes[i].at, processes[i].bt, ct[i], tat[i], wt[i], wt_r[i]);//主函数int mainstruct Process processes[] ={1,0,3},{2,3,5},{3,4,6},{4,5,2},{5,6,4}};fcfs_schedule(processes, 5);return 0;输出:。
(完整版)操作系统进程调度C语言代码.docx
//sun.cpp : 定义控制台应用程序的入口点。
//本算法包含四种调度 :先到先服务,短作业优先,时间片轮转,优先级优先!#include"stdio.h"#define N 50void main(){ void sjp(); voidfcfs();void sjf();void yxj();int a;while(true){printf("\n\n");printf("\t\t/*************************/");printf("\n\t\t/*1、先到先服务调度*/");printf("\n\t\t/*2、短作业优先调度*/");printf("\n\t\t/*3、时间片轮转调度*/");printf("\n\t\t/*4、优先级优先调度*/");printf("\n\t\t/*0、退出*/\n");printf("\t\t/*************************/");printf("\n\n\t 请选择菜单项:\t");scanf("%d",&a);printf("\n");switch(a){case 1: fcfs();break;case 2: sjf();break;case 3: sjp();break;case 4: yxj();break;default: break;}if(a<0&&a>4) break;}}void sjp(){int i,j,n,min,px,sjp,time;float sum1,sum2;bool flag=true;printf("\t请输入有n 个进程 (0<n<=50):\t");scanf("%d",&n);while(n>50||n<=0){printf("n\t请重新输入:");scanf("%d",&n);}printf("\n\n");printf("\t请输入时间片大小(0<sjp):\t"); scanf("%d",&sjp);while(sjp<=0){printf("n\t请重新输入:");scanf("%d",&sjp);}struct Gzuo{int id;// 进程名字int dt;// 到达时刻int st;// 服务时间int wct;// 完成时刻int st2; // 标志是否完成float zt;// 周转时间float dczt; // 带权周转时间};Gzuo a[N];for(i=0;i<n;i++){a[i].id=i+1;printf("\t到达时间:");scanf("%d",&a[i].dt);printf("\t服务时间:");scanf("%d",&a[i].st);a[i].st2 = a[i].st;printf("\n");}for(j=n -1;j>=0;j --){for(i=0;i<j;i++){if(a[i].dt>a[i+1].dt){min=a[i].dt;a[i].dt=a[i+1].dt;a[i+1].dt=min;min=a[i].st;a[i].st=a[i+1].st;a[i+1].st=min;min=a[i].st2;a[i].st2=a[i+1].st2;a[i+1].st2=min;min=a[i].id;a[i].id=a[i+1].id;a[i+1].id=min;}}}time = a[0].dt;//printf(" 赋值后 TIME 值为: %d\n",time);min = 0;while(min<n){flag = true;for(i = 0;i<n;i++){if(a[i].st2>0&&a[i].dt<=time)flag = false;}for(i=0;i<n;i++){if(a[i].st2 > 0 ){if(a[i].dt<=time){//printf(" 当前 a[%d].st2 值为: %d\n",i,a[i].st2);a[i].st2 = a[i].st2 - sjp;//printf(" 运算后当前a[%d].st2 值为: %d\n",i,a[i].st2);//printf(" 当前 TIME 值为: %d\n",time);time = time + sjp;//printf(" 增加之后TIME 值为: %d\n",time);if(a[i].st2<=0){a[i].wct = time + a[i].st2;a[i].zt=(float)(a[i].wct -a[i].dt);a[i].dczt=a[i].zt/a[i].st;min++;}}else if(flag){for(i=0;i<n;i++){if(a[i].st2>0&&a[i].dt>time){time = a[i].dt;break;}}}}}}printf("\t1 、按 id 号依次输出 \n");printf("\t2 、按完成顺序依次输出\n");printf("\n\t 请选择输出顺序:\t");scanf("%d",&px);printf("\nid :到达时间 \t 服务时间 \t 完成时间 \t 周转时间 \t 带权周转时间\n"); sum1=0;sum2=0;switch(px){case 2:{for(i=0;i<n;i++){printf("%d : %d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n",a[i].id,a[i].dt,a[i].st,a[i].wct,a[i].zt,a[i].dczt);sum1+=a[i].zt;sum2+=a[i].dczt;}printf("\n平均周转时间:%.2f\n",sum1/n);printf("\n平均带权周转时间:%.2f\n\n",sum2/n);break;}case 1:{for(j=0;j<n;j++){for(i=0;i<n;i++)if(a[i].id==j+1){printf("%d : %d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n",a[i].id,a[i].dt,a[i].st,a[i].wct,a[i].zt,a[i].dczt);sum1+=a[i].zt;sum2+=a[i].dczt;}}printf("\n平均周转时间:%.2f\n",sum1/n);printf("\n平均带权周转时间:%.2f\n\n",sum2/n);break;}default: break;}}void fcfs(){int i,j,n,min,px;float sum1,sum2;printf("\t请输入有n 个进程 (0<n<=50):\t");scanf("%d",&n);while(n>50||n<=0){printf("n\t请重新输入:");scanf("%d",&n);}printf("\n\n");struct Gzuo{int id; int dt; int st;// 进程名字// 到达时刻// 服务时间int wct; float zt; float dczt;// 完成时刻// 周转时间// 带权周转时间};Gzuo a[N];for(i=0;i<n;i++){a[i].id=i+1;printf("\t到达时间:");scanf("%d",&a[i].dt);printf("\t服务时间:");scanf("%d",&a[i].st);printf("\n");}for(j=n -1;j>=0;j --){for(i=0;i<j;i++){if(a[i].dt>a[i+1].dt){min=a[i].dt;a[i].dt=a[i+1].dt;a[i+1].dt=min;min=a[i].st;a[i].st=a[i+1].st;a[i+1].st=min;min=a[i].id;a[i].id=a[i+1].id;a[i+1].id=min;}}}a[0].wct=a[0].st+a[0].dt;a[0].zt=(float)a[0].st;a[0].dczt=a[0].zt/a[0].st;for(i=1;i<n;i++){if(a[i].dt>a[i -1].wct){a[i].wct=a[i].dt+a[i].st;a[i].zt=(float)a[i].st;a[i].dczt=a[i].zt/a[i].st;}else{a[i].wct=a[i -1].wct+a[i].st;a[i].zt=(float)(a[i].wct -a[i].dt);a[i].dczt=a[i].zt/a[i].st;}}printf("\t1 、按 id 号依次输出 \n");printf("\t2 、按完成顺序依次输出\n");printf("\n\t 请选择输出顺序:\t");scanf("%d",&px);printf("\nid :到达时间 \t 服务时间 \t 完成时间 \t 周转时间 \t 带权周转时间 \n");sum1=0;sum2=0;switch(px){case 2:{for(i=0;i<n;i++){printf("%d : %d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n",a[i].id,a[i].dt,a[i].st,a[i].wct,a[i].zt,a[i].dczt);sum1+=a[i].zt;sum2+=a[i].dczt;}printf("\n平均周转时间:%.2f\n",sum1/n);printf("\n平均带权周转时间:%.2f\n\n",sum2/n);break;}case 1:{for(j=0;j<n;j++){for(i=0;i<n;i++)if(a[i].id==j+1){printf("%d : %d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n",a[i].id,a[i].dt,a[i].st,a[i].wct,a[i].zt,a[i].dczt);sum1+=a[i].zt;sum2+=a[i].dczt;}}printf("\n平均周转时间:%.2f\n",sum1/n);printf("\n平均带权周转时间:%.2f\n\n",sum2/n);break;}default: break;}}void sjf(){int i,j,n,min,px;int b=0,z;float sum1,sum2;printf("\n\t\t 请输入有n 个进程 (0<n<=50):\t");scanf("%d/n",&n);while(n>50||n<=0){printf("n\t请重新输入:");scanf("%d",&n);}printf("\n");struct Gzuo{int id; // 进程名字int dt; // 到达时刻int st; // 服务时间int wct;// 完成时刻float zt;// 周转时间float dczt; // 带权周转时间};Gzuo a[N];for(i=0;i<n;i++){a[i].id=i+1;printf("\t到达时间:");scanf("%d",&a[i].dt);printf("\t服务时间:");scanf("%d",&a[i].st);printf("\n");}min=a[0].dt;for(j=n -1;j>=0;j --){for(i=0;i<j;i++){if(a[i].dt>a[i+1].dt){min=a[i].dt;a[i].dt=a[i+1].dt;a[i+1].dt=min;min=a[i].st;a[i].st=a[i+1].st;a[i+1].st=min;min=a[i].id;a[i].id=a[i+1].id;a[i+1].id=min;}if(a[i].dt==a[i+1].dt&&a[i].st>a[i+1].st){min=a[i].dt;a[i].dt=a[i+1].dt;a[i+1].dt=min;min=a[i].st;a[i].st=a[i+1].st;a[i+1].st=min;min=a[i].id;a[i].id=a[i+1].id;a[i+1].id=min;}}}a[0].wct=a[0].st+a[0].dt;a[0].zt=(float)a[0].st;a[0].dczt=a[0].zt/a[0].st;for(i=1;i<n;i++){if(a[i].dt>a[0].wct) ;else b=b+1;}for(j=b -1;j>=1;j --){for(i=1;i<j;i++){if(a[i].st>a[i+1].st){min=a[i].dt;a[i].dt=a[i+1].dt;a[i+1].dt=min;min=a[i].st;a[i].st=a[i+1].st;a[i+1].st=min;min=a[i].id;a[i].id=a[i+1].id;a[i+1].id=min;}}}for(i=1;i<n;i++){if(a[i].dt>a[i -1].wct){a[i].wct=a[i].dt+a[i].st;a[i].zt=(float)a[i].st;a[i].dczt=a[i].zt/a[i].st;}else{a[i].wct=a[i -1].wct+a[i].st;a[i].zt=(float)(a[i].wct -a[i].dt);a[i].dczt=a[i].zt/a[i].st;}for(j=i+1,b=j;j<n;j++){if(a[j].dt>a[i].wct) ;else b=b+1;}for(j=b -1;j>=i;j --){for(z=i;z<j;z++){if(a[z].st>a[z+1].st){min=a[z].dt;a[z].dt=a[z+1].dt;a[z+1].dt=min;min=a[z].st;a[z].st=a[z+1].st;a[z+1].st=min;min=a[i].id;a[i].id=a[i+1].id;a[i+1].id=min;}}}}printf("\n\t 请选择输出顺序\n");printf("\t1 、按 id 号依次输出 \n");printf("\t2 、按完成顺序依次输出\n");scanf("%d",&px);printf("\nid :到达时间 \t 服务时间 \t 完成时间 \t 周转时间 \t 带权周转时间\n"); sum1=0;sum2=0;switch(px){case 2:{for(i=0;i<n;i++){printf("%d : %d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n",a[i].id,a[i].dt,a[i].st,a[i].wct,a[i].zt,a[i].dczt);sum1+=a[i].zt;sum2+=a[i].dczt;}printf("\n平均周转时间:%.2f\n",sum1/n);printf("\n平均带权周转时间:%.2f\n\n",sum2/n);break;}case 1:{for(j=0;j<n;j++){ for(i=0;i<n;i++)if(a[i].id==j+1){printf("%d : %d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n",a[i].id,a[i].dt,a[i].st,a[i].wct,a[i].zt,a[i].dczt);sum1+=a[i].zt;sum2+=a[i].dczt;}}printf("\n平均周转时间:%.2f\n",sum1/n);printf("\n平均带权周转时间:%.2f\n\n",sum2/n);break;}default: break;}}void yxj(){int i,j,n,min,px;int b=0,z;float sum1,sum2;printf("\n\t\t 请输入有n 个进程 (0<n<=50):\t");scanf("%d/n",&n);while(n>50||n<=0){printf("n\t请重新输入:");scanf("%d",&n);}printf("\n");struct Gzuo{int id;// 进程名字int dt;// 到达时刻int st;// 服务时间int yxj; // 优先级int wct;// 完成时刻float zt;// 周转时间float dczt; // 带权周转时间};Gzuo a[N];for(i=0;i<n;i++){a[i].id=i+1;printf("\t到达时间:");scanf("%d",&a[i].dt);printf("\t服务时间:");scanf("%d",&a[i].st);printf("\t优先级:");scanf("%d",&a[i].yxj);printf("\n");}min=a[0].dt;for(j=n -1;j>=0;j --){for(i=0;i<j;i++){if(a[i].dt>a[i+1].dt){min=a[i].dt;a[i].dt=a[i+1].dt;a[i+1].dt=min;min=a[i].st;a[i].st=a[i+1].st;a[i+1].st=min;min=a[i].id;a[i].id=a[i+1].id;a[i+1].id=min;min=a[i].yxj;a[i].yxj=a[i+1].yxj;a[i+1].yxj=min;}if(a[i].dt==a[i+1].dt&&a[i].yxj<a[i+1].yxj){min=a[i].dt;a[i].dt=a[i+1].dt;a[i+1].dt=min;min=a[i].st;a[i].st=a[i+1].st;a[i+1].st=min;min=a[i].id;a[i].id=a[i+1].id;a[i+1].id=min;min=a[i].yxj;a[i].yxj=a[i+1].yxj;a[i+1].yxj=min;}}}a[0].wct=a[0].st+a[0].dt;a[0].zt=(float)a[0].st;a[0].dczt=a[0].zt/a[0].st;for(i=1;i<n;i++){if(a[i].dt>a[0].wct) ;else b++;}for(j=b -1;j>=1;j --){for(i=1;i<j;i++){if(a[i].yxj<a[i+1].yxj){min=a[i].dt;a[i].dt=a[i+1].dt;a[i+1].dt=min;min=a[i].st;a[i].st=a[i+1].st;a[i+1].st=min;min=a[i].id;a[i].id=a[i+1].id;a[i+1].id=min;min=a[i].yxj;a[i].yxj=a[i+1].yxj;a[i+1].yxj=min;}}}for(i=1;i<n;i++){if(a[i].dt>a[i -1].wct){a[i].wct=a[i].dt+a[i].st;a[i].zt=(float)a[i].st;a[i].dczt=a[i].zt/a[i].st;}else{a[i].wct=a[i -1].wct+a[i].st;a[i].zt=(float)(a[i].wct -a[i].dt);a[i].dczt=a[i].zt/a[i].st;}for(j=i+1,b=j;j<n;j++){if(a[j].dt>a[i].wct) ;else b=b+1;}for(j=b -1;j>=i;j --){for(z=i;z<j;z++){if(a[z].yxj<a[z+1].yxj){min=a[z].dt;a[z].dt=a[z+1].dt;a[z+1].dt=min;min=a[z].st;a[z].st=a[z+1].st;a[z+1].st=min;min=a[i].id;a[i].id=a[i+1].id;a[i+1].id=min;}}}}printf("\n\t 请选择输出顺序\n");printf("\t1 、按 id 号依次输出 \n");printf("\t2 、按完成顺序依次输出\n");scanf("%d",&px);printf("\nid :到达时间 \t 服务时间 \t 优先级 \t 完成时间 \t 周转时间 \t 带权周转时间 \n");sum1=0;sum2=0;switch(px){case 2:{for(i=0;i<n;i++){printf("%d : %d\t\t%d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n",a[i].id,a[i].dt,a[i].yxj,a[i].st,a[i].wct,a[i].z t,a[i].dczt);sum1+=a[i].zt;sum2+=a[i].dczt;}printf("\n平均周转时间:%.2f\n",sum1/n);printf("\n平均带权周转时间:%.2f\n\n",sum2/n);break;}case 1:{for(j=0;j<n;j++){ for(i=0;i<n;i++)if(a[i].id==j+1){printf("%d : %d\t\t%d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n",a[i].id,a[i].dt,a[i].yxj,a[i].st,a[i].wct,a[i].z t,a[i].dczt);sum1+=a[i].zt;sum2+=a[i].dczt;}}printf("\n平均周转时间:%.2f\n",sum1/n);printf("\n平均带权周转时间:%.2f\n\n",sum2/n);break;}default: break;}}。
基于优先级调度进程算法的实现
基于优先级调度进程算法的实现优先级调度进程算法是操作系统中常用的调度算法之一,其核心思想是根据进程的优先级来确定调度顺序。
优先级较高的进程会先被调度执行,从而提高系统的响应速度和任务处理效率。
以下是基于优先级调度进程算法的示例代码(使用C语言实现):```c#include<stdio.h>#include<stdlib.h>#define MAX_PROCESS 10typedef struct Processint pid; // 进程IDint priority; // 进程优先级} Process;//按照进程优先级降序排列void sort(Process *p, int n)for (int i = 0; i < n - 1; i++)for (int j = 0; j < n - i - 1; j++)if (p[j].priority < p[j + 1].priority)Process temp = p[j];p[j]=p[j+1];p[j + 1] = temp;}}}for (int i = 1; i < n; i++)}for (int i = 0; i < n; i++)}void display(Process *p, int n)printf("进程ID\t优先级\t执行时间\t等待时间\t周转时间\n"); for (int i = 0; i < n; i++)}int maiint n;Process p[MAX_PROCESS];printf("请输入进程数量:");scanf("%d", &n);printf("请输入进程的优先级和执行时间:\n");for (int i = 0; i < n; i++)p[i].pid = i + 1;printf("进程%d的优先级:", i + 1);scanf("%d", &p[i].priority);printf("进程%d的执行时间:", i + 1);}sort(p, n); // 按照优先级排序display(p, n); // 显示结果return 0;```以上代码实现了基本的优先级调度进程算法,包括输入进程数量、优先级和执行时间,并按照进程的优先级进行排序。
操作系统进程调度C语言代码
t2值为:%d\n",i,a[i].st2);
t2<=0)
{
a[i].wct = time + a[i].st2;
a[i].zt=(float)(a[i].wct-a[i].dt);
a[i].dczt=a[i].zt/a[i].st;
min++;
}
scanf("%d",&px);
printf("\nid:到达时间\t服务时间\t完成时间\t周转时间\t带权周转时间\n");
sum1=0;
sum2=0;
switch(px)
{
case 2:
{
for(i=0;i<n;i++)
{
printf("%d:%d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n",a[i].id,a[i].dt,a[i].st,a[i].wct,a[i].zt,a[i].dczt);
sum1+=a[i].zt;
sum2+=a[i].dczt;
}
printf("\n平均周转时间:%.2f\n",sum1/n);
printf("\n平均带权周转时间:%.2f\n\n",sum2/n);
break;
}
case 1:
{
for(j=0;j<n;j++)
{
for(i=0;i<n;i++)
if(a[i].id==j+1)
min=a[i].id;
a[i].id=a[i+1].id;
操作系统程序调度算法(C++)
头文件部分:#include <conio.h>#include <stdlib.h>#include <stdio.h>#include <io.h>#include <string.h>#define MAX_PROCESS 10int process_number=0;typedef struct pcb{struct pcb *next; //下一个进程控制块指针char process_name[20]; //进程名int process_number; //进程编号int process_start_moment; //进程启动时刻int process_need_time; //要求运行时间int process_time_slice; //时间片int process_priority; //优先数}PCB; //自定义数据类型:进程控制块PCB pcb_table[MAX_PROCESS]; //进程控制块表PCB *pcb_run=NULL; //进程运行队列头指针PCB *pcb_free=NULL; //进程空闲队列头指针PCB *pcb_ready=NULL; //进程就绪队列头指针PCB *pcb_ready_rear=NULL; //进程就绪队列尾指针PCB *pcb_blocked=NULL; //阻塞队列头指针PCB *pcb_blocked_rear=NULL; //阻塞队列尾指针void init_pcb_table( ); //初始化进程控制块表void print_space(int num); //显示若干个空格void display_process_queue(PCB *queue); //显示进程队列PCB *create_process( ); //创建进程函数,成功时返回新创建进程的PCB,失败时返回NULL。
操作系统五种进程调度算法的代码
进程调度算法的模拟实现⏹实验目的1.本实验模拟在单处理机情况下的处理机调度问题,加深对进程调度的理解。
2.利用程序设计语言编写算法,模拟实现先到先服务算法FCFS、轮转调度算法RR、最短作业优先算法SJF、优先级调度算法PRIOR、最短剩余时间优先算法SRTF。
3.进行算法评价,计算平均等待时间和平均周转时间。
⏹实验内容及结果1.先来先服务算法2.轮转调度算法3. 优先级调度算法4. 最短时间优先算法5. 最短剩余时间优先算法⏹实验总结在此次模拟过程中,将SRTF单独拿了出来用指针表示,而其余均用数组表示。
⏹完整代码【Srtf.cpp代码如下:】//最短剩余时间优先算法的实现#include<stdio.h>#include<stdlib.h>#include<time.h>typedefstruct{int remain_time;//进程剩余执行时间int arrive_time;//进程到达时间int Tp;//进入就绪队列的时间int Tc;//进入执行队列的时间int To;//进程执行结束的时间int number;//进程编号}Process_Block;//定义进程模块typedefstruct _Queue{Process_Block PB;struct _Queue *next;}_Block,*Process;//定义一个进程模块队列中结点typedefstruct{Process head;//队列头指针Process end;//队列尾指针}Process_Queue;//进程队列Process_Queue PQ;//定义一个全局队列变量int t;//全局时间Process Run_Now;//当前正在运行的进程,作为全局变量void InitQueue(Process_Queue PQ){PQ.head ->next = NULL;PQ.end ->next = PQ.head;}/*初始化队列*/int IsEmpty(Process_Queue PQ){if(PQ.end->next == PQ.head)return 1;//队列空的条件为头指针指向尾指针并且尾指针指向头指针elsereturn 0;}/*判定队列是否为空队列*/void EnQueue(Process_Queue PQ,Process P){Process temp =(Process)malloc(sizeof(_Block));temp = PQ.end;temp->next->next = P;PQ.end->next = P;}/*插入队列操作*/Process DeQueue(Process_Queue PQ){if(IsEmpty(PQ))return NULL;Process temp = PQ.head->next;PQ.head->next= temp ->next;if(PQ.end->next == temp)PQ.end->next = PQ.head;return temp;}/*出列操作*/Process ShortestProcess(Process_Queue PQ){if(IsEmpty(PQ))//如果队列为空,返回{if(!Run_Now)return NULL;elsereturn Run_Now;}Process temp,shortest,prev;int min_time;if(Run_Now)//如果当前有进程正在执行,{shortest = Run_Now;//那么最短进程初始化为当前正在执行的进程,min_time = Run_Now->PB.remain_time;}else//如果当前没有进程执行,{shortest = PQ.head->next;//则最短进程初始化为队列中第一个进程min_time = PQ.head->next->PB.remain_time;}temp = PQ.head;prev = temp;while(temp->next){if(temp->next->PB.remain_time <min_time)//如果当前进程的剩余时间比min_time短,{shortest = temp->next;//则保存当前进程,min_time = shortest->PB.remain_time;prev=temp;//及其前驱}temp=temp->next;}if(shortest == PQ.end->next)//如果最短剩余时间进程是队列中最后一个进程,PQ.end->next = prev;//则需要修改尾指针指向其前驱prev->next = shortest->next;//修改指针将最短剩余时间进程插入到队头return shortest;}/*调度最短剩余时间的进程至队头*/void Run(){Run_Now->PB.remain_time--;//某一时间运行它的剩余时间减return;}/*运行函数*/void Wait(){return ;}int sum(intarray[],int n){int i,sum=0;for(i=0;i<n;i++)sum+=array[i];return sum;}int main(){PQ.head = (Process)malloc(sizeof(_Block));PQ.end = (Process)malloc(sizeof(_Block));Run_Now = (Process)malloc(sizeof(_Block));Run_Now =NULL;InitQueue(PQ);int i,N,Total_Time=0;//Total_Time为所有进程的执行时间之和printf("请输入计算机中的进程数目:\n");scanf("%d",&N);Process *P,temp;P = (Process*)malloc(N*sizeof(Process));int *wt,*circle_t;wt =(int*)malloc(N*sizeof(int));circle_t =(int*)malloc(N*sizeof(int));for(i=0;i<N;i++){P[i] = (Process)malloc(sizeof(_Block));P[i]->PB.number =i+1;P[i]->next =NULL;wt[i] =0;circle_t[i] =0;printf("输入第%d个进程的到达时间及剩余执行时间:\n",i+1);scanf("%d %d",&P[i]->PB.arrive_time,&P[i]->PB.remain_time);}for(i=0;i<N;i++)Total_Time+=P[i]->PB.remain_time;printf("\n进程按顺序运行依次为:\n");i=0;int k=0;for(t=0;;t++){if(Run_Now)//如果当前有进程正在执行{Run();if(t == P[i]->PB.arrive_time)//如果当前时间正好有进程进入{if(P[i]->PB.remain_time < Run_Now->PB.remain_time){temp = P[i];P[i] = Run_Now;Run_Now = temp;//则调度它至运行队列中,Run_Now->PB.Tp=t;Run_Now->PB.Tc=t;wt[Run_Now->PB.number-1]+=Run_Now->PB.Tc-Run_Now->PB.Tp;printf("%d ",Run_Now->PB.number);}EnQueue(PQ,P[i]);//并将当前运行进程重新插入队列中P[i]->PB.Tp=t;k++;i=(i+1)>(N-1)?(N-1):(i+1);}if(Run_Now->PB.remain_time == 0)//如果当前进程运行结束,{Run_Now->PB.To=t;//进程运行结束的时间circle_t[Run_Now->PB.number-1] +=t-Run_Now->PB.arrive_time;free(Run_Now);//则将它所占资源释放掉,Run_Now =NULL;//并修改Run_Now为NULLRun_Now = ShortestProcess(PQ);//从就绪队列中调出最短剩余时间进程至队头,if(!Run_Now)//如果队列为空,转为等待状态{if(IsEmpty(PQ) && k >= N) break;Wait();continue;}else{Run_Now->PB.Tc=t;wt[Run_Now->PB.number-1]+=Run_Now->PB.Tc-Run_Now->PB.Tp;printf("%d ",Run_Now->PB.number);}}}else//如果当前运行进程为空,那么{if(t == P[i]->PB.arrive_time)//如果正好这时有进程入队{k++;EnQueue(PQ,P[i]);Run_Now = DeQueue(PQ);//则直接被调入运行队列中Run_Now->PB.Tp=t;Run_Now->PB.Tc=t;printf("%d ",Run_Now->PB.number);i=(i+1)>(N-1)?(N-1):(i+1);}else{Wait();continue;}}}printf("\n");printf("平均等待时间是:\n%f\n",((float)sum(wt,N))/N);printf("平均周转时间是:\n%f\n",((float)sum(circle_t,N))/N);return 0;}//////////////////////////////////////////////////////【Process.cpp代码如下:】#include<iostream>#include<string>usingnamespace std;class Process{public:string ProcessName; // 进程名字int Time; // 进程需要时间int leval; // 进程优先级int LeftTime; // 进程运行一段时间后还需要的时间};void Copy ( Process proc1, Process proc2); // 把proc2赋值给proc1void Sort( Process pr[], int size) ; // 此排序后按优先级从大到小排列void sort1(Process pr[], int size) ; // 此排序后按需要的cpu时间从小到大排列void Fcfs( Process pr[], int num, int Timepice); // 先来先服务算法void TimeTurn( Process process[], int num, int Timepice); // 时间片轮转算法void Priority( Process process[], int num, int Timepice); // 优先级算法void main(){int a;cout<<endl;cout<<" 选择调度算法:"<<endl;cout<<" 1: FCFS 2: 时间片轮换3: 优先级调度4: 最短作业优先5: 最短剩余时间优先"<<endl; cin>>a;constint Size =30;Process process[Size] ;int num;int TimePice;cout<<" 输入进程个数:"<<endl;cin>>num;cout<<" 输入此进程时间片大小: "<<endl;cin>>TimePice;for( int i=0; i< num; i++){string name;int CpuTime;int Leval;cout<<" 输入第"<< i+1<<" 个进程的名字、cpu时间和优先级:"<<endl;cin>>name;cin>> CpuTime>>Leval;process[i].ProcessName =name;process[i].Time =CpuTime;process[i].leval =Leval;cout<<endl;}for ( int k=0;k<num;k++)process[k].LeftTime=process[k].Time ;//对进程剩余时间初始化cout<<" ( 说明: 在本程序所列进程信息中,优先级一项是指进程运行后的优先级!! )";cout<<endl; cout<<endl;cout<<"进程名字"<<"共需占用CPU时间"<<" 还需要占用时间"<<" 优先级"<<" 状态"<<endl; if(a==1)Fcfs(process,num,TimePice);elseif(a==2)TimeTurn( process, num, TimePice);elseif(a==3){Sort( process, num);Priority( process , num, TimePice);}else// 最短作业算法,先按时间从小到大排序,再调用Fcfs算法即可{sort1(process,num);Fcfs(process,num,TimePice);}}void Copy ( Process proc1, Process proc2){proc1.leval =proc2.leval ;proc1.ProcessName =proc2.ProcessName ;proc1.Time =proc2.Time ;}void Sort( Process pr[], int size) //以进程优先级高低排序{// 直接插入排序for( int i=1;i<size;i++){Process temp;temp = pr[i];int j=i;while(j>0 && temp.leval<pr[j-1].leval){pr[j] = pr[j-1];j--;}pr[j] = temp;} // 直接插入排序后进程按优先级从小到大排列for( int d=size-1;d>size/2;d--){Process temp;temp=pr [d];pr [d] = pr [size-d-1];pr [size-d-1]=temp;} // 此排序后按优先级从大到小排列}/*最短作业优先算法的实现*/void sort1 ( Process pr[], int size) // 以进程时间从低到高排序{// 直接插入排序for( int i=1;i<size;i++){Process temp;temp = pr[i];int j=i;while(j>0 && temp.Time < pr[j-1].Time ){pr[j] = pr[j-1];j--;}pr[j] = temp;}}/*先来先服务算法的实现*/void Fcfs( Process process[], int num, int Timepice){ // process[] 是输入的进程,num是进程的数目,Timepice是时间片大小while(true){if(num==0){cout<<" 所有进程都已经执行完毕!"<<endl;exit(1);}if(process[0].LeftTime==0){cout<<" 进程"<<process[0].ProcessName<< " 已经执行完毕!"<<endl;for (int i=0;i<num;i++)process[i]=process[i+1];num--;}elseif(process[num-1].LeftTime==0){cout<<" 进程"<<process[num-1].ProcessName<< " 已经执行完毕!"<<endl;num--;}else{cout<<endl; //输出正在运行的进程process[0].LeftTime=process[0].LeftTime- Timepice;process[0].leval =process[0].leval-1;cout<<" "<<process[0].ProcessName <<" "<<process[0].Time <<"";cout<<process[0].LeftTime <<" "<<process[0].leval<<" 运行";cout<<endl;for(int s=1;s<num;s++){cout<<" "<<process[s].ProcessName <<" "<<process[s].Time <<"";cout<<process[s].LeftTime <<" "<<process[s].leval<<" 等待"<<endl; ;}} // elsecout<<endl;system(" pause");cout<<endl;} // while}/*时间片轮转调度算法实现*/void TimeTurn( Process process[], int num, int Timepice){while(true){if(num==0){cout<<" 所有进程都已经执行完毕!"<<endl;exit(1);}if(process[0].LeftTime==0){cout<<" 进程"<<process[0].ProcessName<< " 已经执行完毕!"<<endl;for (int i=0;i<num;i++)process[i]=process[i+1];num--;}if( process[num-1].LeftTime ==0 ){cout<<" 进程" << process[num-1].ProcessName <<" 已经执行完毕! "<<endl;num--;}elseif(process[0].LeftTime > 0){cout<<endl; //输出正在运行的进程process[0].LeftTime=process[0].LeftTime- Timepice;process[0].leval =process[0].leval-1;cout<<" "<<process[0].ProcessName <<" "<<process[0].Time <<"";cout<<process[0].LeftTime <<" "<<process[0].leval<<" 运行";cout<<endl;for(int s=1;s<num;s++){cout<<" "<<process[s].ProcessName <<" "<<process[s].Time <<"";cout<<process[s].LeftTime <<" "<<process[s].leval;if(s==1)cout<<" 就绪"<<endl;elsecout<<" 等待"<<endl;}Process temp;temp = process[0];for( int j=0;j<num;j++)process[j] = process[j+1];process[num-1] = temp;} // elsecout<<endl;system(" pause");cout<<endl;} // while}/*优先级调度算法的实现*/void Priority( Process process[], int num, int Timepice){while( true){if(num==0){cout<< "所有进程都已经执行完毕!"<<endl;exit(1);}if(process[0].LeftTime==0){cout<<" 进程" << process[0].ProcessName <<" 已经执行完毕! "<<endl;for( int m=0;m<num;m++)process[m] = process[m+1]; //一个进程执行完毕后从数组中删除num--; // 此时进程数目减少一个}if( num!=1 && process[num-1].LeftTime ==0 ){cout<<" 进程" << process[num-1].ProcessName <<" 已经执行完毕! "<<endl;num--;}if(process[0].LeftTime > 0){cout<<endl; //输出正在运行的进程process[0].LeftTime=process[0].LeftTime- Timepice;process[0].leval =process[0].leval-1;cout<<" "<<process[0].ProcessName <<" "<<process[0].Time <<"";cout<<process[0].LeftTime <<" "<<process[0].leval<<" 运行";cout<<endl; // 输出其他进程for(int s=1;s<num;s++){cout<<" "<<process[s].ProcessName <<" "<<process[s].Time <<" ";cout<<process[s].LeftTime <<" "<<process[s].leval ; if(s==1)cout<<" 就绪"<<endl;elsecout<<" 等待"<<endl;}} // elseSort(process, num);cout<<endl;system(" pause");cout<<endl;} // while}。
操作系统处理机调度算法的实现c语言源代码
6.运行所设计程序,显示或打印逐次被选中进程的进程名以及进程控制块的动态变化过程。
#include <stdio.h>
#include <stdlib.h>
struct PCB {
char name[10];
int prБайду номын сангаасority,time;
struct PCB *next;
}*k;
struct LinkQueue {
PCB * front;
PCB * rear;
};
//队列初始化
LinkQueue init(){
LinkQueue Q;
PCB * p;
p=(PCB *)malloc(sizeof(PCB));
if(p) {
Q.front=Q.rear=p;
}
LinkQueue input(LinkQueue Q) /*建立进程控制块函数*/
{
int i;
for(i=1;i<=5;i++)
{
printf("\n进程号No.%d:\n",i);
k=(PCB *)malloc(sizeof(PCB));
printf("\n输入进程名:");
scanf("%s",k->name);
temp2=temp1->next;
}
if(temp2->next==NULL && temp2->priority>=p->priority) {
temp2->next=p;
操作系统调度算法源程序
调度算法实验计算机科学与技术学院计081 0813022022 杨文章实验目的:1. 掌握进程控制块的概念,理解基本的进程调度算法。
实验内容:1. FCFS2. 短作业优先算法3. 优先级调度算法4.最高响应比调度算法实验源程序:#include<iostream>#include<iomanip>#include<fstream>using namespace std;class JOB{ //定义作业数据结构体public:int number; //作业个数int reach_time; //作业到达时间int reach_hour;int reach_minite;int need_time; //作业运行时间int privilege; //优先级float excellent;int start_time; //开始时间int wait_time; //等待时间int visited; //已读取次数public:JOB(){number=0;reach_time=0;reach_hour=0;reach_minite=0;need_time=0;privilege=0;excellent=0.0;start_time=0;visited=0;}JOB& Copy(JOB js[]){int i=0;while(js[i].number&&js[i].number>0){i++;}JOB *js2=new JOB[i];for(int j=0;j<i;j++){js2[j].number=js[j].number;js2[j].reach_time=js[j].reach_time;js2[j].reach_hour=js[j].reach_hour;js2[j].reach_minite=js[j].reach_minite;js2[j].need_time=js[j].need_time;js2[j].privilege=js[j].privilege;js2[j].excellent=js[j].excellent;js2[j].start_time=js[j].start_time;js2[j].wait_time=js[j].wait_time;}return *js2;}};//FIFO算法void FIFO(JOB job[],int quantity){JOB *jobs;jobs=(&jobs->Copy (job));int current_hour;int current_minute;int total_time=0;//输出作业流cout<<endl<<"FIFO调度算法:"<<endl;cout.setf(2);cout<<setw(10)<<"作业号:"<<setw(12)<<"到达时间:"<<setw(12)<<"开始时间:"<<setw(14)<<"周转时间(分):"<<endl;current_hour=jobs[0].reach_hour;current_minute=jobs[0].reach_minite;for(int i=0;i<quantity;i++){jobs[i].start_time=current_hour*100+current_minute;jobs[i].wait_time=(current_hour-jobs[i].reach_hour)*60+(current_minute-jobs[i].reach_minite)+jobs[i ].need_time;cout<<setw(10)<<jobs[i].number<<setw(12)<<jobs[i].reach_time<<setw(12)<<jobs[i].start_time<<setw(14)<<jobs[i].wait_time<<endl;current_hour=current_hour+(jobs[i].need_time+current_minute)/60;current_minute=(jobs[i].need_time+current_minute)%60;total_time+=jobs[i].wait_time;}cout<<endl<<"总周转时间:"<<total_time<<" 平均周转时间:"<<total_time/quantity<<endl; }//运算时间短的作业优先算法void Shorter(JOB job[],int quantity){JOB *jobs;jobs=(&jobs->Copy (job));int i,j,p;int current_hour;int current_minute;int current_need_time;int total_time=0;//输出作业流cout<<endl;cout<<endl<<"时间短作业优先算法:"<<endl;cout.setf(2);cout<<setw(10)<<"作业号:"<<setw(12)<<"到达时间:"<<setw(14)<<"周转时间(分):"<<endl;current_hour=jobs[quantity-1].reach_hour;current_minute=jobs[quantity-1].reach_minite;for(i=0;i<quantity;i++){current_need_time=30000;for(j=0;j<quantity;j++){if((jobs[j].visited==0)&&(jobs[j].need_time<current_need_time)){p=j;current_need_time=jobs[j].need_time;}}jobs[p].start_time=current_hour*100+current_minute;jobs[p].wait_time=(current_hour-jobs[p].reach_hour)*60+(current_minute-jobs[p].reach_minite)+jobs[p ].need_time;cout<<setw(10)<<jobs[p].number<<setw(12)<<jobs[p].reach_time<<setw(14)<<jobs[p].need_time<<setw(12)<<jobs[p].start_time<<setw(14)<<jobs[p].wait_time<<endl;current_hour=current_hour+(jobs[p].need_time+current_minute)/60;current_minute=(jobs[p].need_time+current_minute)%60;jobs[p].visited=1;total_time+=jobs[p].wait_time;}cout<<endl<<"总周转时间:"<<total_time<<" 平均周转时间:"<<total_time/quantity<<endl; }//优先数调度算法void Privilege(JOB job[],int quantity){JOB *jobs;jobs=(&jobs->Copy (job));int i,j,p;int current_hour;int current_minute;int current_privilege;int total_time=0;//输出作业流cout<<endl;cout.setf(2);cout<<setw(10)<<"作业号:"<<setw(12)<<"到达时间:"<<setw(14)<<"优先级:(0>1):"<<setw(12)<<"开始时间:"<<setw(14)<<"周转时间(分):"<<endl;current_hour=jobs[quantity-1].reach_hour;current_minute=jobs[quantity-1].reach_minite;for(i=0;i<quantity;i++){current_privilege=30000;for(j=0;j<quantity;j++){if((jobs[j].visited==0)&&(jobs[j].privilege<current_privilege)){p=j;current_privilege=jobs[j].privilege;}}jobs[p].start_time=current_hour*100+current_minute;jobs[p].wait_time=(current_hour-jobs[p].reach_hour)*60+(current_minute-jobs[p].reach_minite)+jobs[p ].need_time;cout<<setw(10)<<jobs[p].number<<setw(12)<<jobs[p].reach_time<<setw(14)<<jobs[p].privilege<<setw(12)<<jobs[p].start_time<<setw(14)<<jobs[p].wait_time<<endl;current_hour=current_hour+(jobs[p].need_time+current_minute)/60;current_minute=(jobs[p].need_time+current_minute)%60;jobs[p].visited=1;total_time+=jobs[p].wait_time;}cout<<endl<<"总周转时间:"<<total_time<<" 平均周转时间:"<<total_time/quantity<<endl; }//响应比最高者优先调度算法void Excellent(JOB job[],int quantity){JOB *jobs;jobs=(&jobs->Copy (job));int i,j,p;int current_hour;int current_minute;float current_excellent;int total_time=0;cout<<endl;cout<<endl<<"响应比高者优先调度算法:"<<endl;cout.setf(2);cout<<setw(10)<<"作业号"<<setw(12)<<"到达时间"<<setw(12)<<"开始时间"<<setw(14)<<"周转时间(分)"<<endl;current_hour=jobs[quantity-1].reach_hour;current_minute=jobs[quantity-1].reach_minite;for(i=0;i<quantity;i++){current_excellent=-1;for(j=0;j<quantity;j++){if(jobs[j].visited==0){jobs[j].wait_time=(current_hour-jobs[j].reach_hour)*60+(current_minute-jobs[j].reach_minite);jobs[j].excellent=(float)(jobs[j].wait_time/jobs[j].need_time);}}for(j=0;j<quantity;j++){if((jobs[j].visited==0)&&(jobs[j].excellent>current_excellent)){p=j;current_excellent=jobs[j].excellent;}}jobs[p].start_time=current_hour*100+current_minute;jobs[p].wait_time=(current_hour-jobs[p].reach_hour)*60+(current_minute-jobs[p].reach_minite)+jobs[p ].need_time;cout<<setw(10)<<jobs[p].number<<setw(12)<<jobs[p].reach_time<<setw(12)<<jobs[p].start_time<<setw(14)<<jobs[p].wait_time<<endl;current_hour=current_hour+(jobs[p].need_time+current_minute)/60;current_minute=(jobs[p].need_time+current_minute)%60;jobs[p].visited=1;total_time+=jobs[p].wait_time;}cout<<endl<<"总周转时间:"<<total_time<<" 平均周转时间:"<<total_time/quantity<<endl;}int main(void){cout<<"**********************调度模拟**************************\n\n\n"<<endl;int quantity=0; //数量JOB jobs[MAXJOB];cout<<"请输入作业数据文件名:\n";FILE *fp;char fname[20];cin>>fname;fp=fopen(fname,"r");//以只读方式打开文件if(!fp){cout<<"打开文件错误!)"<<endl;}else{while(!feof(fp)){fscanf(fp,"%d %d %d %d",&jobs[quantity].number,&jobs[quantity].reach_time,&jobs[quantity].need_time,&jobs[quantity].privilege);jobs[quantity].reach_hour=jobs[quantity].reach_time/100;jobs[quantity].reach_minite=jobs[quantity].reach_time%100;quantity++;}//输出初始作业数据cout<<"初始作业数据:"<<endl;cout.setf(2);cout<<setw(10)<<"作业号:"<<setw(12)<<"到达时间:"<<setw(14)<<"所需时间(分):"<<setw(14)<<"优先级:(0>1)"<<endl;for(int i=0;i<quantity;i++){cout<<setw(10)<<jobs[i].number<<setw(12)<<jobs[i].reach_time<<setw(14)<<jobs[i].need_time<<setw(14)<<jobs[i].privilege<<endl;}}lb: cout<<"请选择调度算法:\n";cout<<"\t\t1---先进先出调度算法\n";cout<<"\t\t2---短作业优先\n";cout<<"\t\t3---优先级调度算法\n";cout<<"\t\t4---响应比最高调度算法"<<endl;int flag=0;la: cin>>flag;switch (flag){case 1:FIFO(jobs,quantity); break;//先进先出调度算法case 2:Shorter(jobs,quantity);break;//短作业优先case 3:Privilege(jobs,quantity);break; //优先级调度算法case 4:Excellent(jobs,quantity);break; //响应比最高调度算法default:cout<<"请输入1-4之间的数"<<endl;goto lb;}goto la;return 0;}数据文件:data.txt1 700 40 12 723 32 23 821 25 34 823 21 45 900 15 66 912 10 4总结及程序简介:1.首先将数据存在data.txt文件中,在主函数中读入数据存入数组jobs[]中2.定义类JOB包含属性:int number; //作业个数int reach_time; //作业到达时间int reach_hour;int reach_minite;int need_time; //作业运行时间int privilege; //优先级float excellent;int start_time; //开始时间int wait_time; //等待时间int visited; //已读取次数方法:构造函数JOB()复制函数Copy()实现一个对象2.分别实现调度算法,然后在主函数中依次调用调试结果:word格式-可编辑-感谢下载支持。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
LinkQueue running(LinkQueue Q) /*建立进程就绪函数(进程运行时间到,置就绪状态*/
{
if(k->time==0) {
printf("运行后进程[%s]已完成状态为\"结束\".\n",k->name);
free(k);
}else
{
(k->priority)--;
(k->time)--;
处理机调度算法的实现
处理机调度算法的实现
1.设定系统中有五个进程,每一个进程用一个进程控制块表示。
2.输入每个进程的“优先数”和“要求运行时间”,
3.为了调度方便,将五个进程按给定的优先数从大到小连成就绪队列。用一单元指出队列首进程,用指针指出队列的连接情况。
4.处理机调度总是选队首进程运行。采用动态优先数算法,进程每运行一次优先数就减“1”,同时将运行时间减“1”。
}
LinkQueue input(LinkQueue Q) /*建立进程控制块函数*/
{
int i;
for(i=1;i<=5;i++)
{
printf("\n进程号No.%d:\n",i);
k=(PCB *)malloc(sizeof(PCB));
printf("\n输入进程名:");
scanf("%s",k->name);
temp2=temp1->next;
}
if(temp2->next==NULL && temp2->priority>=p->priority) {
temp2->next=p;
Q.rear=p;
} else {
p->next=temp1->next;
temp1->next=p;
}
}
return Q;
5.若要求运行时间为零,则将其状态置为“结束”,且退出队列。
6.运行所设计程序,显示或打印逐次被选中进程的进程名以及进程控制块的动态变化过程。
#include <stdio.h>
#include <stdlib.h>
struct PCB {
char name[10];
int priority,time;
{
int h=0;
LinkQueue P;
P=init();
P=input(P);
check(P);
while((P.front->next)!=NULL) {
h++;
k=P.front->next;
P.front->next=k->next;
k->next=NULL;
printf("\n第%d次运行,被选中进程是:%s ",h,k->name);
struct PCB *next;
}*k;
struct LinkQueue {
PCB * front;
PCB * rear;
};
//队列初始化
LinkQueue init(){
LinkQueue Q;
PCB * p;
p=(PCB *)malloc(sizeof(PCB));
if(p) {
Q.front=Q.rear=p;
printf("运行后优先数:%d需要运行时间:%d\n",k->priority,k->time);
Q=sort(Q,k); /*调用sort函数*/
}
return Q;
}
void check(LinkQueue Q) /*建立进程查看函数*/
{ PCB *pr;
pr=(PCB *)malloc(sizeof(PCB));
if(Q.rear==Q.front) {
Q.front->next=p;
Q.rear=p;
} else {
temp1=Q.front;
temp2=temp1->next;
while(temp2->priority>=p->priority && temp2->next!=NULL) {
temp1=temp2;
pr=Q.front->next;
printf("\n ****输入的五个过程为:\n");
while(pr!=NULL) {
printf("\n进程名:%s状态:就绪优先数:%d需要运行时间:%d\n",pr->name,pr->priority,pr->time);
pr=pr->next;
}
}
void main()
Q.front->next=NULL;
return Q;
}else{
printf("队列初始化失败,程序运行终止! \n");
exit(0);
}
}
//插入新进程,使优先数从大到小排列
LinkQueue sort(LinkQueue Q,PCB *p) {
PCB * temp1;
PCB * temp2;
P=running(P);
}
printf("\n\n进程已经完成.\n");
}
printf("\n输入进程优先数:");
scanf("%d",&k->priority);
printf("\n输入进程运行时间:");
scanf("%d",&k->time);
printf("\n");
k->next=NULL;
Q=sort(Q,k); /*调用sort函数*/
}
return Q;