本文目錄一覽:
- 1、求一個銀行家演算法c語言模擬,能運行的,謝謝。
- 2、c語言銀行家演算法安全性判別
- 3、求n個數的全排列,n不定。用c語言。用於銀行家演算法中求安全序列
- 4、銀行家演算法的C語言程序
- 5、急!銀行家演算法用C語言編寫.全部程序.
- 6、c程序調用函數出錯,關於sprintf
求一個銀行家演算法c語言模擬,能運行的,謝謝。
在什麼地方運行(即運行環境是什麼),用什麼語言寫,要達到什麼功能,你要說清楚
c語言銀行家演算法安全性判別
把1作為參數傳給yanzheng() yanzheng(int m)
然後驗證函數里修改:
work=Avaliable;
i=m;
while(im)
{
if (Finish[i]==falseNeed[i]=work)
{
work=work+Allocation[i];
Finish[i]=true;
anquan[k]=i;
k++;
i = 0;
}
else
i++;
}
求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語言程序
1.根據下面給出的系統中資源分配情況,以及各個進程的資源申請情況,通過銀行家演算法來判斷各進程的資源請求能否滿足(要求記錄程序的運行過程)。 已分配的
急!銀行家演算法用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;irow;i++)
{
for (j=0;jcolum;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;irow;i++)
{
for (j=0;jcolum;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;jcolum;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;irow;i++)
for (j=0;jcolum;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;irow;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;temprow;temp++)
{
alloctemp=allochead;
needtemp=needhead;
finishtemp=finihead;
worktemp=workhead;
for(i=0;irow;i++)
{
worktemp1=worktemp;
if(finishtemp-stat==0)
{
for(j=0;jcolum;j++,needtemp=needtemp-next,worktemp=worktemp-next)
if(needtemp-value=worktemp-value)
processtest++;
if(processtest==colum)
{
for(j=0;jcolum;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;tcolum;t++)
alloctemp=alloctemp-next;
finishtemp-stat=0;
}
}
else
for(t=0;tcolum;t++)
{
needtemp=needtemp-next;
alloctemp=alloctemp-next;
}
processtest=0;
worktemp=workhead;
finishtemp=finishtemp-next;
}
}
path2-next=NULL;
finishtemp=finihead;
for(temp=0;temprow;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;
}
c程序調用函數出錯,關於sprintf
你用了sprintf後只是格式化了
字元串
,想要在終端輸出還是要printf那個格式化後的字元串的。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/154170.html