├── os1.cpp ├── os2.cpp ├── os3.cpp ├── os4.cpp └── os5.cpp /os1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | struct PCB{ 8 | string Name; 9 | PCB * pcb; 10 | unsigned int runTime; 11 | unsigned int runPriorit; 12 | bool runStatus; 13 | }; 14 | 15 | class mainPcb{ 16 | private: 17 | PCB pcb1,pcb2,pcb3,pcb4,pcb5,headPcb; 18 | public: 19 | void initialPcb(); 20 | void shufflePcb(); 21 | void runPcb(); 22 | void showPcb(); 23 | }; 24 | 25 | void mainPcb::initialPcb(){ 26 | cout<<"==========welcome to PCB scheduling algorithm==========\n"; 27 | cout<<"Plz insert name for the 5 PCB\n"; 28 | cin>>pcb1.Name>>pcb2.Name>>pcb3.Name>>pcb4.Name>>pcb5.Name; 29 | cout<<"Plz insert run time for the 5 PCB(no small than 0)\n"; 30 | cin>>pcb1.runTime>>pcb2.runTime>>pcb3.runTime>>pcb4.runTime>>pcb5.runTime; 31 | cout<<"Plz insert priority for the 5 PCB(no small than 0 and its run time)\n"; 32 | cin>>pcb1.runPriorit>>pcb2.runPriorit>>pcb3.runPriorit>>pcb4.runPriorit>>pcb5.runPriorit; 33 | cout<<"Setting pcb status...\n"; 34 | if (pcb1.runTime!=0) {pcb1.runStatus=1;} 35 | if (pcb2.runTime!=0) {pcb2.runStatus=1;} 36 | if (pcb3.runTime!=0) {pcb3.runStatus=1;} 37 | if (pcb4.runTime!=0) {pcb4.runStatus=1;} 38 | if (pcb5.runTime!=0) {pcb5.runStatus=1;} 39 | cout<<"Setting pcb sequence...\n"; 40 | pcb1.pcb=&pcb2;pcb2.pcb=&pcb3;pcb3.pcb=&pcb4;pcb4.pcb=&pcb5;pcb5.pcb=&pcb1;headPcb.pcb=&pcb1; 41 | cout<<"Initialise successfully...\n"; 42 | } 43 | 44 | void mainPcb::shufflePcb(){ 45 | int num=5; 46 | while(headPcb.pcb->runTime==0&&num!=0) { 47 | headPcb.pcb=headPcb.pcb->pcb; 48 | num--; 49 | } 50 | PCB exchangePcb=headPcb;//??? 51 | PCB oldheadPcb=headPcb;//??? 52 | headPcb.pcb=headPcb.pcb->pcb; 53 | while (headPcb.pcb!=oldheadPcb.pcb) { 54 | if (headPcb.pcb->runPriorit>exchangePcb.pcb->runPriorit&&headPcb.pcb->runStatus==1) { 55 | exchangePcb.pcb=headPcb.pcb; 56 | } 57 | headPcb.pcb=headPcb.pcb->pcb; 58 | } 59 | headPcb=exchangePcb; 60 | cout<<"Suffle successfully...\n"; 61 | } 62 | 63 | void mainPcb::runPcb(){ 64 | cout<<"Start poccessing...\n"; 65 | shufflePcb(); 66 | while (headPcb.pcb->runStatus) { 67 | headPcb.pcb->runPriorit--; 68 | headPcb.pcb->runTime--; 69 | if (headPcb.pcb->runTime==0) { 70 | headPcb.pcb->runStatus=0; 71 | } 72 | sleep(1); 73 | cout<<"Run\t"<Name<<"\tone time\n"; 74 | showPcb(); 75 | shufflePcb(); 76 | } 77 | cout<<"End poccessing...\n"; 78 | } 79 | void mainPcb::showPcb(){ 80 | cout<<"PCB status as follows(Name|runTime|runPriority|runStatus)\n"; 81 | cout< 2 | #include 3 | using namespace std; 4 | typedef unsigned int NUM;//把无符号数命名为NUM 5 | #define chartItemNum 3//说明表列数 6 | #define MaxNum 1000//定义最大数以供使用 7 | 8 | class romTest{ 9 | private: 10 | NUM romSize;//用户给定的内存大小 11 | NUM chartNum;//用户给定的说明表行数 12 | NUM (*chartPtr)[chartItemNum]=NULL;//动态数组指针 13 | NUM chart[MaxNum][chartItemNum];//定义说明表 14 | vector> runtimeTable;// 15 | public: 16 | romTest(NUM rS,NUM cS);//构造函数 17 | void run();//运行 18 | void insert(NUM spaceNum);//插入内存 19 | void _delete();//删除内存 20 | void display();//展示 21 | void checkForRepetition();//检查是否重叠 22 | }; 23 | 24 | romTest::romTest(NUM rS,NUM cS){ 25 | romSize=rS;chartNum=cS; 26 | for (int i=0; i>choice; 44 | if (choice) { 45 | cout<<"Plz insert the space num\n"; 46 | cin>>Num; 47 | insert(Num); 48 | }else{ 49 | _delete(); 50 | } 51 | } 52 | } 53 | 54 | void romTest::insert(NUM spaceNum){ 55 | //插入说明表项 56 | bool key=false; 57 | for (int i=0; ispaceNum) { 60 | //对于有足够空间的说明项来说,判断插入后是否能占用其他表项 61 | runtimeTable.push_back({chartPtr[i][0],spaceNum}); 62 | chartPtr[i][0]=chartPtr[i][0]+spaceNum; 63 | chartPtr[i][1]=chartPtr[i][1]-spaceNum; 64 | key=true; 65 | break; 66 | }else if(chartPtr[i][1]==spaceNum){ 67 | //对于有足够空间的说明项来说,判断插入后是否能替代其他表项 68 | runtimeTable.push_back({chartPtr[i][0],spaceNum}); 69 | chartPtr[i][0]=chartPtr[i][1]=chartPtr[i][2]=0; 70 | key=true; 71 | break; 72 | } 73 | } 74 | if (key) { 75 | cout<<"Insert successfully!\n"; 76 | display(); 77 | }else{ 78 | cout<<"No enough space\n"; 79 | display(); 80 | } 81 | } 82 | 83 | void romTest::checkForRepetition(){ 84 | //检查合并过后是否还是会有能结合的点 85 | for(int i=0;ifirst<<"\t"<second<<"\n"; 113 | count++; 114 | it++; 115 | } 116 | cout<<"Which one do you wanna delete?\n"; 117 | cin>>count; 118 | it=runtimeTable.begin()+count-1; 119 | //检查删除当前表项后是否会能合并其他表项 120 | bool key=0; 121 | for (int i=0; ifirst+it->second-1+1)) { 123 | chartPtr[i][0]=it->first; 124 | chartPtr[i][1]=chartPtr[i][1]+it->second; 125 | key=1; 126 | break; 127 | }else if(chartPtr[i][1]+1==it->first){ 128 | chartPtr[i][1]=chartPtr[i][1]+it->second; 129 | key=1; 130 | break; 131 | } 132 | } 133 | //表明不会对其他表项造成影响,新建表项 134 | if (!key) { 135 | for (int i=0; ifirst; 138 | chartPtr[i][1]=it->first+it->second; 139 | chartPtr[i][2]=1; 140 | break; 141 | } 142 | } 143 | } 144 | //检查是否重复 145 | checkForRepetition(); 146 | runtimeTable.erase(runtimeTable.begin()+count-1); 147 | display(); 148 | } 149 | } 150 | 151 | void romTest::display(){ 152 | //展示函数 153 | cout<<"====================\n"; 154 | cout<<"start"<<"|"<<"space"<<"|"<<"status"<<"\n"; 155 | for (int i=0; i>rS>>cS; 178 | romTest rom(rS,cS); 179 | rom.run(); 180 | return 0; 181 | } 182 | -------------------------------------------------------------------------------- /os3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | char *decToBin(int num,int bits){ 7 | //实现十进制到二进制的转换 8 | char *arr; 9 | char temp; 10 | int n=0; 11 | int _bits=bits; 12 | arr=(char *)malloc(sizeof(char)*4); 13 | while (num>0) { 14 | arr[n++]=num%2+'0'; 15 | num/=2; 16 | } 17 | while (bits>0) { 18 | if (arr[bits-1]==0) { 19 | arr[bits-1]='0'; 20 | } 21 | bits--; 22 | } 23 | n=_bits; 24 | for (int i=0; i0) { 37 | if (num[_bits-1]=='1') { 38 | number+=pow(2,bits-_bits); 39 | } 40 | _bits--; 41 | } 42 | return number; 43 | } 44 | 45 | 46 | class diskAllocation{ 47 | private: 48 | //用来代表位示图的bool数组 49 | bool diskSignP[8][2][4]; 50 | public: 51 | diskAllocation(){ 52 | //构造函数 53 | for (int i=0; i<8; i++) { 54 | for (int j=0; j<2; j++) { 55 | for (int k=0; k<4; k++) { 56 | diskSignP[i][j][k]=0; 57 | } 58 | } 59 | } 60 | } 61 | bool askForAllocation();//申请 62 | bool askForDelete(string addr);//删除 63 | void askForPlot();//展示函数 64 | }; 65 | 66 | void diskAllocation::askForPlot(){ 67 | //展示硬盘占用情况 68 | cout<<"=====================================================\n"; 69 | for (int i=0; i<8; i++) { 70 | cout<<"No."<=0)&&(SideNum<2)&&(SideNum>=0)&&(RecordNum<4)&&(RecordNum>=0)) { 114 | cout<<"=====================================================\n"; 115 | cout<<"CYL num:"<>choice; 139 | if (choice) { 140 | da.askForAllocation(); 141 | da.askForPlot(); 142 | }else{ 143 | cout<<"Please insert the address\n"; 144 | cin>>addressInsert; 145 | da.askForDelete(addressInsert); 146 | da.askForPlot(); 147 | } 148 | } 149 | return 0; 150 | } 151 | -------------------------------------------------------------------------------- /os4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define sourceNum 3//源数量 3 | #define processNum 4//处理进程数量 4 | typedef unsigned int NUM;//把无符号整形变量设置为NUM 5 | using namespace std; 6 | 7 | class bankAlgorithm{ 8 | //封装后的类 9 | private: 10 | NUM ava[sourceNum]={1,1,2};//availble数组 11 | NUM work[sourceNum]={0};//work数组 12 | NUM max[processNum][sourceNum]={{3,2,2},{6,1,3},{3,1,4},{4,2,2}};//max数组 13 | NUM allocation[processNum][sourceNum]={{1,0,0},{5,1,1},{2,1,1},{0,0,2}};//allocation数组 14 | NUM need[processNum][sourceNum]={{2,2,2},{1,0,2},{1,0,3},{4,2,0}};//need数组 15 | NUM request[4]={0};//request数组 16 | bool finish[processNum]={0};//标志是否完成数组 17 | public: 18 | bool sendRequest();//发送request请求 19 | bool askForSafetyCheck();//请求安全性检查 20 | void display();//展示函数 21 | }; 22 | 23 | bool bankAlgorithm::sendRequest(){ 24 | cout<<"Please insert the request vector, 1 for process,2-4 for request num\n"; 25 | //输入数据 26 | cin>>request[0]>>request[1]>>request[2]>>request[3]; 27 | cout<<"Now checking the request vector...\n"; 28 | //检查need数组 29 | if (need[request[0]][0]>=request[1]&&need[request[0]][1]>=request[2]&&need[request[0]][2]>=request[3]) { 30 | //检查availble数组 31 | if (ava[0]>=request[1]&&ava[1]>=request[2]&&ava[2]>=request[3]) { 32 | cout<<"Now checking the safety...\n"; 33 | NUM oldAvail_0 = ava[0]; 34 | NUM oldAvail_1 = ava[1]; 35 | NUM oldAvail_2 = ava[2]; 36 | //检查allocation数组 37 | NUM oldAllocation_0=allocation[request[0]][0]; 38 | NUM oldAllocation_1=allocation[request[0]][1]; 39 | NUM oldAllocation_2=allocation[request[0]][2]; 40 | //以下为改变老数组的值 41 | NUM oldNeed_0=need[request[0]][0]; 42 | NUM oldNeed_1=need[request[0]][1]; 43 | NUM oldNeed_2=need[request[0]][2]; 44 | ava[0]=ava[0]-request[1]; 45 | ava[1]=ava[1]-request[2]; 46 | ava[2]=ava[2]-request[3]; 47 | allocation[request[0]][0]=allocation[request[0]][0]+request[1]; 48 | allocation[request[0]][1]=allocation[request[0]][1]+request[2]; 49 | allocation[request[0]][2]=allocation[request[0]][2]+request[3]; 50 | need[request[0]][0]=need[request[0]][0]-request[1]; 51 | need[request[0]][1]=need[request[0]][1]-request[2]; 52 | need[request[0]][2]=need[request[0]][2]-request[3]; 53 | //要求安全性检查 54 | if(!askForSafetyCheck()){ 55 | ava[0]=oldAvail_0; 56 | ava[1]=oldAvail_1; 57 | ava[2]=oldAvail_2; 58 | allocation[request[0]][0]=oldAllocation_0; 59 | allocation[request[0]][1]=oldAllocation_1; 60 | allocation[request[0]][2]=oldAllocation_2; 61 | need[request[0]][0]=oldNeed_0; 62 | need[request[0]][1]=oldNeed_1; 63 | need[request[0]][2]=oldNeed_2; 64 | cout<<"False! Failing the safety test\n"; 65 | return false; 66 | }else{ 67 | display(); 68 | } 69 | }else{ 70 | cout<<"False! The request num is bigger than availble num\n"; 71 | return false; 72 | } 73 | }else{ 74 | cout<<"False! The request num is bigger than need num\n"; 75 | return false; 76 | } 77 | return true; 78 | } 79 | 80 | bool bankAlgorithm::askForSafetyCheck(){ 81 | //安全性检查函数 82 | for(int i=0;i=0){ 91 | if (finish[k]==false) { 92 | for (int i=0; iwork[i]) { 94 | key=false; 95 | } 96 | } 97 | if (key) { 98 | for (int i=0; i 10 | #include 11 | typedef unsigned int NUM; 12 | #define pageChartNum 5 13 | using namespace std; 14 | 15 | class pageChartTran{ 16 | //页表结构 17 | private: 18 | NUM pageChart[pageChartNum]={2,3,8,5,1};//对应的物理块号 19 | NUM pageMoveMaxLength = 1023;//页内最大偏移 20 | NUM pageMaxNum = 5;//最大的页号 21 | NUM moveAddress;//偏移量 22 | NUM pageNum;//页号 23 | public: 24 | void pageTran();//转换函数 25 | }; 26 | 27 | char *decToBin(int num,int bits){ 28 | //实现十进制到二进制字符串的转换 29 | char *arr; 30 | char temp; 31 | int n=0; 32 | int _bits=bits; 33 | arr=(char *)malloc(sizeof(char)*(bits+1)); 34 | while (num>0) { 35 | arr[n++]=num%2+'0'; 36 | num/=2; 37 | } 38 | while (bits>0) { 39 | if (arr[bits-1]==0) { 40 | arr[bits-1]='0'; 41 | } 42 | bits--; 43 | } 44 | n=_bits; 45 | for (int i=0; i>pageNum>>moveAddress; 60 | if (!(pageNum<=pageMaxNum&&moveAddress<=pageMoveMaxLength)) { 61 | cout<<"Invalid input, plz insert again\n"; 62 | }else{ 63 | cout<<"Correct input\n"; 64 | break; 65 | } 66 | } 67 | cout<<"=========================================\n"; 68 | cout<<"Your page's logical address is "<<"|"<