C语言模拟CPU调度

合集下载

操作系统进程调度C语言代码

操作系统进程调度C语言代码

操作系统进程调度C语言代码操作系统是计算机系统中的重要组成部分,用于管理和控制计算机资源的分配和使用。

在操作系统中,进程调度是指将系统资源(如 CPU、内存、磁盘、网络等)分配给运行中的进程的过程。

进程调度是操作系统实现多任务、多用户和分时系统的关键。

进程调度的算法有多种。

最常见的是时间片轮转算法、优先级调度算法、最短进程优先算法和先到先服务算法等。

下面我们将介绍一下时间片轮转算法的 C 语言代码实现。

1. 时间片轮转算法时间片轮转算法是一种基于时间片的调度算法,它给每个进程分配一个时间片,当时间片用完后,系统将进程暂停,并将 CPU 分配给下一个进程。

时间片轮转算法可以让所有进程公平地使用 CPU 时间,并且可以避免进程饥饿的情况发生。

2. C 语言代码实现在 C 语言中,可以用结构体来表示一个进程,包括进程 ID、进程状态、优先级、到达时间和需要运行的时间片等属性。

下面是一个简单的进程结构体的定义:```struct Process processes[5];在实现时间片轮转算法之前,需要先实现一个进程调度函数,它的作用是找到就绪进程中优先级最高的进程,并返回它的位置。

下面是一个简单的进程调度函数:```int find_highest_priority_process(struct Process *processes, int n) {int highest_priority = -1;int highest_priority_index = -1;for (int i = 0; i < n; i++) {if (processes[i].state != 2 && processes[i].priority >highest_priority) {highest_priority = processes[i].priority;highest_priority_index = i;}}return highest_priority_index;}```在实现时间片轮转算法之前,需要先定义一些全局变量,包括就绪队列、当前时间、时间片大小和进程数量等。

操作系统进程调度算法(c语言实现)

操作系统进程调度算法(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语言实现

操作系统实验处理机调度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语言实现

调度算法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语言

处理器调度算法1. 介绍处理器调度是操作系统中的一个重要组成部分,它负责决定运行在处理器上的进程顺序。

处理器调度算法的目标是优化系统的性能和资源利用率,通过合理地分配处理器时间,提高系统的响应速度、吞吐量和效率。

在本文中,我们将介绍常见的处理器调度算法,包括先来先服务(FCFS)调度算法、最短作业优先(SJF)调度算法、优先级调度算法、轮转调度算法和多级反馈队列调度算法。

我们将深入探讨每种算法的工作原理、优缺点以及适用场景。

2. 先来先服务(FCFS)调度算法先来先服务是一种最简单的处理器调度算法。

它按照作业到达的顺序依次分配处理器时间。

当一个进程执行完毕或阻塞时,下一个进程将被执行。

该算法的优点是实现简单、公平性强,适用于长时间作业和对响应时间要求不高的场景。

然而,FCFS算法的缺点是平均等待时间较长,不能满足短作业优先的需求。

3. 最短作业优先(SJF)调度算法最短作业优先调度算法是根据作业的执行时间来决定进程的执行顺序。

执行时间短的作业优先执行,从而减少平均等待时间。

SJF算法的优点是能够最大限度地提高系统的吞吐量和效率。

然而,它可能导致长作业等待时间过长,产生饥饿的问题。

此外,因为需要预测作业的执行时间,所以实现比较困难。

4. 优先级调度算法优先级调度算法根据进程的优先级来决定执行顺序。

每个进程有一个优先级,优先级高的进程先被执行。

可以通过静态优先级或动态优先级来确定进程的优先级。

该算法的优点是能够根据系统需求调整每个进程的优先级,适用于有紧急任务和高优先级任务的场景。

然而,如果进程的优先级设置不当或调度策略不合理,可能会导致优先级反转的问题。

5. 轮转调度算法轮转调度算法是一种基于时间片的调度算法。

每个进程被分配一个固定大小的时间片,当时间片用完后,进程被暂停并放入队列尾部,等待下一次调度。

轮转调度算法的优点是公平性好,能够合理分配处理器时间,适用于响应时间要求不高的场景。

但是,如果时间片过大,则可能导致长时间作业等待时间过长;如果时间片过小,则会导致上下文切换频繁,影响系统性能。

c语言实现进程调度算法

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语言模拟

操作系统进程调度优先级算法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`时,所有进程执行完毕。

