实验一进程调度

实验性质:设计

建议学时:6学时

实验目的:

通过这次实验,加深对进程概念的理解,进一步掌握进程状态的转变、进程调度的策略及对系统 性能的评价方法。

实验内容;

设计程序模拟进程的轮转法调度过程。假设初始状态为:有n个进程处于就绪状态,有m个进 程处于阻塞状态。釆用轮转法进程调度算法进行调度。调度过程中,假设处于执行状态的进程不会阻 塞,且每过t个时间片系统释放资源,唤醒处于阻塞队列队首的进程。

程序要求如下:

1) 输出系统中进程的调度次序;

2) 计算CPU利用率。

实现提示:

用C语言实现提示:

1) 程序中进程可用PCB表示,其类型描述如下:

struct PCB_type {char name ; 〃进程名
int state ; //进程状态
2   表示“执行"状态
1——表示“就绪”状态
0——表示“阻塞”状态
int cpu_time ; 〃运行需要的CPU时间(需运行的时间片个数)
}

2) 设置两个队列,将处于“就绪”状态的进程PCB挂在队列ready中;将处于“阻塞”状态的 进程PCB挂在队列blocked中。队列类型描述如下:

struct QueueNode{struct PCB_type PCB;
Struct QueueNode *next;
}

并设全程量:

struct QueueNode ready_head=NULL, //ready队列队首指针
*ready_tail=NULL,    //ready队列队尾指针
*blocked_head=NULL,  //blocked队列队首指针
*blocked_tail=NULL;  //blocked队列队尾指针 

3)设计子程序

start_stateO; 〃读入假设的数据,设置系统初始状态
dispathO;    〃模拟调度
calculate。;  //计算 CPU 利用率

实验要求:

1) 上机前仔细编好程序;

2) 上机时独立调试程序;

3) 提交实验报告,包括纸质稿和电子稿两部分。实验报告要求详见实验报告模板。

测试用数据:

n=2 (处于就绪状态的进程的个数)

m=3 (处于阻塞状态的进程的个数)

t=5 (每过t个时间片系统释放资源,唤醒处于阻塞队列队首的进程)

测试用例:

代码展示

#include <iostream>
#include <fstream>
using namespace std;struct PCB_type
{char name;/* 0:阻塞 1:就绪 2:执行 */int state;/* 需要的CPU时间,即运行的时间片个数 */double cpu_time;
};struct QueueNode
{PCB_type PCB;QueueNode *next;
};QueueNode *ready_head = NULL,*ready_tail = NULL,*blocked_head = NULL,*blocked_tail = NULL;int all_time = 0;
int free_time = 0;
double time_slice_len = 1;// 函数声明
void start_state();
void dispath();
void calculate();
void ready_Enqueue(QueueNode *p);
void blocked_Enqueue(QueueNode *p);
void ready_Dequeue();
void blocked_Dequeue();
QueueNode *ready_Front();
QueueNode *blocked_Front();int main()
{start_state();dispath();calculate();system("pause");return 0;
}void start_state()
{ifstream f;f.open("OS\\test1.txt");int ready_pcb_num, blocked_pcb_num;f >> ready_pcb_num;f >> blocked_pcb_num;// 就绪队列初始化for (int i = 0; i < ready_pcb_num; i++){QueueNode *p = new QueueNode;f >> p->PCB.name >> p->PCB.cpu_time;p->PCB.state = 1;p->next = NULL;ready_Enqueue(p);}// 阻塞队列初始化for (int i = 0; i < blocked_pcb_num; i++){QueueNode *p = new QueueNode;f >> p->PCB.name >> p->PCB.cpu_time;p->PCB.state = 0;p->next = NULL;blocked_Enqueue(p);}cout << "The processes in the ready queue are:" << endl;if (ready_head == NULL){cout << "The ready queue is empty";}else{QueueNode *p = ready_head;while (p){cout << p->PCB.name << " " << p->PCB.state << " " << p->PCB.cpu_time << endl;p = p->next;}}// 输入tint t;f >> t;
}void dispath()
{cout << "Start scheduling" << endl;while (ready_head != NULL || blocked_head != NULL){if (ready_head != NULL){QueueNode *tmp = ready_Front();ready_Dequeue();tmp->PCB.state = 2;tmp->PCB.cpu_time -= time_slice_len;all_time++;printf("Time slice %d: process %c scheduling\n", all_time, tmp->PCB.name);if (tmp->PCB.cpu_time < time_slice_len){printf("process %c over!\n", tmp->PCB.name);}else{ready_Enqueue(tmp);}}else{all_time++;free_time++;printf("Time slice %d: Free a slice of time\n", all_time);}if (blocked_head != NULL && all_time % 5 == 0){QueueNode *tmp = blocked_Front();blocked_Dequeue();ready_Enqueue(tmp);}}
}void calculate()
{cout << "CPU Utilization rate: " << ((all_time - free_time) / (double)all_time) * 100 << "%" << endl;
}// 队列实现
void ready_Enqueue(QueueNode *p)
{if (ready_head == NULL){ready_head = ready_tail = p;}else{ready_tail->next = p;ready_tail = p;}
}void blocked_Enqueue(QueueNode *p)
{if (blocked_head == NULL){blocked_head = blocked_tail = p;}else{blocked_tail->next = p;blocked_tail = p;}
}void ready_Dequeue()
{QueueNode *temp = ready_head;if (ready_head == NULL){printf("ready Queue is Empty\n");return;}if (ready_head == ready_tail){ready_head = ready_tail = NULL;}else{ready_head = ready_head->next;}
}void blocked_Dequeue()
{QueueNode *temp = blocked_head;if (blocked_head == NULL){printf("blocked Queue is Empty\n");return;}if (blocked_head == blocked_tail){blocked_head = blocked_tail = NULL;}else{blocked_head = blocked_head->next;}
}QueueNode *ready_Front()
{return ready_head;
}QueueNode *blocked_Front()
{return blocked_head;
}

