银行家算法实现

#include <iostream>
#include<string.h>
using namespace::std;
struct pcb1{
    int pid;
    int a_max;
    int b_max;
    int c_max;
    int a_allocation;
    int b_allocation;
    int c_allocation;
    int a_need;
    int b_need;
    int c_need;
};

struct pcb2{
    int pid;
    int a_work;
    int b_work;
    int c_work;
    int a_allocation;
    int b_allocation;
    int c_allocation;
    int a_need;
    int b_need;
    int c_need;
};


int showdetail(pcb1 *p,int pidnum){
    cout<<"pid   max   allocation   need"<<endl;
    for (int i=0; i<pidnum; i++) {
        cout<<p[i].pid<<"     "<<p[i].a_max<<" "<<p[i].b_max<<" "<<p[i].c_max<<"     "<<p[i].a_allocation<<" "<<p[i].b_allocation<<" "<<p[i].c_allocation<<"     "<<p[i].a_need<<" "<<p[i].b_need<<" "<<p[i].c_need<<endl;
    }
    return 0;
}

int run(pcb1 *p,int pidnum,int a_available,int b_available,int c_available){
    pcb2 c[100];
    int count=0;
    int flag=1;
    int n=pidnum;
    for (int j=0; j<n; j++) {
        for (int i=0; i<pidnum; i++) {
            if (p[i].a_need<=a_available&&p[i].b_need<=b_available&&p[i].c_need<=c_available) {
                //  将满足条件的进程记录
                c[count].pid=p[i].pid;
                c[count].a_work=a_available;
                c[count].b_work=b_available;
                c[count].c_work=c_available;
                a_available=a_available+p[i].a_allocation;
                b_available=b_available+p[i].b_allocation;
                c_available=c_available+p[i].c_allocation;
                c[count].a_need=p[i].a_need;
                c[count].b_need=p[i].b_need;
                c[count].c_need=p[i].c_need;
                c[count].a_allocation=p[i].a_allocation;
                c[count].b_allocation=p[i].b_allocation;
                c[count].c_allocation=p[i].c_allocation;
                //  删除已完成的进程
                if (i!=(pidnum-1)) {
                    for (int k=i; k<(pidnum-1); k++) {
                        p[k].pid=p[k+1].pid;
                        p[k].a_max=p[k+1].a_max;
                        p[k].b_max=p[k+1].b_max;
                        p[k].c_max=p[k+1].c_max;
                        p[k].a_allocation=p[k+1].a_allocation;
                        p[k].b_allocation=p[k+1].b_allocation;
                        p[k].c_allocation=p[k+1].c_allocation;
                        p[k].a_need=p[k+1].a_need;
                        p[k].b_need=p[k+1].b_need;
                        p[k].c_need=p[k+1].c_need;
                    }
                }
                pidnum--;
                count++;
                break;
            }
            else if(i==(pidnum-1)){
                cout<<"unsafe!"<<endl;
                flag=0;
                j=pidnum;
                break;
            }
        }
    }
    if (flag) {
        cout<<"pid   work    allocation   need   work+allocation"<<endl;
        for (int i=0; i<n; i++) {
            cout<<c[i].pid<<"     "<<c[i].a_work<<" "<<c[i].b_work<<" "<<c[i].c_work<<"     "<<c[i].a_allocation<<" "<<c[i].b_allocation<<" "<<c[i].c_allocation<<"     "<<c[i].a_need<<" "<<c[i].b_need<<" "<<c[i].c_need<<"     "<<c[i].a_work+c[i].a_allocation<<" "<<c[i].b_work+c[i].b_allocation<<" "<<c[i].c_work+c[i].c_allocation<<endl;
        }
    }
    return 0;
}