用c语言编写进程调度的算法

用c语言编写进程调度的算法

用c语言编写进程调度的算法进程调度是计算机操作系统中一个很重要的组成部分。

目的是通过选择适当的策略,合理地分配处理器时间,保证各个进程有公正的机会去执行它们的任务,从而达到提高系统性能,提高使用量的目的。

进程调度算法根据不同的策略的不同优先级进行分类,并根据优先级来确定在什么时候该执行进程。

下面,我们将讨论一下基于策略的进程调度算法。

1.先入先出调度(FIFO)先入先出调度算法是一种最基本的调度算法。

该算法的执行方式是根据进程请求进入内存的时间顺序,进行排队,进入队列最开始的进程被分配CPU处理时间并运行,直到进程完成或发生中断或问题撤销。

但是,这种方法存在一个明显的缺陷-平均等待时间相对较长,而优先级较高的任务需要等待一段时间才能执行。

当有一个长时间的任务进入系统时,整个系统的响应时间也很长。

2.短作业(SJF)调度短作业优先(SJF)调度算法,是指根据任务在CPU上运行所需的时间长度(即任务的长度)来选择进程。

以开始时间和运行时间和作业大小为基础的调度算法,目的是为了尽可能地减少平均等待时间和平均花费时间。

该算法中,进程会依照作业长度被分配执行时间。

因此,若进程需要执行较长的作业,可能会将优先权移交给较短的作业,而导致长时间等待。

3.基于优先级的调度算法基于优先级的调度算法是指根据不同作业或进程的优先级,选定当前优先级最高的进程进行执行。

该算法的优先级分类方式可以分为静态优先级和动态优先级。

静态优先级:这种算法分先分配好不同进程的不同优先级,每个进程都有自己的优先级。

在动态运行过程中,进程优先级不发生变化。

该算法的缺点是对于散乱的进程和许多小型进程,此算法的相反效果很快变现。

动态权重算法:这种算法分配给程序一个初始权重,不断根据进程的相对优先级而减小或增加权重。

当进程被选中或被执行时,权重会下降。

进程被阻止或者等待时,权重会增加使之在下一次调度时有更高的几率获得执行的机会。

4.多级反馈调度算法多级反馈调度算法是一种渐进式算法。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

C语言模拟CPU调度C语言模拟CPU调度CPU在处理多个进程时,要根据各种情况对处理的进程进行调度。

这其中就包括对各个进程优先级的处理,和调度算法的处理。

下面这个C语言程序,是我在大学期间学习《操作系统》课程的CPU调度时编写的,模拟了CPU的两种调度模式和各种模式所对应的多种调度算法。

为了模拟得更为形象,采用了图形屏幕输出。

#include&lt;stdlib.h&gt;#include&lt;stdio.h&gt;#include&lt;conio.h&gt;#include&lt;graphics.h&gt;#include&lt;math.h&gt;#include&lt;dos.h&gt;#define NULL 0/*-----------------------------------------------------------------*/ struct event /*事件结点结构*/{int evtype; /*1:进程产生。

2:进程执行。

3:激活阻塞进程。

