实验?一 进程调度模拟
?一、实验?目的
通过这次实验, 加深对进程概念的理理解, 进?一步掌握进程状态的转变、 进程调度的策略略及对系统性能的评价?方法。
?二、实验原理理
设计程序模拟进程的时间?片轮转法调度过程。
假设初始状态为:有ready个进程处于就绪状态,有blocked个进程处于阻塞状态。采?用轮转法进程调度算法进?行行调度(调度过程中,假设处于执?行行状态的进程不不会阻塞),且每过counter个时间?片,唤醒处于阻塞队列列队?首的进程。
三、实验要求
1. 上机前认真复习进程调度算法;2. 上机时独?立编程、调试程序;
3. 根据具体实验要求,填写好实验报告(包括思想、 源程序、 实例例运?行行结果)。
四、实验代码分析
队列列分析 - Queue_List
队列列声明
进程信息声明
12345
struct PCB_type {
char name; // 进程号 int state; // 进程状态 int cpu_time; // 需要时间?片数};
进程节点声明
123456
typedef PCB_Type ElemType;typedef struct Node *PtrToNode;struct Node { ElemType PCB; PtrToNode Next;};
进程队列列声明
12345
struct QNode { Position Front; Position Rear;};
typedef struct QNode *Queue;
队列列功能
创建队列列
12345
Queue CreateQueue() {
Queue Q = (Queue) malloc(sizeof(struct QNode)); Q->Front = Q->Rear = NULL; return Q;}
添加队列列节点
12345678910111213
bool AddQ(Queue Q, ElemType X) { PtrToNode item;
item = (PtrToNode) malloc(sizeof(struct Node)); item->PCB = X; item->Next = NULL; if (Q->Rear == NULL) { Q->Front = item; } else {
Q->Rear->Next = item; }
Q->Rear = item; return true;}
删除队列列节点
1234567891011121314151617
ElemType DeleteQ(Queue Q) { Position FrontNode; ElemType FrontElem; if (IsEmpty(Q)) {
cout << \队列列已空\ << endl; } else {
FrontNode = Q->Front; FrontElem = FrontNode->PCB; if (Q->Front == Q->Rear) { Q->Front = Q->Rear = NULL; } else {
Q->Front = Q->Front->Next; }
free(FrontNode); return FrontElem; }}
打印节点
12345678910
void Print(Queue Q) { PtrToNode item; item = Q->Front;
cout << \队列列?长度为 \ << length(Q) << \元素节点名称如下:\; while (item) {
cout << item->PCB->name << \; item = item->Next; }
cout << endl;}
进程调度 - Process_Scheduing.cpp
状态准备
123456789101112131415161718192021222324252627
void state_start() { int n, m; PCB_Type item;
Ready_Q = CreateQueue(); Block_Q = CreateQueue();
cout << \输?入就绪节点个数 (n): \; cin >> n;
cout << \输?入阻塞节点个数 (m): \; cin >> m;
cout << \输?入释放资源所需时间?片(t): \; cin >> t;
cout << \就绪节点准备 -----------\ << endl; for (int i = 0; i < n; i++) {
item = (PCB_Type) malloc(sizeof(struct PCB_type)); cout << \请输?入第 \ << i + 1 << \个就绪节点的信息:\; cin >> item->name >> item->state >> item->cpu_time; AddQ(Ready_Q, item); }
cout << \阻塞节点准备 -----------\ << endl; for (int i = 0; i < m; i++) {
item = (PCB_Type) malloc(sizeof(struct PCB_type)); cout << \请输?入第 \ << i + 1 << \个阻塞节点的信息:\; cin >> item->name >> item->state >> item->cpu_time; AddQ(Block_Q, item); }
cout << endl;}
调度?方法
1
void dispath() {
234567891011121314151617181920212223242526272829303132333435363738
use_cpu = 0; // cpu运?行行时间?片 unuse_cpu = 0; // cpu空闲时间?片 x = 0; // 系统时钟 PCB_Type item; // 零时进程节点
cout << endl << \结果输出 ------------\ << endl; cout << \执?行行序列列:\;
// 就绪队列列和堵塞队列列?至少有?一个不不为空时执?行行
while (!IsEmpty(Ready_Q) || !IsEmpty(Block_Q)) { if (!IsEmpty(Ready_Q)) { // 就绪队列列不不为空时执?行行 // 删除就绪队列列头节点元素,并?用item接收节点中的PCB信息 item = DeleteQ(Ready_Q);
item->state = 2; // 更更改item进程的状态为运?行行 item->cpu_time--; // 进程需运?行行的时间?片?自减 use_cpu++; // cpu运?行行时间?片?自增 cout << item->name << \; // 打印运?行行进程名称 // item进程的需运?行行时间?片?大于零时执?行行 if (item->cpu_time > 0) // 将item进程加?入就绪队列列尾部 AddQ(Ready_Q, item);
else // item->cpu_time <= 0 free(item); // 释放进程资源 } else { // 就绪队列列为空时执?行行 // 就绪队列列为空,cpu空闲时间?片?自增 unuse_cpu++; cout << \; }
// x?自增后与系统释放资源所需时间?片相?比较,相等则执?行行 if (++x == t) {
// 删除阻塞队列列头节点元素,并?用item接收节点中的PCB信息 item = DeleteQ(Block_Q); // 将阻塞进程item添加?至就绪队列列
AddQ(Ready_Q, item); x = 0; // 重置系统时钟 } }
cout << endl;}
计算?方法
1234
void calculate() {
float result = (float) use_cpu / (use_cpu + unuse_cpu) * 100; cout << \利利?用率为:\ << setprecision(3) << result << \ << endl;}
main ?方法
实验一 操作系统 - 进程调度模拟 - 图文