int main(){
    int a_all, b_all, c_all;
    int a_available, b_available, c_available;
    cout<<"please input the all of A&B&C:";
    cin>>a_all;
    cin>>b_all;
    cin>>c_all;
    int flag=1;
    pcb1 a[100];
    pcb1 b[100];
    int pidNum;
    int p_request, a_request, b_request, c_request;
    cout<<"please input pidNum:";
    cin>>pidNum;
    cout<<"please input the max&allocation of A&B&C:"<<endl;
    for(int i=0;i<pidNum;i++)
        {
            a[i].pid=i;
            cin>>a[i].a_max>>a[i].b_max>>a[i].c_max>>a[i].a_allocation>>a[i].b_allocation>>a[i].c_allocation;
            a[i].a_need=a[i].a_max-a[i].a_allocation;
            a[i].b_need=a[i].b_max-a[i].b_allocation;
            a[i].c_need=a[i].c_max-a[i].c_allocation;
            a_all=a_all-a[i].a_allocation;
            b_all=b_all-a[i].b_allocation;
            c_all=c_all-a[i].c_allocation;
            //  备份a
            b[i].pid=a[i].pid;
            b[i].a_max=a[i].a_max;
            b[i].b_max=a[i].b_max;
            b[i].c_max=a[i].c_max;
            b[i].a_allocation=a[i].a_allocation;
            b[i].b_allocation=a[i].b_allocation;
            b[i].c_allocation=a[i].c_allocation;
            b[i].a_need=a[i].a_need;
            b[i].b_need=a[i].b_need;
            b[i].c_need=a[i].c_need;
        }
    a_available=a_all;
    b_available=b_all;
    c_available=c_all;
    while (flag) {
        string choice;
        cout<<"place input next:";
        cin>>choice;
        if (choice=="showdetail") {
            showdetail(a,pidNum);
        }
        else if(choice=="run") {
            run(a, pidNum,a_available,b_available,c_available);
            for (int i=0;i<pidNum;i++){
                a[i].pid=b[i].pid;
                a[i].a_max=b[i].a_max;
                a[i].b_max=b[i].b_max;
                a[i].c_max=b[i].c_max;
                a[i].a_allocation=b[i].a_allocation;
                a[i].b_allocation=b[i].b_allocation;
                a[i].c_allocation=b[i].c_allocation;
                a[i].a_need=b[i].a_need;
                a[i].b_need=b[i].b_need;
                a[i].c_need=b[i].c_need;
            }
            a_available=a_all;
            b_available=b_all;
            c_available=c_all;
        }
        else if(choice=="request"){
            cout<<"please input pid request of A&B&C:";
            cin>>p_request>>a_request>>b_request>>c_request;
            if (a_request<=a_available&&b_request<=b_available&&c_request<=c_available&&a_request<=a[p_request].a_need&&b_request<=a[p_request].b_need&&c_request<=a[p_request].c_need) {
                a[p_request].a_need=a[p_request].a_need-a_request;
                a[p_request].b_need=a[p_request].b_need-b_request;
                a[p_request].c_need=a[p_request].c_need-c_request;
                a[p_request].a_allocation=a[p_request].a_allocation+a_request;
                a[p_request].b_allocation=a[p_request].b_allocation+b_request;
                a[p_request].c_allocation=a[p_request].c_allocation+c_request;
                a_available=a_available-a_request;
                b_available=b_available-b_request;
                c_available=c_available-c_request;
                run(a, pidNum,a_available,b_available,c_available);
                for (int i=0;i<pidNum;i++){
                    a[i].pid=b[i].pid;
                    a[i].a_max=b[i].a_max;
                    a[i].b_max=b[i].b_max;
                    a[i].c_max=b[i].c_max;
                    a[i].a_allocation=b[i].a_allocation;
                    a[i].b_allocation=b[i].b_allocation;
                    a[i].c_allocation=b[i].c_allocation;
                    a[i].a_need=b[i].a_need;
                    a[i].b_need=b[i].b_need;
                    a[i].c_need=b[i].c_need;
                }
                a_available=a_all;
                b_available=b_all;
                c_available=c_all;
            }
            else{
                cout<<"rquest>available or need error"<<endl;
            }
        }
        else if(choice=="exit"){
            flag=0;
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值