实验内容
分析和探索处理器实施进程调度的前提条件,理解并掌握处理器调度算法的设计原理和实现机制,随机发生和模拟进程创建及相关事件,编程实现基于特定处理器调度算法(三种以上,如先来先服务调度算法、短进程优先调度算法、高优先权优先调度算法、高响应比优先调度算法、时间片轮转调度算法、多级反馈队列调度算法等等)的系统调度处理过程,并加以测试验证。
本实验课题主要功能设计要求包括:
(1)选取和设计实现三种以上的处理器调度算法;
(2)针对特定的处理器调度算法,分析处理器实施进程调度的前提条件和要求(譬如进程创建时刻、运行时间长短、各【集中计算运行/输入输出操作】时间段长短、优先级),并随机发生和模拟处理对应的进程创建及相关事件;
(3)编程实现处理器调度机制,针对特定的处理器调度算法和随机事件序列,给出相应的调度处理过程,主要涵盖进程相关事件、处理器调度操作或处理措施以及各状态进程列表;
(4)测试验证处理器调度机制的有效性及有关处理器调度算法设计方案的正确性。
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
typedef struct PCB{
char name[20];
// 运行时间
int running_time;
// 到达时间
int enter_time;
// 优先级
int priority;
// 完成时间
int done_time; //用于时间片轮转
int copyRunning_time; //用于时间片轮转
// 进程开始运行的时间
int start_time;
struct PCB* next;
} PCB;
typedef struct PCBQueue{
PCB* firstProg;
PCB* LastProg;
int size;
} PCBQueue;
void Queueinit(PCBQueue* queue){
if(queue==NULL){
return;
}
queue->size = 0;
queue->LastProg = (PCB*)malloc(sizeof(PCB));
queue->firstProg = queue->LastProg;
}
void EnterQueue(PCBQueue* queue,PCB* pro){ //加入进程队列
queue->LastProg->next = (PCB*)malloc(sizeof(PCB));
queue->LastProg = queue->LastProg->next;
queue->LastProg->enter_time = pro->enter_time;
// 将name复制给 LastProg
memcpy(queue->LastProg->name,pro->name,sizeof(pro->name));
queue->LastProg->priority = pro->priority;
queue->LastProg->running_time = pro->running_time;
queue->LastProg->copyRunning_time = pro->copyRunning_time;
queue->LastProg->start_time = pro->start_time;
queue->size++;
}
PCB* poll(PCBQueue* queue){
PCB* temp = queue->firstProg->next;
if(temp == queue->LastProg){
queue->LastProg=queue->firstProg;
queue->size--;
return temp;
}
queue->firstProg->next = queue->firstProg->next->next;
queue->size--;
return temp;
}
void inputPCB(PCB pro[],int num){
for(int i=0;i<num;i++){
PCB prog ;
printf("请输入第%d个进程的名字,到达时间 ,服务时间,优先级\n",i+1);
scanf("%s",prog.name);
scanf("%d",&prog.enter_time) ;
scanf("%d",&prog.running_time);
prog.copyRunning_time = prog.running_time;
scanf("%d",&prog.priority);
pro[i]=prog;
}
}
//冒泡排序算法(每次找到最大的放在末尾)
void sortWithEnterTime(PCB pro[],int num){
for(int i=1;i<num;i++){
for(int j= 0;j<num-i;j++){
if(pro[j].enter_time>pro[j+1].enter_time){
PCB temp = pro[j];
pro[j] = pro[j+1];
pro[j+1] = temp;
}
}
}
}
void FCFS(PCB pro[],int num){
printf("进程 到达时间 服务时间 开始时间 完成时间 周转时间 带权周转时间\n");
sortWithEnterTime(pro,num); //按照进入顺序排序
PCBQueue* queue = (PCBQueue*)malloc(sizeof(PCBQueue));
Queueinit(queue);
EnterQueue(queue,&pro[0]);
int time = pro[0].enter_time;
int pronum=1; //记录当前的进程
//平均周转时间
float sum_T_time = 0;
// 带权平均周转时间
float sum_QT_time = 0 ;
while(queue->size>0){
PCB* 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));
}
//按照运行时间排序
void sortWithLongth(PCB pro[],int start,int end){
int len = end - start;
if(len == 1)
return;
for(int i=1;i<len;i++){
for(int j= start;j<end-i;j++){
if(pro[j].running_time>pro[j+1].running_time){
PCB temp = pro[j];
pro[j] = pro[j+1];
pro[j+1] = temp;
}
}
}
}
void SJF(PCB pro[],int num) {
printf("进程 到达时间 服务时间 开始时间 完成时间 周转时间 带权周转时间\n");
sortWithEnterTime(pro,num);
PCBQueue* queue = (PCBQueue*)malloc(sizeof(PCBQueue));;
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){
PCB* 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;
float QT_time = T_time / (curpro->running_time+0.0) ;
sum_T_time += T_time;
sum_QT_time += QT_time;
int pre = pronum;
for(int tt = time;tt<=done_time&&pronum<num;tt++){ //模拟进程的执行过程
if(tt>=pro[pronum].enter_time){ // 统计从此任务开始到结束之间有几个进程到达
pronum++;
}
}
sortWithLongth(pro,pre,pronum);//将到达的进程按照服务时间排序
for(int i=pre;i<pronum;i++){ //将进程链入队列
EnterQueue(queue,&pro[i]);
}
pre = 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);
}
//按照响应比排序(倒序)
void sortWithResponse(PCB pro[],int start,int end){
int len = end - start;
if(len == 1)
return;
for(int i=1;i<len;i++){
for(int j= start;j<end-i;j++){
//计算响应比
float m = (pro[j].start_time-pro[j].enter_time+pro[j].running_time)/(pro[j].running_time+0.0);
float n = (pro[j+1].start_time-pro[j+1].enter_time+pro[j+1].running_time)/(pro[j+1].running_time+0.0);
if(m < n){
PCB temp = pro[j];
pro[j] = pro[j+1];
pro[j+1] = temp;
}
}
}
}
//高响应比优先
void HRRN(PCB pro[],int num) {
printf("进程 到达时间 服务时间 开始时间 完成时间 周转时间 带权周转时间\n");
sortWithEnterTime(pro,num);
PCBQueue* queue = (PCBQueue*)malloc(sizeof(PCBQueue));;
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){
PCB* 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;
float QT_time = T_time / (curpro->running_time+0.0) ;
sum_T_time += T_time;
sum_QT_time += QT_time;
int pre = pronum;
for(int tt = time;tt<=done_time&&pronum<num;tt++){ //模拟进程的执行过程
if(tt>=pro[pronum].enter_time){ // 统计从此任务开始到结束之间有几个进程到达
pronum++;
}
}
sortWithResponse(pro,pre,pronum);//将到达的进程按照响应时间排序
for(int i=pre;i<pronum;i++){ //将进程链入队列
EnterQueue(queue,&pro[i]);
}
pre = 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);
}
//按权重排序
void sortWithPriority(PCB pro[],int start,int end){
int len = end - start;
if(len == 1) return ;
for(int i=1;i<len;i++){
for(int j= start;j<end-i;j++){
if(pro[j].priority>pro[j+1].priority){
PCB temp = pro[j];
pro[j] = pro[j+1];
pro[j+1] = temp;
}
}
}
}
//优先级调度算法
void HPF(PCB pro[],int num){
printf("进程 到达时间 服务时间 开始时间 完成时间 周转时间 带权周转时间\n");
sortWithEnterTime(pro,num);
PCBQueue* queue = (PCBQueue*)malloc(sizeof(PCBQueue));;
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){
PCB* 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;
float QT_time = T_time / (curpro->running_time+0.0) ;
sum_T_time += T_time;
sum_QT_time += QT_time;
int pre = pronum;
for(int tt = time;tt<=done_time&&pronum<num;tt++){ //模拟进程的执行过程
if(tt>=pro[pronum].enter_time){ // 统计从此任务开始到结束之间有几个进程到达
pronum++;
}
}
sortWithPriority(pro,pre,pronum);//将到达的进程按照服务时间排序
for(int i=pre;i<pronum;i++){ //将进程链入队列
EnterQueue(queue,&pro[i]);
}
pre = 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));
}
//时间片轮转调度
void RR(PCB pro[],int num){
printf("请输入时间片大小");
int timeslice;
scanf("%d",×lice);
printf("进程 到达时间 服务时间 进入时间 完成时间 周转时间 带权周转时间\n");
sortWithEnterTime(pro,num);
PCBQueue* queue = (PCBQueue*)malloc(sizeof(PCBQueue));;
Queueinit(queue);
//第一个进程开始运行的时间就是到达时间
pro[0].start_time = pro[0].enter_time;
EnterQueue(queue,&pro[0]);
int time = 0;
int pronum = 1;
float sum_T_time = 0,sum_QT_time = 0;
while(queue->size>0){
PCB* curpro = poll(queue); // 从队列中取出头节点
if(time<curpro->enter_time)
time = curpro->enter_time;
if(timeslice >= curpro->running_time){ // 如果剩余时间小于时间片 则此任务完成
for(int tt = time;tt<=time+curpro->running_time&&pronum<num;tt++){ // 模拟进程的执行过程
if(tt>=pro[pronum].enter_time){ // 统计从此任务开始到结束之间有几个进程到达
pro[pronum].start_time = tt;
EnterQueue(queue,&pro[pronum]);
pronum++;
}
}
time += curpro->running_time;
curpro->running_time = 0;
curpro->done_time = time;
int T_time = curpro->done_time-curpro->start_time;
float QT_time = T_time / (curpro->copyRunning_time+0.0);
sum_T_time += T_time;
sum_QT_time += QT_time;
printf("%s\t%d\t%d\t %d\t %d\t %d\t %.2f\n",curpro->name,curpro->enter_time,curpro->copyRunning_time,
curpro->start_time,curpro->done_time,T_time,QT_time);
if(queue->size==0&&pronum<num){ //防止出现前一个进程执行完到下一个进程到达之间无进程进入
pro[pronum].start_time = pro[pronum].enter_time;
EnterQueue(queue,&pro[pronum]);
pronum++;
}
continue;
}
// 运行时间大于时间片
for(int tt = time;tt<=time+timeslice&&pronum<num;tt++){ //模拟进程的执行过程
if(tt>=pro[pronum].enter_time){ // 统计从此任务开始到结束之间有几个进程到达
pro[pronum].start_time = tt;
EnterQueue(queue,&pro[pronum]);
pronum++;
}
}
time += timeslice;
curpro->running_time -= timeslice;
//当前程序未完成 继续添加到队列中
EnterQueue(queue,curpro);
if(queue->size==0&&pronum<num){ //防止出现前一个进程执行完到下一个进程到达之间无进程进入
pro[pronum].start_time = pro[pronum].enter_time;
EnterQueue(queue,&pro[pronum]);
pronum++;
}
}
printf("平均周转时间为%.2f\t平均带权周转时间为%.2f\n\n",sum_T_time/(num+0.0),sum_QT_time/(num+0.0));
}
void choiceMenu(){
printf("请选择进程调度算法:\n\n");
printf("1.先来先服务算法\n2.短进程优先算法\n3.高优先级优先\n4.时间片轮转算法\n5.高响应比优先算法\n6.退出\n");
}
void menu(){
int proNum;
printf("请输入进程的个数:");
scanf("%d",&proNum);
PCB pro[proNum];
inputPCB(pro,proNum);
choiceMenu();
int choice;
while(1){
scanf("%d",&choice);
switch(choice){
case 1:FCFS(pro,proNum);choiceMenu();break;
case 2:SJF(pro,proNum);choiceMenu();break;
case 3:HPF(pro,proNum);choiceMenu();break;
case 4:RR(pro,proNum);choiceMenu();break;
case 5:HRRN(pro,proNum);choiceMenu();break;
case 6:return;
}
}
}
int main(){
menu();
return 0;
}
输入数据 :
错误总结:
1.数据原文代码复制粘贴出错,用的CSDN那篇代码
二、
完整代码,直接运行,输入参考点击标题
#include<stdio.h>
#include<stdlib.h>
struct nodespace{
int teskid; // 作业号
int begin; // 开始地址
int size; // 大小
int status; // 状态 0代表占用,1代表空闲
struct nodespace *next; // 后指针
};
void initNode(struct nodespace *p){
if(p == NULL){ //如果为空则新创建一个
p = (struct nodespace*)malloc(sizeof(struct nodespace));
}
p->teskid = -1;
p->begin = 0;
p->size = 640;
p->status = 1;
p->next =NULL;
}
void myMalloc1(int teskid,int size,struct nodespace *node){
while(node != NULL){
if(node->status == 1){ //空闲的空间
if(node->size > size){ //当需求小于剩余空间充足的情况
//分配后剩余的空间
struct nodespace *p = (struct nodespace*)malloc(sizeof(struct nodespace));
p->begin = node->begin + size;
p->size = node->size - size;
p->status = 1;
p->teskid = -1;
//分配的空间
node->teskid = teskid;
node->size = size;
node->status = 0;
//改变节点的连接
p->next = node->next;
node->next = p;
printf("==================================分配内存成功!==================================\n");
break;
}else if(node->size == size){ //需求空间和空闲空间大小相等时
node->teskid = teskid;
node->size = size;
node->status = 0;
printf("==================================分配内存成功!==================================\n");
break;
}
}
if(node->next == NULL){
printf("===============================分配失败,没有足够的空间!=============================\n");
break;
}
node = node->next;
}
}
void myFree(int teskid,struct nodespace *node){
if(node->next == NULL && node->teskid == -1){
printf("================================您还没有分配任何作业!================================\n");
}
while(node != NULL){
if(node->status == 1 && node->next->status ==0 && node->next->teskid == teskid){
struct nodespace *q = node->next;
node->next = node->next->next;
free(q);
printf("==================================释放内存成功!==================================\n");
if(node->next->status == 1){ //下一个空间是空闲空间时
node->size = node->size + node->next->size;
struct nodespace *q = node->next;
node->next = node->next->next;
free(q);
printf("==================================释放内存成功!==================================\n");
}
break;
}else if(node->status == 0 && node->teskid == teskid){ //释放空间和空闲空间不连续时
node->status = 1;
node->teskid = -1;
if(node->next != NULL && node->next->status == 1){ //下一个空间是空闲空间时
node->size = node->size + node->next->size;
struct nodespace *q = node->next;
node->next = node->next->next;
free(q);
}
printf("==================================释放内存成功!==================================\n");
break;
}else if(node->next == NULL){ //作业号不匹配时
printf("==================================没有此作业!!==================================\n");
break;
}
node = node->next;
}
}
void printNode(struct nodespace *node){
printf(" 内存情况 \n");
printf(" -------------------------------------------------------\n");
printf("| 起始地址\t结束地址\t大小\t状态\t作业号\t|\n");
while(node != NULL){
if(node->status==1){
printf("| %d\t\t%d\t\t%dKB\tfree\t 无\t|\n", node->begin + 1, node->begin+node->size, node->size);
}else{
printf("| %d\t\t%d\t\t%dKB\tbusy\t %d\t|\n", node->begin + 1, node->begin+node->size, node->size, node->teskid);
}
node = node->next;
}
printf(" -------------------------------------------------------\n");
}
void destory(struct nodespace *node){
struct nodespace *q = node;
while(node != NULL){
node = node->next;
free(q);
q = node;
}
}
void menu(){
printf("\n");
printf("\t\t\t\t ╭═════════════════════════════════○●○●═══╮\n");
printf("\t\t\t\t │ 首次适应算法的动态分区分配方式模拟 │\n");
printf("\t\t\t\t ╰═══○●○●═════════════════════════════════╯\n");
printf("\t\t\t\t ┌───────────────────────────────────────────-┐\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 1. 申请内存 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 2. 回收内存 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 3. 查看内存情况 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 4. 退出 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t └────────────────────────────────────────────┘\n");
printf("\t\t\t\t\t\t 请您选择(1-4):\t");
}
int main(){
// node为整个空间
system("color 0f");
//system("mode con cols=120 lines=50");
struct nodespace *init = (struct nodespace*)malloc(sizeof(struct nodespace));
struct nodespace *node = NULL;
initNode(init); //初始化主链
node = init; //指向链表头
int option;
int teskid;
int size;
while(1){
menu(); //打印想要进行的操作
scanf("%d",&option);
if(option == 1){
printf("请输入作业号;");
scanf("%d",&teskid);
printf("此作业申请的空间大小(KB):");
scanf("%d",&size);
myMalloc1(teskid,size,node);
printf("\n");
printNode(node);
}else if(option == 2){
printf("请输入作业号:");
scanf("%d",&teskid);
myFree(teskid,node);
printf("\n");
printNode(node);
}else if(option == 3){
printNode(node);
}else if(option == 4){
destory(node);
initNode(init);
node = init;
break;
}else{
printf("===========================您的输入有误,请重新输入!============================\n");
continue;
}
}
return 0;
}
操作系统实验:页面置换算法的模拟实现及命中率对比(学习笔记) | 码农家园
操作系统实验三:内存管理(动态分区分配方式的模拟)_望不秃的博客-CSDN博客
动态分区式内存管理(完整代码)_动态分区分配存储管理_Cheney822的博客-CSDN博客
操作系统——连续动态内存管理模拟实现_weixin_43774168的博客-CSDN博客
四、实验四 页面置换算法的模拟实现与比较
完整代码,直接运行,输入参考点击标题
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#define R 32 //物理内存块数
#define V 64 //虚拟内存块数
using namespace std;
typedef struct LNode{
int data;
int flag;
int modify;
}LNode;
typedef struct PNode{
int data;
int flag;
int modify;
PNode* next;
}PNode;
typedef struct Link{
int num;
PNode* next;
}Link;
typedef struct node{
int num;
node* next;
} Node, *pNode;
typedef struct queue{
int n;
pNode front;
pNode rear;
} Queue, *pQueue;
void Generate();
void InitMemo();
bool IsInMemo (int n);
void Optimal (int n);
void MainOptimal();
void LRU (int n);
void MainLRU();
bool IsInNodes (int n);
void UpdatedClock (int n);
void MainClock();
void InitQueue (pQueue q);
void QPush (pQueue q, int num);
void QPop (pQueue q);
void QDestroy (pQueue q);
bool FindQueue (pQueue q, int num);
void FIFO (pQueue q, int num);
void MainFIFO();
void AddLink (int data, int type);
void FreeIdle();
void FreeMod();
void PBA (int n);
void MainPBA();
bool IsInPNodes (int n);
int Block = 3;
int Access[32]; //访问序列
int* Memo;
int Lost = 0;
int Index = 0;
LNode* Nodes;
int Size = 3;//分配的内存
int p;//工作集起始位置
int table[32];//物理内存
PNode* Pnodes;
Link Idle;
Link Modified;
int main (){
Generate();
MainOptimal();
MainLRU();
MainClock();
MainFIFO();
MainPBA();
cout << "*******************************************************************" << endl;
cout << "访问序列:" << endl;
for (int i = 0; i < 32; i++){
cout << Access[i] << ", ";
}
return 0;
}
void Generate(){
srand ( (unsigned) time (NULL)); //用时间产生随机数
int p = rand() % 64;
int m = 8, e = 8;
int i, j;
double t;
t = rand() % 10 / 10.0;
for (i = 0; i < 4; i++){
for (j = i * m; j < (i + 1) *m; j++)
Access[j] = (p + rand() % e) % 64;
double r = (rand() % 10) / 10.0;
if (r < t)
p = rand() % 64;
else
p = (p + 1) % 64;
}
}
void InitMemo(){
Memo = (int*) malloc (Block * sizeof (int));
int i = 0;
for (; i < Block; i++)
Memo[i] = -1;
return;
}
void MainOptimal(){
InitMemo();
int i = 0;
cout << "******************************************************************" << endl;
cout << "最佳置换算法:" << endl;
Lost = 0;
for (; i < 32; i++){
Optimal (i);
cout << Memo[0] << " " << Memo[1] << " " << Memo[2] << endl;
}
cout << "最佳置换算法缺页率:" << Lost / 32.0 << " " << Lost << endl;
Lost = 0;
free (Memo);
Index = 0;
}
bool IsInMemo (int n){
for (int i = 0; i < Block; i++){
if (Access[n] == Memo[i]){
return true;
}
}
return false;
}
void Optimal (int n){
int i = 0, j = 0;
if (IsInMemo (n))
cout << "页面已被调入" << endl;
else
if (Index == Block){
Lost++;
int max = 0, Pos, Tag;
for (i = 0; i < Block; i++){
Tag = -1;
for (j = n + 1; j < 32; j++){
if (Access[j] == Memo[i]){
Tag = j;
break;
}
}
if (Tag == -1){
max = 32;
Pos = i;
break;
}
else{
if (max < Tag){
max = Tag;
Pos = i;
}
}
}
Memo[Pos] = Access[n];
}
else{
Memo[Index] = Access[n];
Index++;
}
}
void LRU (int n){
int i, j;
if (IsInMemo (n))
cout << "已经装入内存" << endl;
else
if (Index == Block){
int max = n, Pos = -1, Tag;
for (i = 0; i < Block; i++){
for (j = n - 1; j >= 0; j--){
if (Access[j] == Memo[i]){
Tag = j;
break;
}
}
if (Tag < max){
max = Tag;
Pos = i;
if (max == 0)
break;
}
}
Memo[Pos] = Access[n];
Lost++;
}
else{
Memo[Index] = Access[n];
Index++;
}
}
void MainLRU(){
int i;
InitMemo();
cout << "*******************************************************************" << endl;
cout << "最近最久未使用算法" << endl;
Lost = 0;
for (i = 0; i < 32; i++){
LRU (i);
cout << Memo[0] << " " << Memo[1] << " " << Memo[2] << endl;
}
cout << "最近最久未使用缺页率: " << Lost / 32.0 << " " << Lost << endl;
Lost = 0;
Index = 0;
free (Memo);
}
bool IsInNodes (int n){
for (int i = 0; i < Block; i++){
if (Nodes[i].data == Access[n])
return true;
}
return false;
}
void UpdatedClock (int n){
if (IsInNodes (n))
cout << "已经装入内存" << endl;
else
if (Index == Block){
Lost++;
int i = 0, Tag = -1;
while (true){
if ( (i / Block) % 2 == 0){
if (Nodes[i % Block].flag == 0 && Nodes[i % Block].modify == 0){
Tag = i % Block;
break;
}
}
if ( (i / Block) % 2 == 1){
if (Nodes[i % Block].flag == 0 && Nodes[i % Block].modify == 1){
Tag = i % Block;
break;
}
else
Nodes[i % Block].flag = 0;
}
i++;
}
Nodes[Tag].data = Access[n];
Nodes[Tag].flag = 1;
if (rand() % 10 < 4)
Nodes[Tag].modify = 1;
else
Nodes[Tag].modify = 0;
}
else{
Nodes[Index].data = Access[n];
Nodes[Index].flag = 1;
if (rand() % 10 < 4)
Nodes[Index].modify = 1;
else
Nodes[Index].modify = 0;
Index++;
}
}
void MainClock(){
int i = 0, j = 0;
cout << "*******************************************************************" << endl;
cout << "改进型Clock置换算法" << endl;
Nodes = (LNode*) malloc (Block * sizeof (LNode));
Lost = 0;
for (i = 0; i < Block; i++){
Nodes[i].data = -1;
Nodes[i].flag = -1;
Nodes[i].modify = -1;
}
for (i = 0; i < 32; i++){
UpdatedClock (i);
for (j = 0; j < Block; j++){
cout << Nodes[j].data << " ";
}
cout << endl;
}
cout << "改进型Clock置换算法缺页率: " << Lost / 32.0 << " " << Lost << endl;
Lost = 0;
Index = 0;
}
void MainFIFO(){
Queue q;
pNode p;
InitQueue (&q);
int i = 0;
cout << "*******************************************************************" << endl;
cout << "先进先出置换算法" << endl;
Lost = 0;
for (; i < 32; i++){
FIFO (&q, Access[i]);
p = q.front->next;
while (p){
cout << p->num << " ";
p = p->next;
}
cout << endl;
}
cout << "先进先出算法缺页率:" << Lost/32.0 << " " << Lost << endl;
QDestroy (&q);
}
void InitQueue (pQueue q){
q->rear = (pNode) malloc (sizeof (Node));
if (q->rear == NULL)
cout << "failed" << endl;
else{
q->front = q->rear;
q->rear->next = NULL;
q->front->next = NULL;
q->n = 0;
}
}
void QPush (pQueue q, int num){
pNode p = (pNode) malloc (sizeof (Node));
if (p == NULL)
cout << "failed" << endl;
else{
p->next = NULL;
p->num = num;
if (q->front == q->rear){
q->front->next = p;
q->rear = p;
}
else{
q->rear->next = p;
q->rear = p;
}
q->n++;
}
}
void QPop (pQueue q){
pNode p;
if (q->front != q->rear){
p = q->front->next;
q->front->next = p->next;
if (p == q->rear)
q->front = q->rear;
q->n--;
free (p);
}
}
void QDestroy (pQueue q){
while (q->front != q->rear)
QPop (q);
}
bool FindQueue (pQueue q, int num){
pNode p;
if (q->front != q->rear){
p = q->front->next;
while (p){
if (p->num == num)
return true;
else
p = p->next;
}
}
return false;
}
void FIFO (pQueue q, int num){
if (FindQueue (q, num))
cout << "已装入内存" << endl;
else{
if (q->n == Size){
QPop (q);
QPush (q, num);
Lost++;
}
else
QPush (q, num);
}
}
void MainPBA(){
int i = 0, j = 0;
cout << "*******************************************************************" << endl;
cout << "页面缓冲置换算法(PBA)" << endl;
Idle.num = 0;
Idle.next = NULL;
Modified.num = 0;
Modified.next = NULL;
Pnodes = (PNode*) malloc (Size * sizeof (PNode));
Memo[0] = -1;Memo[1] = -1;Memo[2] = -1;
Lost = 0;
for (i = 0; i < Size; i++){
Pnodes[i].data = -1;
Pnodes[i].flag = 0;
Pnodes[i].modify = 0;
Pnodes[i].next = NULL;
}
for (i = 0; i < 32; i++){
PBA (i);
for (j = 0; j < Size; j++){
cout << Pnodes[j].data << " ";
}
cout << endl;
}
cout << "页面缓冲置换算法(PBA)缺页率:" << Lost / 32.0 << " " << Lost << endl;
}
bool IsInPNodes (int n){
int i;
for (i = 0; i < 3; i++){
if (Pnodes[i].data == Access[n])
return true;
}
return false;
}
PNode* isinLinks (int n){
PNode*p, *q;
p = Idle.next;
q = NULL;
while (p){
if (p->data == Access[n]){
if (q != NULL){
q->next = p->next;
p->next = NULL;
Idle.num--;
break;
}
else
Idle.next = NULL;
}
q = p;
p = p->next;
}
if (p == NULL){
p = Modified.next;
while (p != NULL){
if (p->data == Access[n]){
if (p == Modified.next)
Modified.next = p->next;
else{
q->next = p->next;
p->next = NULL;
Modified.num--;
}
if (Modified.num == 0)
Modified.next = NULL;
break;
}
q = p;
p = p->next;
}
}
return p;
}
void PBA (int n){
if (IsInPNodes (n))
cout << "已装入内存" << endl;
else
if (Index == Size){
PNode *p;
if ( (p = isinLinks (n)) != NULL){
Pnodes = (PNode*) realloc (Pnodes, (Size + 1) * sizeof (PNode));
Pnodes[Size] .data = p->data;
Pnodes[Size].flag = p->flag;
Pnodes[Size].modify = p->modify;
Pnodes[Size].next = p->next;
free (p);
Size++;
Index++;
}
else{
Lost++;
if (Pnodes[n % 3].modify == 1)
AddLink (Pnodes[n % 3].data, 1);
else
AddLink (Pnodes[n % 3].data, 0);
Pnodes[n % 3].data = Access[n];
Pnodes[n % 3].flag = 1;
Pnodes[n % 3].next = NULL;
if (rand() % 10 < 4)
Pnodes[n % 3].modify = 0;
else
Pnodes[n % 3].modify = 1;
}
}
else{
Pnodes[Index].data = Access[n];
Pnodes[Index].flag = 1;
Pnodes[Index].next = NULL;
if (rand() % 10 < 4)
Pnodes[Index].modify = 1;
else
Pnodes[Index].modify = 0;
Index++;
}
}
void AddLink (int data, int type){
PNode* p;
PNode* q;
q = (PNode*) malloc (sizeof (PNode));
q->data = data;
q->flag = 1;
if (type == 1){
q->modify = 1;
p = Modified.next;
}
else{
q->modify = 0;
p = Idle.next;
}
q->next = NULL;
if (p == NULL){
if (type == 0)
Idle.next = q;
else{
Modified.next = q;
}
}
else{
while (p){
if (p->next == NULL){
p->next = q;
break;
}
else
p = p->next;
}
}
if (type == 0){
Idle.num += 1;
if (Idle.num == 10)
FreeIdle();
}
else{
Modified.num += 1;
if (Modified.num == 10)
FreeMod();
}
}
void FreeIdle (){
PNode* p;
p = Idle.next;
while (p){
Idle.next = p->next;
free (p);
p = Idle.next;
}
Idle.num = 0;
}
void FreeMod(){
PNode* p;
p = Modified.next;
while (p){
Modified.next = p->next;
free (p);
p = Modified.next;
}
Modified.num = 0;
}
https://www.cnblogs.com/wasi-991017/p/13072328.html
操作系统实验四:页面置换算法 实验报告_页面置换实验报告_Invictus46的博客-CSDN博客
关于Linux下“cp”命令的实验_weixin_34115824的博客-CSDN博客
实验 6 仿写 Linux 下的 ls-l 命令:
Linux实验之仿ls命令_白晓伊的博客-CSDN博客Linux系统编程之——实现ls-l命令_捂上眼睛看世界的博客-CSDN博客
操作系统实验一:进程管理(含成功运行C语言源代码) - 知乎
计算机操作系统实验二之进程的控制_实验进程和实验过程的区别_笑扯了嘴角的博客-CSDN博客
实验 8 线程控制和同步 :
【操作系统实验】Linux环境下用进程实现哲学家进餐问题——C语言完整代码+详细实验报告_linux哲学家算法需求描述_小天才才的博客-CSDN博客
实验 9 生产者-消费者问题(附加) :
【操作系统实验】Linux环境下用进程实现生产者消费者问题——C语言完整代码+详细实验报告_生产者消费者问题流程图_小天才才的博客-CSDN博客
OS: 生产者消费者问题(多进程+共享内存+信号量)_yaozhiyi的博客-CSDN博客