當前位置:外匯行情大全網 - 助學貸款 - 用C語言或C++編寫操作系統作業:銀行家算法

用C語言或C++編寫操作系統作業:銀行家算法

無死鎖算法。

要解釋銀行家算法,首先要解釋操作系統的安全狀態和不安全狀態。

安全狀態:如果系統中存在由所有進程組成的安全序列P1,…,Pn,則系統處於安全狀態。安全狀態必須是沒有死鎖。

不安全狀態:安全序列不存在。不安全狀態不壹定導致死鎖。

那麽什麽是安全序列呢?

安全序列:流程序列是安全的。如果對於每個進程Pi(1≤i≤n),它在未來仍然需要的資源量不超過系統和所有進程當前剩余的資源PJ (j

銀行家算法:

我們可以把操作系統看成壹個銀行家。操作系統管理的資源相當於莊家管理的資金,進程向操作系統請求分配資源相當於用戶向莊家貸款。操作系統根據銀行家制定的規則為進程分配資源。當進程第壹次申請資源時,它應該測試對資源的最大需求。如果系統的現有資源能夠滿足其最大需求,就應該按照當前的申請量來分配資源,否則就推遲分配。當進程在執行過程中繼續申請資源時,首先測試該進程占用的資源和本次申請的資源之和是否超過該進程對資源的最大需求。如果超過,就會拒絕分配資源;如果沒有超過,它將測試系統中現有的資源是否能夠滿足進程中仍然需要的最大資源量;如果可以,會根據當前申請量分配資源;不然就延期。

算法:

n:系統中進程的總數

m:資源類的總數

可用:數組[1..m]的整數;

Max: ARRAY[1..n,1..m]的整數;

分配:數組[1..n,1..m]的整數;

需要:數組[1..n,1..m]的整數;

請求:數組[1..n,1..m]的整數;

符號描述:

可用的剩余資源

最大最大需求

分配已分配資源

需要需求資源

請求請求資源

當進程pi申請資源時,系統執行以下操作。

步驟:(“=”是賦值符號,“= =”是等號)

如果請求,執行步驟(1)

步驟(2)如果要求

步驟(3)假設系統分配資源,有:

可用=可用-請求;

分配=分配+請求;

需要=需要-請求

如果系統的新狀態是安全的,則分配完成。

如果系統的新狀態是不安全的,則恢復原始狀態,並且該過程等待。

對於安全檢查,定義數據結構:

工作:數組[1..m]的整數;

完成:數組[1..布爾型的n];

安全檢查的步驟:

步驟(1):

工作=可用;

Finish = false

步驟(2)找到我:

a.Finish = = false

b.需要& lt=工作;

如果不存在,轉到步驟(4)

第三步

工作=工作+分配;

Finish = true

轉到步驟(2)

步驟(4)如果所有I的finish = true,則系統處於安全狀態,否則處於不安全狀態。

/*銀行家算法,OS概念六版。

由SCAU的Johnny hagen重新編輯,運行於vc6.0

*/

#包含“malloc.h”

#包含“stdio.h”

#包含" stdlib.h "

#define alloclen sizeof(結構分配)

#define maxlen sizeof(struct max)

#定義avalen sizeof(結構可用)

#定義needlen sizeof(結構需要)

#定義finilen sizeof(結構結束)

#define pathlen sizeof(結構路徑)

結構分配

{

int值;

結構分配* next

};

最大結構

{

int值;

struct max * next

};

可用結構/*可用資源的數量*/

{

int值;

結構可用* next

};

結構需求/*所需資源的數量*/

{

int值;

struct需要* next

};

結構路徑

{

int值;

結構路徑* next

};

結構完成

{

int stat

struct finish * next

};

int main()

{

int row,colum,status=0,I,j,t,temp,processtest

結構分配*allochead,*alloc1,*alloc2,* alloctemp

struct max *maxhead,*maxium1,*maxium2,* maxtemp

struct available *avahead,*available1,*available2,*workhead,*work1,*work2,*worktemp,* work temp 1;

struct need *needhead,*need1,*need2,* needtemp

struct finish *finihead,*finish1,*finish2,* finishtemp

結構路徑*路徑頭,*路徑1,*路徑2;

printf(" \ n請輸入系統資源類型的數量:");

scanf("%d ",& ampcolum);

Printf("請輸入當前內存中的進程數:");

scanf("%d ",& amp排);

Printf("請輸入已分配資源的矩陣:\ n ");

for(I = 0;我& lt排;i++)

{

for(j = 0;j & ltcolumj++)

{

Printf("請輸入分配給進程p%d的%c系統資源:",I,' A '+j);

如果(狀態==0)

{

allochead = alloc 1 = alloc 2 =(struct allocation *)malloc(alloclen);

alloc1->next = alloc 2-& gt;next = NULL

scanf("%d ",& ampallochead-& gt;值);

status++;

}

其他

{

alloc 2 =(struct allocation *)malloc(alloclen);

scanf("%d,%d ",& ampalloc2->值);

if(狀態==1)

{

allochead-& gt;next = alloc2

status++;

}

alloc1->next = alloc2

alloc 1 = alloc 2;

}

}

}

alloc2->next = NULL

狀態= 0;

Printf("請輸入最大需求矩陣:\ n ");

for(I = 0;我& lt排;i++)

{

for(j = 0;j & ltcolumj++)

{

Printf("請輸入進程p%d類別%c系統資源最大需求:",I,' A '+j);

如果(狀態==0)

{

maxhead = maxium 1 = maxium 2 =(struct max *)malloc(maxlen);

maxium 1->;next = maxium 2-& gt;next = NULL

scanf("%d ",& ampmaxium 1->;值);

status++;

}

其他

{

maxium 2 =(struct max *)malloc(maxlen);

scanf("%d,%d ",& ampmaxium 2->;值);

if(狀態==1)

{

maxhead-& gt;next = maxium2

status++;

}

maxium 1->;next = maxium2

maxium 1 = maxium 2;

}

}

}

maxium 2->;next = NULL

狀態= 0;

Printf("請輸入當前系統剩余的資源矩陣:\ n ");

for(j = 0;j & ltcolumj++)

{

Printf("類別%c的剩余系統資源:",' A '+j);

如果(狀態==0)

{

ava head = available 1 = available 2 =(struct available *)malloc(avalen);

work head = work 1 = work 2 =(struct available *)malloc(avalen);

available 1->;next = available 2-& gt;next = NULL

work 1->;next = work 2-& gt;next = NULL

scanf("%d ",& ampavailable 1->;值);

work 1->;value = available 1-& gt;價值;

status++;

}

其他

{

available 2 =(struct available *)malloc(avalen);

work 2 =(struct available *)malloc(avalen);

scanf("%d,%d ",& amp可用2->值);

工作2->;value = available 2-& gt;價值;

if(狀態==1)

{

ava head-& gt;next = available2

工作頭->;next = work2

status++;

}

available 1->;next = available2

available 1 = available 2;

work 1->;next = work2

work 1 = work 2;

}

}

可用2->next = NULL

工作2->;next = NULL

狀態= 0;

alloctemp = allochead

maxtemp = maxhead

for(I = 0;我& lt排;i++)

for(j = 0;j & ltcolumj++)

{

如果(狀態==0)

{

need head = need 1 = need 2 =(struct need *)malloc(needlen);

need 1->;next = need 2-& gt;next = NULL

need 1->;value = maxtemp-& gt;value-allocatemp-& gt;價值;

status++;

}

其他

{

need 2 =(struct need *)malloc(needlen);

need 2->;value =(maxtemp-& gt;value)-(alloct EMP-& gt;值);

if(狀態==1)

{

needhead-& gt;next = need2

status++;

}

need 1->;next = need2

need 1 = need 2;

}

max temp = max temp-& gt;接下來;

alloctemp = alloctemp-& gt;接下來;

}

need 2->;next = NULL

狀態= 0;

for(I = 0;我& lt排;i++)

{

如果(狀態==0)

{

fini head = finish 1 = finish 2 =(struct finish *)malloc(finilen);

finish 1->;next = finish 2-& gt;next = NULL

finish 1->;stat = 0;

status++;

}

其他

{

finish 2 =(struct finish *)malloc(finilen);

完成2->stat = 0;

if(狀態==1)

{

fini head-& gt;next = finish2

status++;

}

finish 1->;next = finish2

finish 1 = finish 2;

}

}

完成2->next = NULL/*初始化完成*/

狀態= 0;

process test = 0;

for(temp = 0;temp & lt排;temp++)

{

alloctemp = allochead

needtemp = needhead

finishtemp = finihead

worktemp = workhead

for(I = 0;我& lt排;i++)

{

工作溫度1 =工作溫度;

if(finish temp-& gt;stat==0)

{

for(j = 0;j & ltcolumj++,need temp = need temp-& gt;接下來,工作溫度=工作溫度-& gt;下壹個)

if(need temp-& gt;價值& lt=工作溫度-& gt;值)

process test++;

if(processtest==colum)

{

for(j = 0;j & ltcolumj++)

{

worktemp1->value+= alloct EMP-& gt;價值;

工作溫度1 =工作溫度1->;接下來;

alloctemp = alloctemp-& gt;接下來;

}

如果(狀態==0)

{

path head = path 1 = path 2 =(struct path *)malloc(path len);

path 1->;next = path 2-& gt;next = NULL

path 1->;value = I;

status++;

}

其他

{

path 2 =(struct path *)malloc(path len);

path2->value = I;

if(狀態==1)

{

path head->;next = path2

status++;

}

path 1->;next = path2

path 1 = path 2;

}

finish temp-& gt;stat = 1;

}

其他

{

for(t = 0;t & ltcolumt++)

alloctemp = alloctemp-& gt;接下來;

finish temp-& gt;stat = 0;

}

}

其他

for(t = 0;t & ltcolumt++)

{

need temp = need temp-& gt;接下來;

alloctemp = alloctemp-& gt;接下來;

}

process test = 0;

worktemp = workhead

finish temp = finish temp-& gt;接下來;

}

}

path2->next = NULL

finishtemp = finihead

for(temp = 0;temp & lt排;temp++)

{

if(finish temp-& gt;stat==0)

{

printf(" \ n系統處於不安全狀態!\ n ");

退出(0);

}

finish temp = finish temp-& gt;接下來;

}

printf(" \ n系統處於安全狀態。\ n ");

printf(" \ n安全序列是:\ n ");

{

printf("p%d ",path head-& gt;值);

}

while(path head = path head-& gt;下壹個);

printf(" \ n ");

返回0;

}

另外站長群上有團購產品,便宜又有保障。

  • 上一篇:銀行抵押貸款和質押貸款的區別
  • 下一篇:有人挑糞,潑在夏翁的衣服上。夏翁為什麽不生氣?
  • copyright 2024外匯行情大全網