操作系统银行家算法

#include <stdio.h>
#include <stdlib.h>

/*********************************************************************
* 文件名称: // bank_alloc.cpp
* 文件标识: //
* 内容摘要: //操作系统银行家算法
* 其它说明: //
* 当前版本: // v1.0
* 作 者: //pipi
* 完成日期: // 2011年12月6日
*********************************************************************
*/
typedef struct banker
{
int alloc;
int need;
int state; // 0 分配 1释放
struct banker *next;
}banker_ST,*lpbanker_ST;


void CreateLoopLink(lpbanker_ST head,int alloc_arry[],int need_arry[])
{

lpbanker_ST p = head;

for(int i=0;i<3;i++)
{
lpbanker_ST new_node = (lpbanker_ST)malloc(sizeof(banker_ST));
new_node->alloc = alloc_arry[i];
new_node->need = need_arry[i];
new_node->state = 0;
p->next = new_node;
p = new_node;
}
p->next = head->next;
}


void request_now (lpbanker_ST head,int requste)
{
lpbanker_ST travel = head;
int i = 0;

travel = travel->next;

while((travel->state==0))
{
printf("第%d次:",++i);
if((requste>=travel->need) && (0==travel->state))
{

printf(" alloc_%d nedd_%d",travel->alloc,travel->need);
requste += (travel->alloc+travel->need);
printf(" requste =%d\n",requste);
//释放本节点
travel->state =1;
}
else
{
printf("不满足回收条件\n");
}
travel = travel->next;
}
}

int main()
{

int alloc_arry[] = {4,7,6};
int need_arry [] = {3,5,1};

lpbanker_ST head = (lpbanker_ST)malloc(sizeof(banker_ST));
head->alloc = 0;
head->need = 0;
lpbanker_ST free_node;

CreateLoopLink(head,alloc_arry,need_arry);
request_now(head,1);

while(free_node)
{
free_node = head;
head =head->next;
free(free_node);
free_node = NULL;
}
return 0;
}

你可能感兴趣的:(操作系统)