OS-银行家算法

银行家算法

1、need是申请资源部分;

2、max是需要资源;

3、allocation 是分配资源;

4、rest结构提是指剩余部分的资源;

5、分配算法就是need小于rest就表示该资源可以成功;

6、这里还要将没有加入部分加入一个counter里面,保存–在其他释放资源后有可能会执行,有可能会等待,flag标记2时候一定不可能在存在的;

7、flag数组里面0–表示为不成立,1–表示成立

typedef int data;
typedef struct node{
    data A;
    data B;
    data C;
}node;
typedef struct Process{
    node Max;
    node Allocation;
    node Need;
    unsigned short flag;
}process;
typedef struct rest{
    node Avaliable;
}rest;
void InitialArr(process * pro,const int len,int *flag)
{
    fprintf(stdout, "in order as you input:max(d,d,d)");
    for(int i=0;i=b.A&&a.B>=b.B&&a.C>=b.C)
    {
        flag=1;
        return flag;
    }
    return flag;
}
void Set_process(process * pro,const int len,int * flag,process * cun,process * Queue,rest  *res,int * local)
{
    int j=0,k=0;
    for(int i=0;iAvaliable, pro[i].Need)==1&&flag[i]==0)
        {
            res->Avaliable.A+=pro[i].Need.A;
            res->Avaliable.B+=pro[i].Need.B;
            res->Avaliable.C+=pro[i].Need.C;
            cun[j]=pro[i];
            flag[i]=1;
            local[j]=i;
            j++;
        }
        else
        {
            pro[i].flag++;
            Queue[k++]=pro[i];
        }
    }
}
void Queue_list(process * Queue,const int len,rest *res,int * local,int * flag)
{
    process * temp=malloc(len*sizeof(process));
    int * s=malloc(len*sizeof(int));
    int k=0;
    for(int i=0;iAvaliable,Queue[i].Need)==1&&flag[local[i]]==0)
        {
            res->Avaliable.A+=Queue[i].Need.A;
            res->Avaliable.B+=Queue[i].Need.B;
            res->Avaliable.C+=Queue[i].Need.C;
            flag[local[i]]=1;
        }
        else
        {
            Queue[i].flag++;
            temp[k]=Queue[i];
            s[k]=i;
            k++;
        }
    }
    for(int i=0;i<=k;i++)
    {
        if(Check_ask(res->Avaliable,temp[i].Need)==1&&local[s[i]]==0)
        {
            res->Avaliable.A+=temp[i].Need.A;
            res->Avaliable.B+=temp[i].Need.B;
            res->Avaliable.C+=temp[i].Need.C;
        }
        else
        {
            continue;
        }
    }
    free(temp);
    free(s);
}

点赞