银行家算法c语言代码
⑴ 求n个数的全排列,n不定。用c语言。用于银行家算法中求安全序列
好久没用c了,所以代码可能要用到伪代码
先定义a[maxn]
用子函数递归
void p(int x)
{
if (n == x+1)
{
//foreach a print
//输出数组a
}
for (int i=1 to n)
{
a[x] = i;
p(x+1);
a[x] = 0;
}
}
主函数main调用p(n)
⑵ 怎样用C语言实现银行家算法
#include<stdio.h>
struct claim
{
int user;
int num[3];
}claims;
int input()
{
printf("please input your request:user(0~4):\n");
scanf("%d",&claims.user);
printf("input the number of resource a:\n");
scanf("%d",&claims.num[0]);
printf("input the number of resource b:\n");
scanf("%d",&claims.num[1]);
printf("input the number of resource c:\n");
scanf("%d",&claims.num[2]);
return 1;
}
int safety_chk(int alloc[][3],int need[][3],int avail[3])
{
int work[3],finish[5];
for(int p=0;p<5;p++)//i大于2后对WORK是无意义的
{
work[p]=avail[p];
finish[p]=0;
}
for(int i=0;i<5;i++)
{
if(finish[i]==0&&
need[i][0]<=work[0]&&
need[i][1]<=work[1]&&
need[i][2]<=work[2] )
{
for(int j=0;j<3;j++)
work[j]=alloc[i][j]+work[j];
finish[i]=1;
i=-1;//重头再来
}
}
for(i=0;i<5;i++)
{
if(finish[i]==0)
return 0;
}
return 1;
}
int process(int alloc[][3],int need[][3],int avail[3])
{
int ret;
input();
for(int i=0;i<3;i++) //out of resource number
{
if(claims.num[i]>need[claims.user][i]||claims.num[i]>avail[i])
return 0;
}
for(i=0;i<3;i++)//trying
{
avail[i]=avail[i]-claims.num[i];
alloc[claims.user][i]=alloc[claims.user][i]+claims.num[i];
need[claims.user][i]=need[claims.user][i]-claims.num[i];
}
if((ret=safety_chk(alloc,need,avail)==0))
{
printf("safety_chk's result %d \n",0);
for(i=0;i<3;i++)
{
avail[i]=avail[i]+claims.num[i];
alloc[claims.user][i]=alloc[claims.user][i]-claims.num[i];
need[claims.user][i]=need[claims.user][i]+claims.num[i];
}
return 0;
}
else
{
printf("safety_chk's result %d \n",1);
}
return 1;
}
void main()
{
int alloc[5][3]={{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};
int need[5][3]={{7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1}};
int avail[3]={3,3,2};
if(process(alloc,need,avail)==0)
printf("sorry,we cannot help you!\n");
else printf("operation complete!\n");
return;
}
⑶ 银行家算法的C语言程序
1.根据下面给出的系统中资源分配情况,以及各个进程的资源申请情况,通过银行家算法来判断各进程的资源请求能否满足(要求记录程序的运行过程)。 已分配的
⑷ 算法上机实验如图所示,用c语言实现
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;//数据域
struct node *R;//左孩子
struct node *L;//右孩子
};
int a[] = {1, 2, 3, -1, -1, -1, 4, 5,7,-1,-1,-1,6,-1,-1};//二叉树序列
int k = 0;
node* buildTree(node *tree) {//创建二叉树
int data=a[k++];//
if (data== - 1) {//-1代表空结点
tree = NULL;
}
else {//非空结点
tree = (node*)malloc(sizeof(node));//分配内存
tree->data = data;//数据域赋值
tree->L = tree->R = NULL;//左右孩子赋空
tree->L=buildTree(tree->L);//前往左孩子
tree->R=buildTree(tree->R);//前往右孩子
}
return tree;//返回根结点地址
}
void dfs1(node *tree) {//前序遍历
if (tree) {
printf("%d ", tree->data);
dfs1(tree->L);
dfs1(tree->R);
}
}
void dfs2(node *tree) {//中序
if (tree) {
dfs2(tree->L);
printf("%d ", tree->data);
dfs2(tree->R);
}
}
void dfs3(node *tree) {//后序
if (tree) {
dfs3(tree->L);
dfs3(tree->R);
printf("%d ", tree->data);
}
}
void level(node *tree){
//层次遍历,类似与bfs(广度优先搜索)
//需要一个队列作为辅助数据结构
node* q[100];//队列
int f=0,r=0;//头,尾指针
q[r++]=tree;//根结点入队
while(f!=r){
node *t=q[f++];//出队
printf("%d ",t->data);//输出
if(t->L!=NULL){//非空左孩子入队
q[r++]=t->L;
}
if(t->R!=NULL){//非空右孩子入队
q[r++]=t->R;
}
}
}
int count(node *tree){
if(tree==NULL){
return 0;
}
else{
int n,m;
n=count(tree->L);//左子树结点个数
m=count(tree->R);//右子树结点个数
return n+m+1;//返回左右子树结点个数之和
}
}
int main() {
node *tree = NULL;
tree=buildTree(tree);
printf(" 前序遍历: ");
dfs1(tree);
printf(" 中序遍历: ");
dfs2(tree);
printf(" 后序遍历: ");
dfs3(tree);
printf(" 层次遍历: ");
level(tree);
printf(" 二叉树结点个数:%d",count(tree));
return 0;
}
⑸ 改程序——银行家算法C语言版
你是学软件的学生吗 ?
我是学软件的,我在操作系统里学了银行家算法,等我看了,在告诉你我的看法,好吗?
愿意的话希望交流一下 我的邮箱[email protected]
⑹ c语言银行家算法安全性判别
把1作为参数传给yanzheng() yanzheng(int m)
然后验证函数里修改:
work=Avaliable;
i=m;
while(i<m)
{
if(Finish[i]==false&&Need[i]<=work)
{
work=work+Allocation[i];
Finish[i]=true;
anquan[k]=i;
k++;
i=0;
}
else
i++;
}
⑺ 关于银行家算法中部分代码不理解,请高手帮忙指点下谢谢!!!
lass ThreadTest {
static int type = 4, num = 10; //定义资源数目和线程数目
static int[] resource = new int[type]; //系统资源总数
//static int[] Resource = new int[type]; //副本
static Random rand = new Random();
static Bank[] bank = new Bank[num]; //线程组
Bank temp = new Bank();
public void init() {
//初始化组中每个线程,随机填充系统资源总数
for(int i = 0; i < type; i++)
resource[i] = rand.nextInt(10) + 80;
System.out.print("Resource:");
for(int i = 0; i < type; i++)
System.out.print(" " + resource[i]);
System.out.println("");
for(int i = 0; i < bank.length; i++)
bank[i] = new Bank("#" + i);
}
public ThreadTest4() {
init();
}
class Bank extends Thread {
//银行家算法避免死锁
public int[]
max = new int[type], //总共需求量
need = new int[type], //尚需资源量
allocation = new int[type]; //已分配量
private int[]
request = new int[type], //申请资源量
Resource = new int[type]; //资源副本
private boolean isFinish = false; //线程是否完成
int[][] table = new int[bank.length][type*4]; //二维资源分配表
private void init() {
// 随机填充总共、尚需、已分配量
synchronized(resource) {
for(int i = 0; i < type; i++) {
max[i] = rand.nextInt(5) + 10;
need[i] = rand.nextInt(10);
allocation[i] = max[i] - need[i];
resource[i] -= allocation[i]; //从系统资源中减去已分配的
}
printer();
for(int i = 0; i < type; i++) {
if(resource[i] < 0) {
//若出现已分配量超出系统资源总数的错误则退出
System.out.println("The summation of Threads' allocations is out of range!");
System.exit(1);
}
}
}
}
public Bank(String s) {
setName(s);
init();
start();
}
public Bank() {
//none
}
public void run() {
try {
sleep(rand.nextInt(2000));
}
catch(InterruptedException e) {
throw new RuntimeException(e);
}
while(true) {
//程序没有完成时一直不断申请资源
if(askFor() == false) {
try {
sleep(1000);
}
catch(InterruptedException e) {
throw new RuntimeException(e);
}
}
else
tryRequest();
if(noNeed() == true)
break;
}
//休眠一段时间模拟程序运行
try {
sleep(1000);
}
catch(InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(getName() + " finish!");
synchronized(resource) {
//运行结束释放占有资源
for(int i = 0; i < type; i++) {
resource[i] += allocation[i];
need[i] = allocation[i] = max[i] = 0;
}
}
}
private void printer() {
//打印当前资源信息
System.out.print(getName() + " Max:");
for(int i = 0; i < type; i++)
System.out.print(" " + max[i]);
System.out.print(" Allocation:");
for(int i = 0; i < type; i++)
System.out.print(" " + allocation[i]);
System.out.print(" Need:");
for(int i = 0; i < type; i++)
System.out.print(" " + need[i]);
System.out.print(" Available:");
for(int i = 0; i < type; i++)
System.out.print(" " + resource[i]);
System.out.println("");
}
private boolean askFor() {
//随机产生申请资源量并检测是否超标
boolean canAsk = false;
for(int i = 0; i < type; i++) {
request[i] = rand.nextInt(20);
//防止申请量超过所需量
if(request[i] > need[i])
request[i] = need[i];
}
for(int i = 0; i < type; i++) //防止随机申请资源全为0
if(request[i] > 0)
canAsk = true;
synchronized(resource) {
//锁住可供资源检查是否超标
for(int i = 0; i < type; i++) {
if(request[i] > resource[i])
//如果申请资源超过可供资源则等待一段时间后重新申请
return false;
}
}
return canAsk;
}
private void tryRequest() {
//创建副本尝试分配请求
synchronized(resource) {
for(int i = 0; i < type; i++)
//依然要防止请求量超出范围
if(request[i] > resource[i])
return;
for(int i = 0; i < type; i++) {
//复制资源量并减去需求量到一个副本上
Resource[i] = resource[i];
Resource[i] -= request[i];
}
System.out.print(getName() + " ask for:");
for(int i = 0; i < type; i++)
System.out.print(" " + request[i]);
System.out.println("");
if(checkSafe() == true) {
//如果检查安全则将副本值赋给资源量并修改占有量和需求量
for(int i = 0; i < type; i++) {
resource[i] = Resource[i];
allocation[i] += request[i];
need[i] -= request[i];
}
System.out.println(getName() + " request succeed!");
}
else
System.out.println(getName() + " request fail!");
}
}
private boolean checkSafe() {
//银行家算法检查安全性
synchronized(bank) {
//将线程资源信息放入二维资源分配表检查安全性,0~type可用资源/type~type*2所需资源/type*2~type*3占有资源/type*3~-1可用+占用资源
for(int i = 0; i < bank.length; i++) {
for(int j = type; j < type*2; j++) {
table[i][j] = bank[i].need[j%type];
}
for(int j = type*2; j < type*3; j++) {
table[i][j] = bank[i].allocation[j%type];
}
}
//冒泡排序按需求资源从小到大排
for(int i = 0; i < bank.length; i++) {
for(int j = i; j < bank.length-1; j++) {
sort(j, 4);
}
}
//进行此时刻的安全性检查
for(int i = 0; i < type; i++) {
table[0][i] = Resource[i];
table[0][i+type*3] = table[0][i] + table[0][i+type*2];
if(table[0][i+type*3] < table[1][i+type])
return false;
}
for(int j = 1; j < bank.length-1; j++) {
for(int k = 0; k < type; k++) {
table[j][k] = table[j-1][k+type*3];
table[j][k+type*3] = table[j][k] + table[j][k+type*2];
if(table[j][k+type*3] < table[j+1][k+type])
return false;
}
}
}
return true;
}
private void sort(int j, int k) {
//递归冒泡排序
int tempNum;
if(table[j][k] > table[j+1][k]) {
for(int i = type; i < type*2; i++) {
tempNum = table[j][i];
table[j][i] = table[j+1][i];
table[j+1][i] = tempNum;
}
/*temp = bank[j];
bank[j] = bank[j+1];
bank[j+1] = temp;*/
}
else if(table[j][k] == table[j+1][k] && k < type*2) //此资源量相同时递归下一个资源量排序并且防止超出范围
sort(j, k+1);
}
private boolean noNeed() {
//是否还需要资源
boolean finish = true;
for(int i = 0; i < type; i++) {
if(need[i] != 0) {
finish = false;
break;
}
}
return finish;
}
}
public static void main(String[] args) {
ThreadTest t = new ThreadTest();
//后台线程,设定程序运行多长时间后自动结束
new Timeout(30000, "---Stop!!!---");
}
}
⑻ 高分求银行家算法c语言版
#include "malloc.h"
#include "stdio.h"
#include "stdlib.h"
#define alloclen sizeof(struct allocation)
#define maxlen sizeof(struct max)
#define avalen sizeof(struct available)
#define needlen sizeof(struct need)
#define finilen sizeof(struct finish)
#define pathlen sizeof(struct path)
struct allocation
{
int value;
struct allocation *next;
};
struct max
{
int value;
struct max *next;
};
struct available /*可用资源数*/
{
int value;
struct available *next;
};
struct need /*需求资源数*/
{
int value;
struct need *next;
};
struct path
{
int value;
struct path *next;
};
struct finish
{
int stat;
struct finish *next;
};
int main()
{
int row,colum,status=0,i,j,t,temp,processtest;
struct allocation *allochead,*alloc1,*alloc2,*alloctemp;
struct max *maxhead,*maxium1,*maxium2,*maxtemp;
struct available *avahead,*available1,*available2,*workhead,*work1,*work2,*worktemp,*worktemp1;
struct need *needhead,*need1,*need2,*needtemp;
struct finish *finihead,*finish1,*finish2,*finishtemp;
struct path *pathhead,*path1,*path2;
printf("\n请输入系统资源的种类数:");
scanf("%d",&colum);
printf("请输入现时内存中的进程数:");
scanf("%d",&row);
printf("请输入已分配资源矩阵:\n");
for(i=0;i<row;i++)
{
for (j=0;j<colum;j++)
{
printf("请输入已分配给进程 p%d 的 %c 种系统资源:",i,'A'+j);
if(status==0)
{
allochead=alloc1=alloc2=(struct allocation*)malloc(alloclen);
alloc1->next=alloc2->next=NULL;
scanf("%d",&allochead->value);
status++;
}
else
{
alloc2=(struct allocation *)malloc(alloclen);
scanf("%d,%d",&alloc2->value);
if(status==1)
{
allochead->next=alloc2;
status++;
}
alloc1->next=alloc2;
alloc1=alloc2;
}
}
}
alloc2->next=NULL;
status=0;
printf("请输入最大需求矩阵:\n");
for(i=0;i<row;i++)
{
for (j=0;j<colum;j++)
{
printf("请输入进程 p%d 种类 %c 系统资源最大需求:",i,'A'+j);
if(status==0)
{
maxhead=maxium1=maxium2=(struct max*)malloc(maxlen);
maxium1->next=maxium2->next=NULL;
scanf("%d",&maxium1->value);
status++;
}
else
{
maxium2=(struct max *)malloc(maxlen);
scanf("%d,%d",&maxium2->value);
if(status==1)
{
maxhead->next=maxium2;
status++;
}
maxium1->next=maxium2;
maxium1=maxium2;
}
}
}
maxium2->next=NULL;
status=0;
printf("请输入现时系统剩余的资源矩阵:\n");
for (j=0;j<colum;j++)
{
printf("种类 %c 的系统资源剩余:",'A'+j);
if(status==0)
{
avahead=available1=available2=(struct available*)malloc(avalen);
workhead=work1=work2=(struct available*)malloc(avalen);
available1->next=available2->next=NULL;
work1->next=work2->next=NULL;
scanf("%d",&available1->value);
work1->value=available1->value;
status++;
}
else
{
available2=(struct available*)malloc(avalen);
work2=(struct available*)malloc(avalen);
scanf("%d,%d",&available2->value);
work2->value=available2->value;
if(status==1)
{
avahead->next=available2;
workhead->next=work2;
status++;
}
available1->next=available2;
available1=available2;
work1->next=work2;
work1=work2;
}
}
available2->next=NULL;
work2->next=NULL;
status=0;
alloctemp=allochead;
maxtemp=maxhead;
for(i=0;i<row;i++)
for (j=0;j<colum;j++)
{
if(status==0)
{
needhead=need1=need2=(struct need*)malloc(needlen);
need1->next=need2->next=NULL;
need1->value=maxtemp->value-alloctemp->value;
status++;
}
else
{
need2=(struct need *)malloc(needlen);
need2->value=(maxtemp->value)-(alloctemp->value);
if(status==1)
{
needhead->next=need2;
status++;
}
need1->next=need2;
need1=need2;
}
maxtemp=maxtemp->next;
alloctemp=alloctemp->next;
}
need2->next=NULL;
status=0;
for(i=0;i<row;i++)
{
if(status==0)
{
finihead=finish1=finish2=(struct finish*)malloc(finilen);
finish1->next=finish2->next=NULL;
finish1->stat=0;
status++;
}
else
{
finish2=(struct finish*)malloc(finilen);
finish2->stat=0;
if(status==1)
{
finihead->next=finish2;
status++;
}
finish1->next=finish2;
finish1=finish2;
}
}
finish2->next=NULL; /*Initialization compleated*/
status=0;
processtest=0;
for(temp=0;temp<row;temp++)
{
alloctemp=allochead;
needtemp=needhead;
finishtemp=finihead;
worktemp=workhead;
for(i=0;i<row;i++)
{
worktemp1=worktemp;
if(finishtemp->stat==0)
{
for(j=0;j<colum;j++,needtemp=needtemp->next,worktemp=worktemp->next)
if(needtemp->value<=worktemp->value)
processtest++;
if(processtest==colum)
{
for(j=0;j<colum;j++)
{
worktemp1->value+=alloctemp->value;
worktemp1=worktemp1->next;
alloctemp=alloctemp->next;
}
if(status==0)
{
pathhead=path1=path2=(struct path*)malloc(pathlen);
path1->next=path2->next=NULL;
path1->value=i;
status++;
}
else
{
path2=(struct path*)malloc(pathlen);
path2->value=i;
if(status==1)
{
pathhead->next=path2;
status++;
}
path1->next=path2;
path1=path2;
}
finishtemp->stat=1;
}
else
{
for(t=0;t<colum;t++)
alloctemp=alloctemp->next;
finishtemp->stat=0;
}
}
else
for(t=0;t<colum;t++)
{
needtemp=needtemp->next;
alloctemp=alloctemp->next;
}
processtest=0;
worktemp=workhead;
finishtemp=finishtemp->next;
}
}
path2->next=NULL;
finishtemp=finihead;
for(temp=0;temp<row;temp++)
{
if(finishtemp->stat==0)
{
printf("\n系统处于非安全状态!\n");
exit(0);
}
finishtemp=finishtemp->next;
}
printf("\n系统处于安全状态.\n");
printf("\n安全序列为: \n");
do
{
printf("p%d ",pathhead->value);
}
while(pathhead=pathhead->next);
printf("\n");
return 0;
}
⑼ 银行家算法的算法实现
在避免死锁的方法中,所施加的限制条件较弱,有可能获得令人满意的系统性能。在该方法中把系统的状态分为安全状态和不安全状态,只要能使系统始终都处于安全状态,便可以避免发生死锁。
银行家算法的基本思想是分配资源之前,判断系统是否是安全的;若是,才分配。它是最具有代表性的避免死锁的算法。
设进程cusneed提出请求REQUEST [i],则银行家算法按如下规则进行判断。
(1)如果REQUEST [cusneed] [i]<= NEED[cusneed][i],则转(2);否则,出错。
(2)如果REQUEST [cusneed] [i]<= AVAILABLE[i],则转(3);否则,等待。
(3)系统试探分配资源,修改相关数据:
AVAILABLE[i]-=REQUEST[cusneed][i];
ALLOCATION[cusneed][i]+=REQUEST[cusneed][i];
NEED[cusneed][i]-=REQUEST[cusneed][i];
(4)系统执行安全性检查,如安全,则分配成立;否则试探险性分配作废,系统恢复原状,进程等待。 (1)设置两个工作向量Work=AVAILABLE;FINISH
(2)从进程集合中找到一个满足下述条件的进程,
FINISH==false;
NEED<=Work;
如找到,执行(3);否则,执行(4)
(3)设进程获得资源,可顺利执行,直至完成,从而释放资源。
Work=Work+ALLOCATION;
Finish=true;
GOTO 2
(4)如所有的进程Finish= true,则表示安全;否则系统不安全。
银行家算法流程图
算法(C语言实现) #include<STRING.H>#include<stdio.h>#include<stdlib.h>#include<CONIO.H>/*用到了getch()*/#defineM5/*进程数*/#defineN3/*资源数*/#defineFALSE0#defineTRUE1/*M个进程对N类资源最大资源需求量*/intMAX[M][N]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};/*系统可用资源数*/intAVAILABLE[N]={10,5,7};/*M个进程已分配到的N类数量*/intALLOCATION[M][N]={{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}};/*M个进程已经得到N类资源的资源量*/intNEED[M][N]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};/*M个进程还需要N类资源的资源量*/intRequest[N]={0,0,0};voidmain(){inti=0,j=0;charflag;voidshowdata();voidchangdata(int);voidrstordata(int);intchkerr();showdata();enter:{printf(请输入需申请资源的进程号(从0到);printf(%d,M-1);printf():);scanf(%d,&i);}if(i<0||i>=M){printf(输入的进程号不存在,重新输入!
);gotoenter;}err:{printf(请输入进程);printf(%d,i);printf(申请的资源数
);printf(类别:ABC
);printf();for(j=0;j<N;j++){scanf(%d,&Request[j]);if(Request[j]>NEED[i][j]){printf(%d,i);printf(号进程);printf(申请的资源数>进程);printf(%d,i);printf(还需要);printf(%d,j);printf(类资源的资源量!申请不合理,出错!请重新选择!
);gotoerr;}else{if(Request[j]>AVAILABLE[j]){printf(进程);printf(%d,i);printf(申请的资源数大于系统可用);printf(%d,j);printf(类资源的资源量!申请不合理,出错!请重新选择!
);gotoerr;}}}}changdata(i);if(chkerr()){rstordata(i);showdata();}elseshowdata();printf(
);printf(按'y'或'Y'键继续,否则退出
);flag=getch();if(flag=='y'||flag=='Y'){gotoenter;}else{exit(0);}}/*显示数组*/voidshowdata(){inti,j;printf(系统可用资源向量:
);printf(***Available***
);printf(资源类别:ABC
);printf(资源数目:);for(j=0;j<N;j++){printf(%d,AVAILABLE[j]);}printf(
);printf(
);printf(各进程还需要的资源量:
);printf(******Need******
);printf(资源类别:ABC
);for(i=0;i<M;i++){printf();printf(%d,i);printf(号进程:);for(j=0;j<N;j++){printf(%d,NEED[i][j]);}printf(
);}printf(
);printf(各进程已经得到的资源量:
);printf(***Allocation***
);printf(资源类别:ABC
);for(i=0;i<M;i++){printf();printf(%d,i);printf(号进程:);/*printf(:
);*/for(j=0;j<N;j++){printf(%d,ALLOCATION[i][j]);}printf(
);}printf(
);}/*系统对进程请求响应,资源向量改变*/voidchangdata(intk){intj;for(j=0;j<N;j++){AVAILABLE[j]=AVAILABLE[j]-Request[j];ALLOCATION[k][j]=ALLOCATION[k][j]+Request[j];NEED[k][j]=NEED[k][j]-Request[j];}}/*资源向量改变*/voidrstordata(intk){intj;for(j=0;j<N;j++){AVAILABLE[j]=AVAILABLE[j]+Request[j];ALLOCATION[k][j]=ALLOCATION[k][j]-Request[j];NEED[k][j]=NEED[k][j]+Request[j];}}/*安全性检查函数*/intchkerr()//在假定分配资源的情况下检查系统的安全性{intWORK[N],FINISH[M],temp[M];//temp[]用来记录进程安全执行的顺序inti,j,m,k=0,count;for(i=0;i<M;i++)FINISH[i]=FALSE;for(j=0;j<N;j++)WORK[j]=AVAILABLE[j];//把可利用资源数赋给WORK[]for(i=0;i<M;i++){count=0;for(j=0;j<N;j++)if(FINISH[i]==FALSE&&NEED[i][j]<=WORK[j])count++;if(count==N)//当进程各类资源都满足NEED<=WORK时{for(m=0;m<N;m++)WORK[m]=WORK[m]+ALLOCATION[i][m];FINISH[i]=TRUE;temp[k]=i;//记录下满足条件的进程k++;i=-1;}}for(i=0;i<M;i++)if(FINISH[i]==FALSE){printf(系统不安全!!!本次资源申请不成功!!!
);return1;}printf(
);printf(经安全性检查,系统安全,本次分配成功。
);printf(
);printf(本次安全序列:);for(i=0;i<M;i++)//打印安全系统的进程调用顺序{printf(进程);printf(%d,temp[i]);if(i<M-1)printf(->);}printf(
);return0;}
⑽ 急!银行家算法用C语言编写.全部程序.
银行家算法
银行家算法是一种最有代表性的避免死锁的算法。
要解释银行家算法,必须先解释操作系统安全状态和不安全状态。
安全状态:如果存在一个由系统中所有进程构成的安全序列P1,…,Pn,则系统处于安全状态。安全状态一定是没有死锁发生。
不安全状态:不存在一个安全序列。不安全状态不一定导致死锁。
那么什么是安全序列呢?
安全序列:一个进程序列{P1,…,Pn}是安全的,如果对于每一个进程Pi(1≤i≤n),它以后尚需要的资源量不超过系统当前剩余资源量与所有进程Pj (j < i )当前占有资源量之和。
银行家算法:
我们可以把操作系统看作是银行家,操作系统管理的资源相当于银行家管理的资金,进程向操作系统请求分配资源相当于用户向银行家贷款。操作系统按照银行家制定的规则为进程分配资源,当进程首次申请资源时,要测试该进程对资源的最大需求量,如果系统现存的资源可以满足它的最大需求量则按当前的申请量分配资源,否则就推迟分配。当进程在执行中继续申请资源时,先测试该进程已占用的资源数与本次申请的资源数之和是否超过了该进程对资源的最大需求量。若超过则拒绝分配资源,若没有超过则再测试系统现存的资源能否满足该进程尚需的最大资源量,若能满足则按当前的申请量分配资源,否则也要推迟分配。
算法:
n:系统中进程的总数
m:资源类总数
Available: ARRAY[1..m] of integer;
Max: ARRAY[1..n,1..m] of integer;
Allocation: ARRAY[1..n,1..m] of integer;
Need: ARRAY[1..n,1..m] of integer;
Request: ARRAY[1..n,1..m] of integer;
符号说明:
Available 可用剩余资源
Max 最大需求
Allocation 已分配资源
Need 需求资源
Request 请求资源
当进程pi提出资源申请时,系统执行下列
步骤:(“=”为赋值符号,“==”为等号)
step(1)若Request<=Need, goto step(2);否则错误返回
step(2)若Request<=Available, goto step(3);否则进程等待
step(3)假设系统分配了资源,则有:
Available=Available-Request;
Allocation=Allocation+Request;
Need=Need-Request
若系统新状态是安全的,则分配完成
若系统新状态是不安全的,则恢复原状态,进程等待
为进行安全性检查,定义数据结构:
Work:ARRAY[1..m] of integer;
Finish:ARRAY[1..n] of Boolean;
安全性检查的步骤:
step (1):
Work=Available;
Finish=false;
step (2) 寻找满足条件的i:
a.Finish==false;
b.Need<=Work;
如果不存在,goto step(4)
step(3)
Work=Work+Allocation;
Finish=true;
goto step(2)
step (4) 若对所有i,Finish=true,则系统处于安全状态,否则处于不安全状态
/* 银行家算法,操作系统概念(OS concepts Six Edition)
reedit by Johnny hagen,SCAU,run at vc6.0
*/
#include "malloc.h"
#include "stdio.h"
#include "stdlib.h"
#define alloclen sizeof(struct allocation)
#define maxlen sizeof(struct max)
#define avalen sizeof(struct available)
#define needlen sizeof(struct need)
#define finilen sizeof(struct finish)
#define pathlen sizeof(struct path)
struct allocation
{
int value;
struct allocation *next;
};
struct max
{
int value;
struct max *next;
};
struct available /*可用资源数*/
{
int value;
struct available *next;
};
struct need /*需求资源数*/
{
int value;
struct need *next;
};
struct path
{
int value;
struct path *next;
};
struct finish
{
int stat;
struct finish *next;
};
int main()
{
int row,colum,status=0,i,j,t,temp,processtest;
struct allocation *allochead,*alloc1,*alloc2,*alloctemp;
struct max *maxhead,*maxium1,*maxium2,*maxtemp;
struct available *avahead,*available1,*available2,*workhead,*work1,*work2,*worktemp,*worktemp1;
struct need *needhead,*need1,*need2,*needtemp;
struct finish *finihead,*finish1,*finish2,*finishtemp;
struct path *pathhead,*path1,*path2;
printf("\n请输入系统资源的种类数:");
scanf("%d",&colum);
printf("请输入现时内存中的进程数:");
scanf("%d",&row);
printf("请输入已分配资源矩阵:\n");
for(i=0;i<row;i++)
{
for (j=0;j<colum;j++)
{
printf("请输入已分配给进程 p%d 的 %c 种系统资源:",i,'A'+j);
if(status==0)
{
allochead=alloc1=alloc2=(struct allocation*)malloc(alloclen);
alloc1->next=alloc2->next=NULL;
scanf("%d",&allochead->value);
status++;
}
else
{
alloc2=(struct allocation *)malloc(alloclen);
scanf("%d,%d",&alloc2->value);
if(status==1)
{
allochead->next=alloc2;
status++;
}
alloc1->next=alloc2;
alloc1=alloc2;
}
}
}
alloc2->next=NULL;
status=0;
printf("请输入最大需求矩阵:\n");
for(i=0;i<row;i++)
{
for (j=0;j<colum;j++)
{
printf("请输入进程 p%d 种类 %c 系统资源最大需求:",i,'A'+j);
if(status==0)
{
maxhead=maxium1=maxium2=(struct max*)malloc(maxlen);
maxium1->next=maxium2->next=NULL;
scanf("%d",&maxium1->value);
status++;
}
else
{
maxium2=(struct max *)malloc(maxlen);
scanf("%d,%d",&maxium2->value);
if(status==1)
{
maxhead->next=maxium2;
status++;
}
maxium1->next=maxium2;
maxium1=maxium2;
}
}
}
maxium2->next=NULL;
status=0;
printf("请输入现时系统剩余的资源矩阵:\n");
for (j=0;j<colum;j++)
{
printf("种类 %c 的系统资源剩余:",'A'+j);
if(status==0)
{
avahead=available1=available2=(struct available*)malloc(avalen);
workhead=work1=work2=(struct available*)malloc(avalen);
available1->next=available2->next=NULL;
work1->next=work2->next=NULL;
scanf("%d",&available1->value);
work1->value=available1->value;
status++;
}
else
{
available2=(struct available*)malloc(avalen);
work2=(struct available*)malloc(avalen);
scanf("%d,%d",&available2->value);
work2->value=available2->value;
if(status==1)
{
avahead->next=available2;
workhead->next=work2;
status++;
}
available1->next=available2;
available1=available2;
work1->next=work2;
work1=work2;
}
}
available2->next=NULL;
work2->next=NULL;
status=0;
alloctemp=allochead;
maxtemp=maxhead;
for(i=0;i<row;i++)
for (j=0;j<colum;j++)
{
if(status==0)
{
needhead=need1=need2=(struct need*)malloc(needlen);
need1->next=need2->next=NULL;
need1->value=maxtemp->value-alloctemp->value;
status++;
}
else
{
need2=(struct need *)malloc(needlen);
need2->value=(maxtemp->value)-(alloctemp->value);
if(status==1)
{
needhead->next=need2;
status++;
}
need1->next=need2;
need1=need2;
}
maxtemp=maxtemp->next;
alloctemp=alloctemp->next;
}
need2->next=NULL;
status=0;
for(i=0;i<row;i++)
{
if(status==0)
{
finihead=finish1=finish2=(struct finish*)malloc(finilen);
finish1->next=finish2->next=NULL;
finish1->stat=0;
status++;
}
else
{
finish2=(struct finish*)malloc(finilen);
finish2->stat=0;
if(status==1)
{
finihead->next=finish2;
status++;
}
finish1->next=finish2;
finish1=finish2;
}
}
finish2->next=NULL; /*Initialization compleated*/
status=0;
processtest=0;
for(temp=0;temp<row;temp++)
{
alloctemp=allochead;
needtemp=needhead;
finishtemp=finihead;
worktemp=workhead;
for(i=0;i<row;i++)
{
worktemp1=worktemp;
if(finishtemp->stat==0)
{
for(j=0;j<colum;j++,needtemp=needtemp->next,worktemp=worktemp->next)
if(needtemp->value<=worktemp->value)
processtest++;
if(processtest==colum)
{
for(j=0;j<colum;j++)
{
worktemp1->value+=alloctemp->value;
worktemp1=worktemp1->next;
alloctemp=alloctemp->next;
}
if(status==0)
{
pathhead=path1=path2=(struct path*)malloc(pathlen);
path1->next=path2->next=NULL;
path1->value=i;
status++;
}
else
{
path2=(struct path*)malloc(pathlen);
path2->value=i;
if(status==1)
{
pathhead->next=path2;
status++;
}
path1->next=path2;
path1=path2;
}
finishtemp->stat=1;
}
else
{
for(t=0;t<colum;t++)
alloctemp=alloctemp->next;
finishtemp->stat=0;
}
}
else
for(t=0;t<colum;t++)
{
needtemp=needtemp->next;
alloctemp=alloctemp->next;
}
processtest=0;
worktemp=workhead;
finishtemp=finishtemp->next;
}
}
path2->next=NULL;
finishtemp=finihead;
for(temp=0;temp<row;temp++)
{
if(finishtemp->stat==0)
{
printf("\n系统处于非安全状态!\n");
exit(0);
}
finishtemp=finishtemp->next;
}
printf("\n系统处于安全状态.\n");
printf("\n安全序列为: \n");
do
{
printf("p%d ",pathhead->value);
}
while(pathhead=pathhead->next);
printf("\n");
return 0;
}