| #include <iostream> |
| #include <vector> |
| #include <string> |
| |
| |
| using namespace std; |
| |
| typedef struct{ |
| int A; |
| int B; |
| int C; |
| }MAX,ALLOCATION,NEED,AVAILABLE,WORK; |
| |
| class PCB{ |
| public: |
| MAX maxs; |
| ALLOCATION allocation; |
| NEED need; |
| WORK work; |
| string pname; |
| bool finish; |
| PCB(){ |
| work = {0,0,0}; |
| } |
| |
| bool runState(AVAILABLE a){ |
| return need.A <= a.A && need.B <= a.B && need.C <= a.C; |
| } |
| void show(){ |
| printf("线程名:%s \t MAX %d %d %d \t ALLOCATION %d %d %d \t NEED %d %d %d \t WORK %d %d %d FINISH: %s \n", |
| pname.c_str(),maxs.A, maxs.B,maxs.C,allocation.A,allocation.B,allocation.C,need.A, need.B,need.C |
| ,work.A, work.B,work.C,(finish ? "true":"false") ); |
| } |
| }; |
| |
| bool Request(PCB &pcb,AVAILABLE &available){ |
| |
| if(pcb.runState(available)){ |
| pcb.work = available; |
| available.A+=pcb.allocation.A; |
| available.B+=pcb.allocation.B; |
| available.C+=pcb.allocation.C; |
| pcb.finish=true; |
| return true; |
| }else return false; |
| } |
| void RunP(vector<PCB> &plist,AVAILABLE &available){ |
| int count = 0; |
| int non = 0; |
| int errorIndex = 0; |
| vector<PCB> saveQueue; |
| while(non < plist.size() && count < plist.size() ){ |
| for(int i = 0; i < plist.size(); i ++) |
| { |
| if(plist[i].finish){ |
| continue; |
| } |
| |
| if(Request(plist[i],available)){ |
| saveQueue.push_back(plist[i]); |
| plist[i].show(); |
| PCB t = plist[i]; |
| |
| count++; |
| }else { |
| non++; |
| errorIndex = i; |
| } |
| } |
| } |
| |
| |
| cout << count << endl; |
| if(count == plist.size()){ |
| printf("运行成功 系统此时状态安全 安全序列为("); |
| for(int i = 0; i < saveQueue.size(); i ++) |
| { |
| printf("%s",saveQueue[i].pname.c_str()); |
| if(i != saveQueue.size()-1) printf(","); |
| } |
| printf(")\n"); |
| }else { |
| printf("运行失败,进程: %s 无法执行",plist[errorIndex].pname.c_str()); |
| } |
| |
| } |
| |
| int main(){ |
| |
| |
| int n; |
| cout <<"请输入进程个数:"; |
| cin >> n; |
| |
| AVAILABLE available; |
| cout <<"请输入ABC的资源数(如: 1 3 4):"; |
| cin >> available.A >> available.B >> available.C; |
| printf("AVAILABLE: A=%d B=%d C=%d \n", available.A , available.B, available.C); |
| vector<PCB> plist; |
| for(int i = 0; i < n;i++){ |
| PCB tempP; |
| cout << "请输入线程名\t MAX A B C \t ALLOCATION A B C \t NEED A B C" << endl; |
| printf( "如: P1 1 2 3 1 2 3 1 2 3\n"); |
| cin |
| >>tempP.pname>>tempP.maxs.A>>tempP.maxs.B>>tempP.maxs.C>> |
| tempP.allocation.A>>tempP.allocation.B>>tempP.allocation.C |
| >>tempP.need.A>>tempP.need.B>>tempP.need.C; |
| tempP.finish = false; |
| tempP.show(); |
| plist.push_back(tempP); |
| } |
| RunP(plist,available); |
| return 0; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!