4:进程执行完5:进程阻塞* 6:返回事件*/int pnum; /*执行该事件的进程号*/int t; /*事件发生的时间*/int ifblock; /*如果是执行事件标准其有无阻塞,其它事件时无定义*/struct event *next;};struct process /*进程结点结构*/{int pnum; /*进程号*/int plong; /*进程长度*/int prior; /*进程优先级*/int blocknum; /*进程当前的阻塞数*/int runtime; /*执行次数*/struct process *next;};struct headnod /*队列头结点结构*/{struct process *head; /*队列头*/int totalpro; /*队列中的进程数*/struct process *tail; /*队列尾*/};/*============================================ =====================*/int Mode,Algorithm; /*选择的调度模式和算法*/ struct event *evhead; /*事件链表的头结点*/struct headnod *ready,*block; /*就绪队列、阻塞队列的头结点*/main(){int gdriver,gmode;struct process *runpro=NULL; /*当前正在执行的进程*/ struct process *wakepro; /*当前被唤醒的进程*/ struct process *newpro; /*新建进程*/int busy=0; /*标志cpu状态*/menu(); /*选择调度模式和算法的菜单*/gdriver=DETECT;initgraph(&amp;gdriver, &amp;gmode, "");setbkcolor(LIGHTBLUE);cleardevice();welcome(); /*显示欢迎屏幕*/start(); /*初始化*/while(evhead!=NULL){switch(evhead-&gt;evtype){case 1: /*产生新进程并在就绪队列中排队*/{randomize();newpro=(struct process *)malloc(sizeof(struct process));newpro-&gt;pnum=evhead-&gt;pnum;newpro-&gt;plong=rand()%3+1;if((Mode==2)&amp;&amp;(Algorithm==2))newpro-&gt;prior=rand()%3+1;elsenewpro-&gt;prior=0; /*优先级相等*/if((Mode==2)&amp;&amp;(Algorithm==1)){newpro-&gt;runtime=newpro-&gt;plong+1;newpro-&gt;blocknum=rand()%2;}else{newpro-&gt;blocknum=rand()%3;newpro-&gt;runtime=1;}newpro-&gt;next=NULL;drawcreate(newpro); /*画出产生一个新进程*/Orderp(1,newpro);drawQ(1);sleep(1);if(ready-&gt;head==newpro){if(busy==0)Runevent();elseif((Mode==2)&amp;&amp;(Algorithm==2)&amp;&amp;(newp ro-&gt;prior&gt;runpro-&gt;prior)){Interupt(runpro);returnev(runpro);}}}break;case 2: /*执行事件*/ {runpro=ready-&gt;head;ready-&gt;head=ready-&gt;head-&gt;next; runpro-&gt;next=NULL;ready-&gt;totalpro--;busy=1;Run(runpro); /*画进入cpu*/ drawQ(1);sleep(1);if(evhead-&gt;ifblock==1)blockev(runpro);else{runpro-&gt;runtime--;if(runpro-&gt;runtime==0)overev(runpro); /*产生结束事件*/elsereturnev(runpro);}}break;case 3: /*激活事件*/ {wakepro=block-&gt;head;block-&gt;head=block-&gt;head-&gt;next;wakepro-&gt;next=NULL;block-&gt;totalpro--;wakeup(wakepro);/*移动激活进程*/if(block-&gt;totalpro!=0){drawQ(2);sleep(1);}Orderp(1,wakepro);drawQ(1);sleep(1);if(ready-&gt;head==wakepro){if(busy==0)Runevent();elseif((Mode==2)&amp;&amp;(Algorithm==2)&amp;&amp;(wake pro-&gt;prior&gt;runpro-&gt;prior)){Interupt(runpro);returnev(runpro);}}wakepro=NULL;if(block-&gt;totalpro!=0)wakeupev();}break;case 4: /*结束事件*/{over(runpro);runpro=NULL;busy=0;if(ready-&gt;totalpro!=0)Runevent();}break;case 5: /*阻塞事件*/ {blocked(runpro);busy=0;if(ready-&gt;totalpro!=0)Runevent();Orderp(2,runpro);drawQ(2);sleep(1);if(block-&gt;head==runpro)wakeupev();runpro=NULL;}break;case 6: /*返回事件*/ {returnwait(runpro); /*画出返回*/ busy=0;Orderp(1,runpro);runpro=NULL;drawQ(1);sleep(1);Runevent();}break;}evhead=evhead-&gt;next;}setcolor(LIGHTRED);settextstyle(1,0,0);setusercharsize(1, 1, 2, 1);outtextxy(290,340,"END!");setcolor(15);settextstyle(1,0,1);outtextxy(105,420,"Author : ZHOUWUBO CLASS : 01-1 NO.17");getch();getch();closegraph();}/*============================================ =====================*/menu() /*选择菜单*/{char wrong;textbackground(LIGHTBLUE);clrscr();window(20,3,60,23);textbackground(LIGHTRED);textcolor(YELLOW);clrscr();gotoy(2,2);cprintf("Please select the Scheduling Mode:");gotoxy(5,3);cprintf("1.Non-Preemptive");gotoxy(5,4);cprintf("2.Preemptive");gotoxy(7,5);cprintf("&gt;&gt; ");cscanf("%d",&amp;Mode);switch(Mode){case 1:{gotoxy(2,7);cprintf("Please select the Scheduling Algorithm:");gotoxy(5,8);cprintf("1.FCFS");gotoxy(5,9);cprintf("2.SPF");gotoxy(7,10);cprintf("&gt;&gt; ");cscanf("%d",&amp;Algorithm);if((Algorithm!=1)&amp;&amp;(Algorithm!=2)){gotoxy(2,12);cprintf("Your select is wrong! Run once again!");scanf("%c",&amp;wrong);}break;}case 2:{gotoxy(2,7);cprintf("Please select the Scheduling Algorithm:");gotoxy(5,8);cprintf("1.Round Robin");gotoxy(5,9);cprintf("2.Priority");gotoxy(7,10);cprintf("&gt;&gt; ");cscanf("%d",&amp;Algorithm);if((Algorithm!=1)&amp;&amp;(Algorithm!=2)){gotoxy(2,12);cprintf("Your select is wrong! Run once again!");scanf("%c",&amp;wrong);}break;}default:{gotoxy(2,7);cprintf("Your select is wrong! Run once again!"); scanf("%c",&amp;wrong);}}/*-----------------------------------------------------------------*/ welcome() /*显示欢迎屏幕*/{int i;setcolor(14);outtextxy(45,178,"create");outtextxy(40,188,"process");outtextxy(555,210,"Finish");line(100,204,170,204);line(170,204,160,199);line(160,199,165,204);line(100,226,170,226);line(170,226,160,231);line(160,231,165,226);rectangle(370,40,420,60);rectangle(425,40,475,60);rectangle(480,40,530,60);rectangle(535,40,588,60);setcolor(LIGHTRED);if((Mode==1)&amp;&amp;(Algorithm==1))int arw[8]={395,62,385,77,405,77,395,62};setlinestyle(0, 0, 3);drawpoly(4,arw);}if((Mode==1)&amp;&amp;(Algorithm==2)) {int arw[8]={450,62,440,77,460,77,450,62};setlinestyle(0, 0, 3);drawpoly(4,arw);}if((Mode==2)&amp;&amp;(Algorithm==1)) {int arw[8]={505,62,495,77,515,77,505,62};setlinestyle(0, 0, 3);drawpoly(4,arw);}if((Mode==2)&amp;&amp;(Algorithm==2)) {int arw[8]={561,62,551,77,571,77,561,62};setlinestyle(0, 0, 3);drawpoly(4,arw);setlinestyle(0, 0, 1);settextstyle(2,0,7);outtextxy(373,40,"FCFS");outtextxy(434,40,"SPF");outtextxy(495,40,"RR");settextstyle(2,0,5);outtextxy(537,42,"Priorty"); settextstyle(1,0,0);setusercharsize(1, 1, 2, 1);outtextxy(82,18, "CPU SCHEDULING"); outtextxy(280,360,"start!");setcolor(14);line(220,204,230,204);line(230,204,230,200);line(230,200,380,200);line(380,200,380,204);line(380,204,390,204);line(390,204,424,180);line(424,180,424,170);line(456,180,456,100);line(456,100,180,100);line(180,204,175,194);line(175,194,180,199);line(456,180,490,204);line(490,204,510,204); settextstyle(1,0,1);outtextxy(275,180,"READY"); line(220,226,230,226);line(230,226,230,230);line(230,230,380,230);line(380,230,380,226);line(380,226,390,226);line(390,226,424,250);line(424,250,424,260);line(456,250,456,330);line(456,330,410,330);line(410,330,410,334);line(410,334,260,334);line(260,334,260,330);line(260,330,180,330);line(180,330,180,226);line(180,226,175,236);line(420,308,410,308);line(410,308,410,304);line(410,304,260,304);line(260,304,260,308);line(260,308,250,308);line(456,250,490,226);line(490,226,510,226);outtextxy(305,285,"BLOCK");circle(440,215,30);sleep(2);setcolor(LIGHTBLUE);for(i=0;i&lt;=43;i++){line(280+i,360,280+i,420);line(365-i,360,365-i,420);delay(5000);}}/*-----------------------------------------------------------------*/ start() /*初始化*/{int i;int t;struct event *p1,*p2;p1=(struct event *)malloc(sizeof(struct event));p1-&gt;evtype=1;p1-&gt;pnum=1;p1-&gt;t=0;p1-&gt;ifblock=0; /*在进程产生事件中无意义*/ p1-&gt;next=NULL;t=p1-&gt;t;evhead=p1;randomize();for(i=2;i&lt;=5;i++){p2=(struct event *)malloc(sizeof(struct event)); p2-&gt;evtype=1;p2-&gt;pnum=i;p2-&gt;t=t+rand()%2+1;p2-&gt;ifblock=0;p2-&gt;next=NULL;t=p2-&gt;t;p1-&gt;next=p2;p1=p1-&gt;next;}ready=(struct headnod*)malloc(sizeof(struct headnod));ready-&gt;head=NULL;ready-&gt;totalpro=0;ready-&gt;tail=NULL;block=(struct headnod*)malloc(sizeof(struct headnod));block-&gt;head=NULL;block-&gt;totalpro=0;block-&gt;tail=NULL;}/*-----------------------------------------------------------------*/ drawcreate(struct process *p) /*画出进程的产生过程*/ {void *buf;int i,size;setfillstyle(1,p-&gt;pnum);bar(56,206,55+10*p-&gt;plong,224);size=imagesize(55,205,56+10*p-&gt;plong,225);buf=malloc(size);getimage(55,205,56+10*p-&gt;plong,225,buf);for(i=0; i&lt;134; i++){putimage(55+i,205,buf,COPY_PUT);delay(3500);}free(buf);}/*-----------------------------------------------------------------*/ Orderp(int flag,struct process *p)/*1:就绪队列排队2:阻塞队列排队*/{struct process *q;if(flag==1) /*就绪队列*/{if(ready-&gt;totalpro==0){ready-&gt;head=p;ready-&gt;tail=p;ready-&gt;totalpro++;}else{if((Mode==1)&amp;&amp;(Algorithm==1)) /*FCFS*/{ready-&gt;tail-&gt;next=p;ready-&gt;tail=ready-&gt;tail-&gt;next;ready-&gt;totalpro++;}if((Mode==1)&amp;&amp;(Algorithm==2)) /*短进程优先*/{if(p-&gt;plong&lt;ready-&gt;head-&gt;plong) /*长度小于当前队头*/{p-&gt;next=ready-&gt;head;ready-&gt;head=p;ready-&gt;totalpro++;}else /*长度大于当前队头*/{q=ready-&gt;head;while(q-&gt;next!=NULL){if(p-&gt;plong&lt;q-&gt;next-&gt;plong) {p-&gt;next=q-&gt;next;q-&gt;next=p;ready-&gt;totalpro++;break;}elseq=q-&gt;next;}if(q-&gt;next==NULL){q-&gt;next=p;ready-&gt;totalpro++;ready-&gt;tail=p;}}}if((Mode==2)&amp;&amp;(Algorithm==1)) /*时间片轮转*/{ready-&gt;tail-&gt;next=p;ready-&gt;tail=ready-&gt;tail-&gt;next;ready-&gt;totalpro++;if((Mode==2)&amp;&amp;(Algorithm==2)) /*抢占优先级*/{if(p-&gt;prior&gt;ready-&gt;head-&gt;prior) /*优先级高于当前队头*/{p-&gt;next=ready-&gt;head;ready-&gt;head=p;ready-&gt;totalpro++;}else /*优先级低于当前队头*/{q=ready-&gt;head;while(q-&gt;next!=NULL){if(p-&gt;prior&gt;q-&gt;next-&gt;prior) {p-&gt;next=q-&gt;next;q-&gt;next=p;ready-&gt;totalpro++;break;elseq=q-&gt;next;}if(q-&gt;next==NULL) {q-&gt;next=p;ready-&gt;totalpro++;ready-&gt;tail=p;}}}}}if(flag==2) /*阻塞队列*/{if(block-&gt;totalpro==0){block-&gt;head=p;block-&gt;totalpro++;block-&gt;tail=p;}else{block-&gt;tail-&gt;next=p;block-&gt;tail=block-&gt;tail-&gt;next;block-&gt;totalpro++;}}}/*-----------------------------------------------------------------*/ drawQ(int flag) /*画出队列,flag=1:就绪队列,flag=2:阻塞队列*/{int i,j;struct process *p;if(flag==1){setcolor(LIGHTBLUE);for(i=0;i&lt;=150;i++)line(230+i,205,230+i,225);for(i=0;i&lt;=30;i++)line(189+i,203,189+i,227);p=ready-&gt;head;j=379;for(i=1;i&lt;=ready-&gt;totalpro;i++) {setfillstyle(1,p-&gt;pnum);bar(j,206,j-10*p-&gt;plong+1,224);j=j-10*p-&gt;plong-1;p=p-&gt;next;}}if(flag==2){setcolor(LIGHTBLUE);for(i=0;i&lt;=150;i++)line(260+i,309,260+i,39);for(i=0;i&lt;=30;i++)line(420+i,308,420+i,329);p=block-&gt;head;j=261;for(i=1;i&lt;=block-&gt;totalpro;i++) {setfillstyle(1,p-&gt;pnum);bar(j,309,j+10*p-&gt;plong-1,328);j=j+10*p-&gt;plong+1;p=p-&gt;next;}}}/*-----------------------------------------------------------------*/ Runevent() /*产生执行事件*/{struct event *p1;struct process *p;p=ready-&gt;head;randomize();p1=(struct event *)malloc(sizeof(struct event));p1-&gt;evtype=2;p1-&gt;pnum=p-&gt;pnum;p1-&gt;t=evhead-&gt;t;if(p-&gt;blocknum!=0){p1-&gt;ifblock=rand()%2;if(p1-&gt;ifblock==1)p-&gt;blocknum--;}elsep1-&gt;ifblock=0;p1-&gt;next=NULL;Orderev(p1);}/*-----------------------------------------------------------------*/ Interupt(struct process *p) /*中断当前正在执行的进程*/ {struct event *q1,*q2;if(evhead-&gt;next==NULL){if(evhead-&gt;evtype==4)p-&gt;runtime++;evhead=NULL;}else{q1=evhead-&gt;next;q2=evhead;}while(q1!=NULL){if(q1-&gt;pnum==p-&gt;pnum){if(q1-&gt;evtype==4)p-&gt;runtime++;q2-&gt;next=q1-&gt;next;break;}else{q2=q1;q1=q1-&gt;next;}}}/*-----------------------------------------------------------------*/ Orderev(struct event *q) /*在事件链表中插入结点*/ {struct event *p;p=evhead;while(p-&gt;next!=NULL){if(q-&gt;t&lt;p-&gt;next-&gt;t){q-&gt;next=p-&gt;next;p-&gt;next=q;break;}elsep=p-&gt;next;}if(p-&gt;next==NULL)p-&gt;next=q;}/*----------------------------------------------------------------*/ blockev(struct process *p) /*产生阻塞事件*/{struct event *q;randomize();q=(struct event *)malloc(sizeof(struct event));q-&gt;evtype=5;q-&gt;pnum=p-&gt;pnum;q-&gt;t=evhead-&gt;t+rand()%4+2;q-&gt;ifblock=0;q-&gt;next=NULL;Orderev(q);}/*-----------------------------------------------------------------*/ blocked(struct process *p) /*画出阻塞过程*/{int i,size;void *buf;setcolor(LIGHTBLUE);for(i=1;i&lt;=28;i++)circle(440,215,i);setfillstyle(1,p-&gt;pnum);bar(426,251,426+10*p-&gt;plong-2,269);size=imagesize(425,250,426+10*p-&gt;plong-1,270);buf=malloc(size);getimage(425,250,426+10*p-&gt;plong-1,270,buf);for(i=0; i&lt;59; i++){putimage(425,250+i,buf,COPY_PUT);delay(3500);}for(i=0;i&lt;5;i++){putimage(425-i,308,buf,COPY_PUT);delay(3500);}free(buf);}/*-----------------------------------------------------------------*/ wakeupev() /*产生唤醒事件*/{struct event *q;struct process *p;p=block-&gt;head;randomize();q=(struct event *)malloc(sizeof(struct event));q-&gt;evtype=3;q-&gt;pnum=p-&gt;pnum;q-&gt;t=evhead-&gt;t+rand()%3+4;q-&gt;ifblock=0; /*此时无意义*/q-&gt;next=NULL;Orderev(q);}/*-----------------------------------------------------------------*/ Run(struct process *p) /*画出执行*/int i,size;void *buf;size=imagesize(380,205,380-10*p-&gt;plong-1,225);buf=malloc(size);getimage(380,205,380-10*p-&gt;plong-1,225,buf);for(i=0; i&lt;29; i++){putimage(380-10*p-&gt;plong-1+i,205,buf,COPY_PUT);delay(3500);}setcolor(LIGHTBLUE);for(i=0;i&lt;29;i++)line(380+i,205,380+i,225);setcolor(p-&gt;pnum);for(i=1;i&lt;=28;i++)circle(440,215,i);free(buf);}/*-----------------------------------------------------------------*/ overev(struct process *p) /*产生结束事件*/struct event *q;randomize();q=(struct event *)malloc(sizeof(struct event));q-&gt;evtype=4;q-&gt;pnum=p-&gt;pnum;if((Mode==2)&amp;&amp;(Algorithm==1))q-&gt;t=evhead-&gt;t+2; /*时间片为2*/elseq-&gt;t=evhead-&gt;t+3*(p-&gt;plong);/*时间由进程长度确定*/q-&gt;ifblock=0;q-&gt;next=NULL;Orderev(q);}/*-----------------------------------------------------------------*/ returnev(struct process *p) /*产生返回事件*/{struct event *q;randomize();q=(struct event *)malloc(sizeof(struct event));q-&gt;evtype=6;q-&gt;pnum=p-&gt;pnum;if((Mode==2)&amp;&amp;(Algorithm==2))q-&gt;t=evhead-&gt;t;elseq-&gt;t=evhead-&gt;t+2; /*时间片为2*/q-&gt;ifblock=0;q-&gt;next=NULL;Orderev(q);}/*-----------------------------------------------------------------*/ wakeup(struct process *p)/*画出唤醒过程*/{int i,size;void *buf;size=imagesize(260,309,261+10*p-&gt;plong,329);buf=malloc(size);getimage(260,309,261+10*p-&gt;plong,329,buf);for(i=0; i&lt;72; i++){putimage(260-i,309,buf,COPY_PUT);delay(3500);}for(i=0;i&lt;104;i++){putimage(189,309-i,buf,COPY_PUT);delay(3500);}free(buf);}/*-----------------------------------------------------------------*/ returnwait(struct process *p)/*画出返回过程*/{int i,size;void *buf;setcolor(LIGHTBLUE);for(i=1;i&lt;=28;i++)circle(440,215,i);setfillstyle(1,p-&gt;pnum);bar(426,161,426+10*p-&gt;plong-2,179);size=imagesize(425,160,426+10*p-&gt;plong-1,180);buf=malloc(size);getimage(425,160,426+10*p-&gt;plong-1,180,buf);for(i=0; i&lt;59; i++){putimage(425,160-i,buf,COPY_PUT);delay(3500);}for(i=0;i&lt;237;i++){putimage(425-i,101,buf,COPY_PUT);delay(3500);}for(i=0;i&lt;105;i++){putimage(189,101+i,buf,COPY_PUT);delay(3500);}free(buf);}/*-----------------------------------------------------------------*/ over(struct process *p) /*画出结束过程*/{int i,size;void *buf;int k=evhead-&gt;pnum;setcolor(LIGHTBLUE);for(i=1;i&lt;=28;i++)circle(440,215,i);setfillstyle(1,k);bar(476,206,476+10*p-&gt;plong,224);size=imagesize(475,205,476+10*p-&gt;plong+1,225);buf=malloc(size);getimage(475,205,476+10*p-&gt;plong+1,225,buf);for(i=0; i&lt;=45; i++){putimage(475+i,205,buf,COPY_PUT);delay(4000);}setcolor(LIGHTBLUE);for(i=0;i&lt;=31;i++){line(520+i,205,520+i,225);delay(4000);}free(buf);}。

相关文档
最新文档