银行家算法问题

—4月28日更新,减少内存需要——–

现代操作系统中,老师给我们布置了银行家算法问题。
源码:


//一维数组的比较,返回bool
bool victorCompare(int*& oneNeedted, int* available, int typeCount)
{

    for(int i=0;i<typeCount;i++)
    {
        if(oneNeedted[i]>available[i])
        {
            return false;
        }
    }
    return true;
}

//验证安全时的回收工作
void release(int*& oneAllocated, int*& oneNeeded, int* available, int typeCount)
{
    for(int i=0;i<typeCount;i++)
    {
        available[i] += oneAllocated[i];
    }
}

//实际进程中的回收工作
void realrelease(int*& oneAllocated, int*& oneNeeded, int* available, int typeCount)
{
    for(int i=0;i<typeCount;i++)
    {
        available[i] += oneAllocated[i];
        oneAllocated[i] = oneNeeded[i] = 0; //对所要请求的资源数置0
    }
}

//检查提出请求是否安全,allocated 已经分配资源的矩阵,available 表示资源的当前可用数,needed 还需要多少资源
//processCount 进程数, typeCount 申请的种类数, 
//二维矩阵allocated, needed = processCount * typeCount;
//一维矩阵available = 1×typeCount;
bool isSafe(int**& allocated, int**& needed, int*& available, int processCount, int typeCount)
{
    bool* isSafe = new bool[processCount];  //记录判断每次比较时各个进程是否安全
    for(int i=0;i<processCount;i++)
    {
        isSafe[i] = false;
    }

    int index = 0;
    int i = 0;//记录连续判断为不安全的次数
    int safe = 0;//记录已经安全的进程,safe+i==5,则返回false
    while(i+safe < 5)
    {
        isSafe[index] = victorCompare(needed[index], available, typeCount);//判断当前进程是否安全
        if(!isSafe[index])
            i++;
        else  //安全,回收资源进入下一轮判断
        {
            release(allocated[index], needed[index], available, typeCount);
            i = 0;
            safe++;
        }
        if(safe==5)
        {
            delete[] isSafe;
            return true;
        }
        //寻找下一个进程判断
        for(int j=1;j<processCount;j++)
        {
            index++;
            index %= processCount;
            if(!isSafe[index])
                break;
        }
    }
    delete[] isSafe;
    return false;
}

//银行家算法
void banker()
{
    int processCount;
    int typeCount;
    cout<<"请按照提示输入数据"<<endl;
    cout<<"请输入进程数x 与资源种类y :"<<endl;
    cout<<"x  y : ";cin>>processCount>>typeCount;

    int** allocated = new int*[processCount];
    int** needed = new int*[processCount];
    int* available = new int[typeCount];
    int* availableCompute = new int[typeCount];     //对资源可用数进行复制的矩阵
    for(int i=0;i<processCount;i++)
    {
        allocated[i] = new int[typeCount];
        needed[i] = new int[typeCount];
    }

    cout<<"请输入"<<typeCount<<" 种进程的可用数: ";
    for(int i=0;i<typeCount;i++)
    {
        cin>>available[i];
        availableCompute[i] = available[i];
    }

    for(int i=0;i<processCount;i++)
    {
        cout<<"请输入第"<<i+1<<" 个进程所请求的"<<typeCount<<" 个资源数: ";
        for(int j=0;j<typeCount;j++)
        {
            cin>>needed[i][j];
            allocated[i][j] = 0;
        }
    }

    int x;
    int y;
    int z;
    while(true)
    {
        cout<<"请输入第x 个进程请求第y 种资源的数量z:";
        cin>>x>>y>>z;

        if(z>available[y-1]||z>needed[x-1][y-1])
        {
            cout<<"不安全,银行家不分配资源"<<endl;
            continue;
        }

        allocated[x-1][y-1] += z;
        needed[x-1][y-1] -= z;
        availableCompute[y-1] -= z;
        if(isSafe(allocated, needed, availableCompute, processCount, typeCount))
        {
            cout<<"请求成功!"<<endl;
            available[y-1] -= z;
        }
        else
        {
            allocated[x-1][y-1] -=z;
            needed[x-1][y-1] += z;
            cout<<"不安全,银行家不分配资源"<<endl;
        }

        bool isend = true;
        cout<<"请求资源数的矩阵: "<<endl;
        for(int i=0;i<processCount;i++)
        {
            bool iscomplete = true;  //判断是否该进程全部获得所需资源数
            for(int j=0;j<typeCount;j++)
            {
                cout<<needed[i][j]<<" ";
                if(isend&&needed[i][j]!=0)
                {
                    isend=false;
                }
                if(iscomplete&&needed[i][j]!=0)
                {
                    iscomplete = false;
                }
            }
            if(iscomplete)//如果该进程全部获得所需资源数,则开始release
            {
                realrelease(allocated[i], needed[i], available, typeCount);
            }
            cout<<endl;
        }

        cout<<"资源可用数的矩阵:"<<endl;
        for(int i=0;i<typeCount;i++)
            cout<<available[i]<<"  ";
        cout<<endl;

        if(isend)
            break;
        for(int i=0;i<typeCount;i++)
            availableCompute[i] = available[i];
    }

    cout<<"所有进程已经运行结束!"<<endl;


    for(int i=0;i<processCount;i++)
    {
        delete[] allocated[i];
        delete[] needed[i];
    }
    delete[] available;
    delete[] availableCompute;
    delete[] allocated;
    delete[] needed;
}

运行结果

欢迎拍砖。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值