高优先权优先调度算法c语言,实验3高(动态)优先权优先的进程调度算法模拟

《实验3高(动态)优先权优先的进程调度算法模拟》由会员分享,可在线阅读,更多相关《实验3高(动态)优先权优先的进程调度算法模拟(11页珍藏版)》请在人人文库网上搜索。

1、实验 3 高(动态 )优先权优先的进程调度算法模拟1. 实验目的通过动态优先权算法的模拟加深对进程概念和进程调度过程的理解。2. 实验环境装有操作系统 Windows XP和开发工具 VC+6.0,内存在256M以上的微机; 或者:装有Linux(Fedora 7)操作系统和gcc编译器,内存在256M以上的微机。3. 实验内容(1)(2)(3)(4)用 C 语言来实现对 N 个进程采用动态优先权优先算法的进程调度。 每个用来标识进程的进程控制块PCB用结构来描述,包括以下字段:进程标识数 ID;进程优先数PRIORITY并规定优先数越大的进程,其优先权越高;进程已占用的 CPU时间CPU T。

2、IME;进程还需占用的 CPU时间NEEDTIME当进程运行完毕时, NEEDTIME变为0; 进程的阻塞时间STARTBLOCK表示当进程再运行 STARTBLOC个时间片后,进 程将进入阻塞状态;进程被阻塞的时间 BLOCKTIME表示已阻塞的进程再等待 片后,进程将转换成就绪状态;进程状态 STATE; (READY,RUNNING, BLOCK, FINISH)队列指针NEXT,用来将PCB排成队列。 优先数改变的原则:进程在就绪队列中呆一个时间片,优先数增加 进程每运行一个时间片,优先数减假设在调度前,系统中有IDPRIORITYBLOCKTIME个时间3。1;5 个进程,它们的初始。

3、状态如下:138230329CPUTIME NEEDTIME STARTBLOCK03-106-103-104-1BLOCKTIMESTATE为了清楚地观察进程的调度过程,程序应将每个时间片内的进程的情况显示出来,(5)参照的具体格式如下:ID3READY0READY0READY0READY0READYRUNNING PROCESS: $id0READY QUEUE: $id1-$id2BLOCK QUEUE: $id3-$id4FINISH QUEUE: $id0-$id1-$id2-$id3-$id4PRIORITY CPUTIMEXXXXXXXXNEEDTIMEXXXXSTATEXXXX。

4、STARTBLOCK BLOCKTIMEXXXXXXXX2XXXXXXXXXXXX3XXXXXXXXXXXX4XXXXXXXXXXXX4. 实验要求(1) 将源程序 (priority.c) 和程序运行结果写入实验报告。(2) 将该算法执行过程与高响应比优先调度算法的执行过程进行比较。#include #include int changestate0()using namespace std; int i;int j; int s;int m; struct pcbint id;int p; /priority int cputime;int alltime; int startblock;。

5、 int blocktime;int state;struct pcb pro5= 0,9,0,3,2,3,0, 1,38,0,3,-1,0,0, 2,30,0,6,-1,0,0, 3,29,0,3,-1,0,0, 4,0,0,4,-1,0,0;/ 循环值/ 还在阻塞或就绪队列中的进程数/ 最大 priority 的 id/0 表示 ready 1 表示 end -1 表示 blockif(pro0.startblock=0) pro0.state=-1;pro0.startblock-; return 1;if(pro0.blocktime=0)pro0.state=0;return 1;i。

6、f(pro0.state=0&pro0.startblock!=-1)pro0.startblock-;return 1;if(pro0.state=-1&pro0.blocktime!=0) pro0.blocktime-;return 1;int state0() changestate0();s=pro0.p;if(pro0.state=-1)s=-100;return s;int maxp()/ 求出最大 prioritystate0();int max=s;m=pro0.id;for(i=0;iproi.p)max=proi+1.p;m=proi+1.id;return m;void。

7、 change()maxp();int x;/ 得到 m 现在的数组编号for(i=0;ij;i+)proi.p+;for(i=0;ij;i+)if(proi.id=m)x=i; prox.cputime+; prox.p=prox.p-4; prox.alltime-;if(prox.alltime=0)prox.state=1;void display() change(); coutRUNNING PROG:mendl;n;cout=coutID ;for(i=0;ij;i+)cout.width(10); coutproi.id;coutendlPRIORITY ;for(i=0;ij。

8、;i+)cout.width(10);coutproi.p;coutendlCPUTIME ;for(i=0;ij;i+)cout.width(10);coutproi.cputime; coutendlALLTIME ; for(i=0;ij;i+)cout.width(10); coutproi.alltime;coutendlSTARTBLOCK;for(i=0;ij;i+)cout.width(10);coutproi.startblock;coutendlBLOCKTIME ; for(i=0;ij;i+)cout.width(10);coutproi.blocktime; cout。

9、endlSTATE ; for(i=0;ij;i+)cout.width(10);coutproi.state; coutendl;int main() j=5;/ 刚开始有 5 个进程 while(j!=0)for(i=0;ij;i+)if(proi.state=1)for(;ij;i+)proi=proi+1; j=j-1; display(); getchar();运行结果:RUNNING P R0G:1ID01234P RIORITY103531301CPU TIME01000ALLTIME32634STARTBLOCK1-1-1-1-1BLOCKTIME30000STATE00000。

