最近刚考完一门,也算是有点闲暇时间,写个博客,就当做操作系统实验的笔记吧。
客官先别急着白嫖点个赞再看吧求求了
文章目录
一、在cmd命令行下编译运行C++源代码
1.进入目标目录
在这里我的demo1的源文件的存储路径是:E:\QTPractice
\OS\demo1
1.win+r回车
2.进入指定目录,先 “盘名:” 进入指定盘
3.cd+路径
2.编译
利用"g++ 文件名.cpp"编译 如果提示需要c++11支持则使用"g++ -std=c++11 -o main 文件名.cpp"
3.运行
文件名.exe 即可
4.代码
#include <iostream>
using namespace std;
int main()
{
cout<<"浑元形意太极拳掌门人大弟子臻哥精神语录:"<<endl;
cout <<"干饭人干饭魂干饭都是人上人!"<< endl;
cout<<"打了一辈子仗了,就不能享受享受"<<endl;
cout<<"社会道路一条条,涩会哥啊,就要学会唱涩会摇"<<endl;
cout<<"全体精神小伙统统听令"<<endl;;
cout<<"瑜哥:zen切"<<endl;
cout<<"-----------------"<<endl;
cout<<"结语:臻哥牛逼思密达!";
return 0;
}
二、进程调度程序设计
1.流程图
2.代码
代码如下:
#include <iostream>
#include <queue>
#include <algorithm>
#include <random>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;
struct PCB{
int id;//标识符
char statement;//运行R(run)、就绪W(wait)和完成F(finish)
int priority;//优先级
int cpuTime;//占用CPU时间
int totalTime;//进程需要的总时间
friend bool operator <(PCB a,PCB b){
return a.priority<b.priority;
}
};
priority_queue<PCB> wait1;
queue<PCB> wait2;
PCB run;//保存运行的进程的进程控制块
PCB pcb[5];
int countTimes=0;//记录运行次数
void init1(){//优先数法初始化
for(int i=1;i<=5;i++){
PCB temp;
temp.id=i;
temp.statement='W';
temp.priority=rand()%30;
temp.totalTime=rand()%5+1;
temp.cpuTime=0;
wait1.push(temp);
pcb[i-1]=temp;
}
run=wait1.top();
run.statement='R';
pcb[run.id-1].statement='R';
wait1.pop();//链首进程作为第一次运行的进程
}
void init2(){//简单轮转法初始化
for(int i=1;i<=5;i++){
PCB temp;
temp.id=i;
temp.statement='W';
temp.totalTime=rand()%5+1;
temp.priority=temp.totalTime;
temp.cpuTime=0;
wait2.push(temp);
pcb[i-1]=temp;
}
run=wait2.front();
run.statement='R';
pcb[run.id-1].statement='R';
wait2.pop();//链首进程作为第一次运行的进程
}
void print1(){
cout<<"第#"<<countTimes++<<"次运行"<<endl;
cout<<"进程标识符 优先数 占用CPU时间片数 进程所需时间片数 进程状态"<<endl;
for(int i=1;i<=5;i++){
cout<<left<<" "<<pcb[i-1].id<<" "<<setw(2)<<pcb[i-1].priority<<" "<<setw(2)<<pcb[i-1].cpuTime;
cout<<" "<<setw(2)<<pcb[i-1].totalTime<<" "<<pcb[i-1].statement<<endl;
}
cout<<"---------------------------------------------------"<<endl;
}
void print2(){
cout<<"第#"<<countTimes++<<"次运行"<<endl;
cout<<"进程标识符 轮转时间片数 占用CPU时间片数 进程所需时间片数 进程状态"<<endl;
for(int i=1;i<=5;i++){
cout<<left<<" "<<pcb[i-1].id<<" "<<setw(2)<<pcb[i-1].priority<<" "<<setw(2)<<pcb[i-1].cpuTime;
cout<<" "<<setw(2)<<pcb[i-1].totalTime<<" "<<pcb[i-1].statement<<endl;
}
cout<<"---------------------------------------------------"<<endl;
}
//优先数法执行一次
void execute1(){//执行一次,即分配一个单位的时间片
run.cpuTime++;
run.priority-=3;
pcb[run.id-1].cpuTime++;
pcb[run.id-1].priority-=3;
if(run.cpuTime==run.totalTime){//进程得到了所需的所有时间片数
run.statement='F';//标记进程已经完成任务
pcb[run.id-1].statement='F';
run=wait2.front();
wait2.pop();//链首进程作为将要运行的进程
print1();
}
else{
if(run.priority<wait1.top().priority){
run.statement='W';
pcb[run.id-1].statement='W';
wait2.push(run);
run=wait1.top();
run.statement='R';
pcb[run.id-1].statement='R';
wait1.pop();
print1();
}
}
}
//简单轮转法执行一次
void execute2(){//执行一次,即分配一个单位的时间片
run.cpuTime++;
pcb[run.id-1].cpuTime++;
run.priority--;
pcb[run.id-1].priority--;
if(run.priority==0){//进程得到了所需的所有时间片数
run.statement='F';//标记进程已经完成任务
pcb[run.id-1].statement='F';
run=wait2.front();
wait2.pop();//链首进程作为下一次运行的进程
print2();
}
else{
run.statement='W';
pcb[run.id-1].statement='W';
wait2.push(run);
run=wait2.front();
run.statement='R';
pcb[run.id-1].statement='R';
wait2.pop();
print2();
}
}
int main()
{
int choice;
cout<<"请输入您要选择的算法 输入1:优先数法 输入2:简单轮转法"<<endl;
cin>>choice;
if(choice==1){
init1();
print1();
while(!(run.cpuTime==run.totalTime&&wait1.empty()))
execute1();
}
else if(choice==2){
init2();
print2();
while(!(run.cpuTime==run.totalTime&&wait2.empty()))
execute2();
}
return 0;
}
三、存储管理程序设计
1.流程图
2.代码
代码如下:
#include <iostream>
#include <map>
#include <string>
#include <queue>
using namespace std;
const int m=4;//系统为该作业分配的主存块数
const int L=6;//作业页表中最大页号
//页表项
struct itemPage{
int numPage;//页号
int flagExist;//标志该块是否在内存中 0-不存在 1-存在
int numBlock;//主存块号 -1表示不在内存块中
int flagModidfy;//是否被修改过 0-未修改 1-修改过
string location;//在磁盘上的位置
};
//指令项
struct itemOp{
string op;//操作
int numPage;//页号
string inerLocation;//业内地址
};
queue<int> q;//
itemPage pageList[7];//页表
void execute(itemOp temp){
if(temp.op=="store"){
pageList[temp.numPage].flagModidfy=1;
}
cout<<"绝对地址为:"<<pageList[temp.numPage].numBlock<<temp.inerLocation<<endl;
}
int main()
{
cout<<"请输入页表:"<<endl;
for(int i=0;i<=L;i++){
cin>>pageList[i].numPage>>pageList[i].flagExist;
pageList[i].numBlock=-1;//默认没有在内存块中
if(pageList[i].flagExist)
cin>>pageList[i].numBlock;
cin>>pageList[i].flagModidfy>>pageList[i].location;
if(q.size()<4){
q.push(i);
}
}
while(true){
cout<<"请输入指令:"<<endl;
itemOp temp;
cin>>temp.op;
if(temp.op=="end"){
cout<<"请求页式存储管理系统运行结束!"<<endl;
break;
}
cin>>temp.numPage>>temp.inerLocation;
if(pageList[temp.numPage].flagExist==1){//该页存在于内存块中
}
else{//该页不在内存块中
cout<<"缺失#"<<temp.numPage<<"号页面"<<endl;
int back=q.front();//将要返回磁盘的页号
pageList[back].flagExist=0;//来的最早的页标志为不在内存块中
pageList[temp.numPage].flagExist=1;//标志缺失的那一页已经存在于内存块中
pageList[temp.numPage].numBlock=pageList[back].numBlock;//将原来的主存块让给新调进来的页
pageList[back].numPage=-1;
q.pop();
q.push(temp.numPage);
if(pageList[back].flagModidfy==1){//如果该页修改过
pageList[back].flagModidfy=0;//返回磁盘后修改为置为0
cout<<"OUT: "<<back<<" ";
}
cout<<"IN: "<<temp.numPage<<endl;
}
execute(temp);
}
return 0;
}
3.样例
1.页表输入
0 1 5 0 011
1 1 8 0 012
2 1 9 0 013
3 1 1 0 021
4 0 0 022
5 0 0 023
6 0 0 121
2.指令输入
+ 0 070
+ 1 050
* 2 015
store 3 021
get 0 056
- 6 040
move 4 053
+ 5 023
store 1 037
get 2 078
+ 4 001
store 6 084