进程调度 时间片轮转算法相关推荐

  1. 进程调度-时间片轮转算法

    进程调度   一.实验目的 用高级语言编写和调试一个进程调度程序,以加深对进程的概念及继承调度算法的理解. 二.实验内容和要求 设计一个有N个进程并发的进程调度程序,采用时间片轮转算法. Ø      ...

  2. 时间片轮转算法思想(java)

    目录 一.要求 二 .实验原理 阐述多道作业从提交到执行流程说明三级调度的概念.之间的区别与关系: ​ 阐述FCFS.SJF/SPF.HRRN.RR和多级反馈队列调度算法思想.特点及优缺点: 三.实验 ...

  3. c语言测试时间片大小,C语言模拟实现时间片轮转算法和优先级调度算法

    一.目的和要求 进程调度是处理机管理的核心内容.本实验要求用高级语言编写模拟进程调度程序,以便加深理解有关进程控制快.进程队列等概念,并体会和了解优先数算法和时间片轮转算法的具体实施办法. 二.实验内 ...

  4. 计算机cpu轮转时间,CPU时间片轮转算法

    <CPU时间片轮转算法>由会员分享,可在线阅读,更多相关<CPU时间片轮转算法(8页珍藏版)>请在人人文库网上搜索. 1.CPU时间片轮转算法时间片轮转法进行CPU调度一.实验 ...

  5. 时间片轮转算法(RR算法)c++ 数组

    #include<iostream> using namespace std; const int N=10; double daoda[N];//存储到达时间的数组 double fuw ...

  6. 作业调度进程c语言代码,进程调度 时间片轮转调度算法源代码(C语言)

    #include #include #define MAX 5   //进程数量 #define RR 2   //时间片大小 /*时间片轮转算法*/ struct pro { int num; in ...

  7. 时间片轮转算法源代码

    #include<stdio.h> #define MAX 10 struct task_struct {     char name[10];           /*进程名称*/ fl ...

  8. 操作系统实验一 进程调度 FCFS_SJF_HRRN_RR算法

    FCFS先来先服务-SJF短作业优先-HRRN高响应比优先-RR时间片轮转算法的python实现 需要创建input.txt文件在程序目录下 格式如下 A,  0,   4 B,  1,   3 C, ...

  9. 实现时间片轮转算法(模拟)计算机操作系统实验5:进程调度算法模拟-RR

    实验内容: 实现时间片轮转算法(模拟),要求如下: 1.用到的数据结构 /* PCB / struct PCB { pid_t pid;//进程 PID int state; //状态信息,1 表示正 ...

最新文章

  1. php中查询结果展示
  2. 万字长文解读运营商搏击5G:一场比拼财力的三国杀
  3. android 浏览器源码分析,从源码出发深入理解 Android Service
  4. placeholder调整颜色
  5. control theory and application
  6. emacs for windows
  7. win10电脑服务器在哪个文件夹下,Win10桌面背景在哪个文件夹?Win10桌面背景所在文件夹介绍...
  8. javascript基础入门_javascript基础入门学习第一篇
  9. springboot 单测加入参数_spring-boot-单元测试参数数
  10. php 织梦模板 防盗,dedecms 软件下载频道防盗链php代码
  11. android手机系统miui,使用MIUI系统MIUI将无刷机器直接安装在其他品牌的Android手机上...
  12. 【数据仓库】数据仓库的发展史
  13. 基于MATLAB的基础图像分析
  14. 怎样屏蔽掉“网页对话框”
  15. 绘制相同到期日欧式期权组合收益图(python)
  16. Opencv-获取两点之间距离
  17. ITSM系统_CMDB设计_业务要件
  18. 【微信小程序】小程序调起付款码
  19. JSTL 标签大全详解
  20. 华为android9.1.0怎么隐藏应用,华为畅享10s怎么隐藏应用 可以设置应用锁和隐私空间...

热门文章

  1. 双色Hanoi塔问题
  2. TDL3 Source Code
  3. 华为软件开发云测评报告二:代码检查
  4. 分享使用Matchvs的一些感受
  5. 华为三星手机惊艳世界的“背后”,竟然都藏着这家中国企业!
  6. 使用ATTO Disk Benchmarks测试闪迪cz880
  7. ORA-00600 [4185]错误处理过程
  8. 财富、健康双重守护,平安人寿盛世金越尊享终身寿险传递金融温度
  9. C++ 函数模板类模板详解
  10. mysql 批量杀进程脚本