10、RUNNING P ROG:1ID01234P RIORITY113232312CPU TIME02000ALLTIME31634STARTBLOCK0-1-1-1-1BLOCKTIME30000STATE00000RUNNING P ROG:1ID01234P RIORITY122933323CPU TIME030001ALLTIMESTARTBLOCK -1-1-1-1-1BLOCKTIMESTATE -1RUNNING PROG:2IDPRIORITY 133033CPUTIMEALLTIMESTARTBLOCK -1-1-1-1BLOCKTIMESTATE -1RUNNING PROG。

11、:3IDPRIORITY 143130CPUTIMEALLTIMESTARTBLOCK -1-1-1-1BLOCKTIMESTATE -1RUNNING PROG:2IDPRIORITY 152831CPUTIMEALLTIMESTARTBLOCK -1-1-1-1BLOCKTIMESTATE -1RUNNING PROG:3IDPRIORITY 162928CPUTIMEALLTIMESTARTBLOCK -1-1-1-1BLOCKTIMESTATERUNNING PROG:2IDPRIORITY 172629CPUTIMEALLTIMESTARTBLOCK -1-1-1-1BLOCKTIM。

12、ESTATERUNNING PROG:3IDPRIORITY 182726CPUTIMEALLTIMESTARTBLOCK -1-1-1-1BLOCKTIMESTATERUNNING PROG:2IDPRIORITY 192410CPUTIMEALLTIMESTARTBLOCK -1-1-1BLOCKTIMESTATERUNNING PROG:2IDPRIORITY 202111CPUTIMEALLTIMESTARTBLOCK -1-1-1BLOCKTIMESTATERUNNING PROG:2IDPRIORITY 211812CPUTIME 060ALLTIME 304STARTBLOCK-。

13、1-1-1BLOCKTIME000STATE 010RUNNING PROG:0ID 0 4PRIORITY 1813CPUTIME 10ALLTIME 24STARTBLOCK-1-1BLOCKTIME00STATE 00RUNNING PROG:0ID 0 4PRIORITY 1514CPUTIME 20ALLTIME 14STARTBLOCK-1-1BLOCKTIME00STATE 00RUNNING PROG:0ID 0 4PRIORITY 1215CPUTIME 30ALLTIME 04STARTBLOCK-1-1BLOCKTIME00STATE 10RUNNING PROG:4ID4PRIORITY 12CPUTIMEALLTIMESTARTBLOCK -10BLOCKTIMESTATERUNNING PROG:4IDPRIORITYCPUTIMEALLTIMESTARTBLOCK -1BLOCKTIMESTATERUNNING PROG:4IDPRIORITYCPUTIMEALLTIMESTARTBLOCK -1BLOCKTIMESTATERUNNING PROG:4IDPRIORITYCPUTIMEALLTIMESTARTBLOCK -1BLOCKTIMESTATE。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
操作系统中常用的动态优先进程调度算法包括Round-Robin算法、最短进程优先(SPN)算法、最短剩余时间优先(SRTN)算法等等。下面是一个使用Round-Robin算法进程调度程序示例,使用C语言编写: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_PROCESS_NUM 10 // 最大进程数 #define TIME_QUANTUM 4 // 时间片长度 typedef struct Process { char name[10]; // 进程名 int arrival_time; // 到达时间 int burst_time; // 运行时间 int priority; // 优先级 int remaining_time; // 剩余时间 } Process; int main() { int n; // 进程数 Process processes[MAX_PROCESS_NUM]; int current_time = 0; // 当前时间 int completed_num = 0; // 已完成的进程数 int i, j; printf("请输入进程数:"); scanf("%d", &n); for(i = 0; i < n; i++) { printf("请输入第%d个进程的信息:\n", i + 1); printf("进程名:"); scanf("%s", processes[i].name); printf("到达时间:"); scanf("%d", &processes[i].arrival_time); printf("运行时间:"); scanf("%d", &processes[i].burst_time); printf("优先级:"); scanf("%d", &processes[i].priority); processes[i].remaining_time = processes[i].burst_time; } while(completed_num < n) { int min_priority = 10000; // 最小优先级 int min_priority_index = -1; // 最小优先进程的下标 // 找到当前时间所有已到达的进程优先级最进程 for(i = 0; i < n; i++) { if(processes[i].arrival_time <= current_time && processes[i].remaining_time > 0 && processes[i].priority < min_priority) { min_priority = processes[i].priority; min_priority_index = i; } } // 如果当前时间没有已到达的进程,则时间加一 if(min_priority_index == -1) { current_time++; continue; } // 运行当前进程 printf("当前时间:%d,运行进程:%s\n", current_time, processes[min_priority_index].name); processes[min_priority_index].remaining_time -= TIME_QUANTUM; current_time += TIME_QUANTUM; // 判断当前进程是否已完成 if(processes[min_priority_index].remaining_time <= 0) { completed_num++; printf("当前时间:%d,进程:%s已完成\n", current_time, processes[min_priority_index].name); } } return 0; } ``` 该程序中使用了Round-Robin算法,通过循环遍历所有已到达的进程优先级最进程来选择要运行的进程。运行进程时每次使用固定长度的时间片,当进程剩余时间小于等于0时即表示该进程已完成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值