私は単純なバンカー アルゴリズム シミュレーターを作成しています。必要性と利用可能なリソースを比較すると、1 ループで問題なく動作します。ただし、リンクされたリストを再度繰り返すことはできません。(銀行家のアルゴリズムでは、リンクされたリストの最後のものしか実行できない場合があります。その場合、リンクされたリストをもう一度調べて、もう実行できるかどうかを確認する必要があります [これは機能していない部分です])ポインターと関係がありますが、何がわかりません。
struct LL //linked list structure(pcb)
{
LL_pid pid;
int alloc[15];
int max[15];
int need[15];
int finish;//flag to show if finished
PCB *next; //points to next pcb in linked list
};
ここで私は困惑しています。私はいくつかのテスト printfs を追加し、ループを繰り返さないことに気付きました (おそらく pcb_head のポインターが null になったため)?
void makeBANK(PCB *pcb_head, int avail[15]){
PCB *temp=pcb_head;
int availnew[15];
int x=0;
for(x=0;x<processCount;x++){//get all the available resources
availnew[x]=avail[x];
}
int alldone=0;//check if all processes could run
int possible=0;//check if its possible if a process can run with current available resources
int y=0;
int i=0;
int z=0;
for(y=0;y<processCount;y++){//trying to iterate the linked list at least the amount of processes there are(worst case)
temp=pcb_head;
while((temp!=NULL)&&(temp->finish!=1)){//search all nodes
for(i=0;i<resourceCount;i++){//compare avail and need
if(availnew[i]>=temp->need[i]){//if possible keep 1 ,loop all
possible=1;
}
else{
possible=0;
printf("oops");
break;
}//if not possible break
}
if(possible==1){//if the possible still 1 then print
printf("%d running",temp->pid);
temp->finish=1;
alldone++;
for(z=0;z<resourceCount;z++){//add the allocated to the available
availnew[z]=availnew[z]+temp->alloc[z];
printf("avil: %d",availnew[z]);
}
}
temp=temp->next;//and go to next node(also needed for else)
}
}
if(alldone!=processCount)
printf("not safe");
else
printf("safe");
}
Googleで見つけることができなかったおそらく単純な解決策に反対票を投じられたとしても、私はすべてのヒント(組織..など)を受け入れます。