-
大小: 6KB文件類型: .py金幣: 1下載: 0 次發(fā)布日期: 2021-06-02
- 語言: Python
- 標(biāo)簽: 操作系統(tǒng)??python??
資源簡介
進程狀態(tài)模擬轉(zhuǎn)換課程設(shè)計代碼,使用python編寫,設(shè)計要求:模擬兩種系統(tǒng)資源,每種資源的初始數(shù)目有自己設(shè)定
有進程的標(biāo)識、進程的具體數(shù)據(jù)結(jié)構(gòu)由自己設(shè)定
利用隊列的概念。設(shè)置就緒隊列和阻塞隊列
至少模擬四種條件下的進程狀態(tài)轉(zhuǎn)換,即進程調(diào)度、時間片用完、I/O請求和I/O完成等四種條件
代碼片段和文件信息
#?coding=utf-8
import?random
import?time
resource1=3
resource2=3
ready?=?[]??#?就緒狀態(tài)隊列
block?=?[]??#?阻塞狀態(tài)隊列
creat=?[]?#???創(chuàng)建狀態(tài)隊列
def?paixu(list?lens):??#?按優(yōu)先級排序
????temp?=?[]
????for?j?in?range(0?lens):
????????for?k?in?range(0?lens?-?j?-?1):
????????????if?list[k][3]?????????????????temp?=?list[k]
????????????????list[k]?=?list[k?+?1]
????????????????list[k?+?1]?=?temp
def?creat_new():
????????global?resource1resource2
????????print(‘*******進程獲得了執(zhí)行所需的資源******‘)
????????#?print(‘創(chuàng)建一個進程前,資源1為%d,資源2剩余%d‘?%?(a?b))
????????#?print(resource1resource2)
????????resource1?=resource1-?need1
????????resource2?=resource2-?need2
???????#?print(need1need2)
???????#?print(resource1?resource2)
????????#?print(‘創(chuàng)建一個進程后,資源1剩余%d,資源2剩余%d‘%(resource1resource2))
????????counter?=?random.randint(1?8)??#?隨機產(chǎn)生進程執(zhí)行所?需的時間片
????????priority?=?random.randint(1?30)??#?優(yōu)先級隨機產(chǎn)生范圍為1-30
????????id?=?creat[0]+1
????????zhouzhuan?=?[]??#?周轉(zhuǎn)時間列表,用于保存進程每次執(zhí)行開始的時間
????????state?=?0
????????io?=?random.randint(0?1)??#?進程是否需要io
????????print(‘----正在創(chuàng)建標(biāo)號為%d的進程----‘?%?(creat[0]?+?1))
????????#time.sleep(0.5)
????????print(‘----進?程?創(chuàng)?建?成?功----‘)
????????del?creat[0]
????????#time.sleep(0.5)
????????task?=?[id?state?priority?counter?zhouzhuan?io?need1?need2]??#?建立進程列表
????????ready.append(task)
????????print(‘該進程已加入就緒隊列中,此時就緒隊列中的進程數(shù)為%d\n‘?%?(len(ready)))
????????#time.sleep(0.5)
def?show_num():
????????print(‘***此時就緒隊列中的進程數(shù)為%d***‘?%?(len(ready)))
????????print(‘***此時阻塞隊列中的進程數(shù)為%d***‘?%?(len(block)))
????????print(‘***此時處于創(chuàng)建狀態(tài)的進程數(shù)為%d***\n‘%(len(creat)))
def?jiance():
????global?resource1?resource2
????global?need1need2
????while?len(creat)>0:
????????need1?=?random.randint(0?2)??#?定義進程執(zhí)行需要的第一種系統(tǒng)資源
????????need2?=?random.randint(0?2)??#?定義進程執(zhí)行需要的第二種系統(tǒng)資源
????????print(‘需求A:%d資源A:%d‘?%?(need1?resource1))
????????print(‘需求B:%d資源B:%d‘?%?(need2?resource2))
????????if?(need1?<=?resource1?and?need2?<=resource2):
????????????creat_new()
????????????print(‘創(chuàng)建一個進程后,資源A剩余%d,資源B剩余%d‘?%?(resource1?resource2))
????????else:
????????????print(‘資源再次不足,等待系統(tǒng)再次釋放資源‘)
????????????show_num()
????????????break
def?io(readyblock):
????for?every?in?block:
????????if?every[5]==0:
????????????every[5]=int(input(‘請為標(biāo)號為%d的進程執(zhí)行I/O操作(輸入1):‘%ev
- 上一篇:python五子棋代碼
- 下一篇:協(xié)同過濾代碼實現(xiàn)
評論
共有 條評論