操作系統(tǒng)課程設計-linux系統(tǒng)下實現(xiàn)PV操作_第1頁
操作系統(tǒng)課程設計-linux系統(tǒng)下實現(xiàn)PV操作_第2頁
操作系統(tǒng)課程設計-linux系統(tǒng)下實現(xiàn)PV操作_第3頁
操作系統(tǒng)課程設計-linux系統(tǒng)下實現(xiàn)PV操作_第4頁
操作系統(tǒng)課程設計-linux系統(tǒng)下實現(xiàn)PV操作_第5頁
已閱讀5頁,還剩14頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

PAGEPAGE10/19 計算機科學與通信工程學院下實現(xiàn) 姓名學號:指導老師:年 月 日、實驗題3二、實驗的和要求 3三、環(huán)境配置 4四、設計思路 6五、代碼實現(xiàn) 8六、總結 17、 實驗題目()認識() 安裝()終端的簡單使用() 源碼安裝水果分配的問題:桌上有只盤子,每次只能放入 只水果。爸爸專放蘋果,()分析問題,寫出偽代碼()線程實現(xiàn)()進程實現(xiàn)二、 實驗目的和要求用Linux是PC機上執(zhí)行的類似UNIX的操作系統(tǒng),是個完1991s開發(fā)了這個操作系統(tǒng)LinusminixLinux互斥原,進程之間的沖突,引入了進程同步。會解除阻塞狀態(tài)。互斥解決問題:通過設置一個表示資源個數(shù)的信號量 ,通過對信號量 的 和 操來實現(xiàn)進程的的互斥。通過設置一個表示資源個數(shù)的信號量 ,通過對信號量 的 和 操來實現(xiàn)進程的的互斥。和 操作分別來自荷蘭語 和 分別表示占有和放。 操作是操作系統(tǒng)的原語,意味著具有原子性。是否小于 如果小于 則阻塞,如果大于 則占有資源進行執(zhí)行。操作是和 的進程減少了 個。然后檢測 是否小于 ,如果小于 則等待使用資源的進程。、 環(huán)境配置安裝() 下載系統(tǒng)鏡像,() 制作啟動 盤,使用 軟件將系統(tǒng)鏡像寫入 盤() 開機進入 界面,從 盤啟動。() 對磁盤分區(qū),等待安裝結束。() 設置 密碼, 命令后輸入兩遍密碼。熟悉 的令:)) ) )安裝 編輯器行 – 即可。安裝結束后輸入 顯示如圖就說明安裝成功:源碼安裝

Figure1安裝vim后輸入vim顯示的結果因為 內(nèi)置的版本為 所以為了程序設方便用版本。輸入 即可進入 交互模式。:Figure2內(nèi)置python運行結果– 解壓到當解壓驗證系統(tǒng)配置,編譯和建立軟鏈接在 生成 的軟鏈接文結果:四、 設計思

Figure3python3安裝結果題分析:、 、 、 是四個線程或程。盤子 是它們共享的變量對盤子的操作要互斥和 要對 同和 要對 同。2. 偽代碼fahe:while(True):p(empty)putapplev(apple)mother:while(True):p(empty)p(mutex)putorangev(mutex)v(orange)son:while(True):p(orange)p(mutex)getorangev(mutex)v(empty)daughter:while(True):p(apple)getapple、 代碼實現(xiàn). 線程實現(xiàn)原理模塊提供了對線程的操作。創(chuàng)建線程 是要運行的函數(shù), 是函數(shù)所需的參數(shù)。創(chuàng)建信號量 是信號量的初始值。信號量 ,當為 時,阻塞當前線程。 信號量,大于,喚醒等待此信號量的一個線程。創(chuàng)建互斥鎖, 加鎖, 解鎖。變量定義 類型的 存放所有的蘋果定義類型的 存放所有的橘子定義類型的當做水果盤,可以放入蘋果和橘子定義方法用于將蘋果放入水果盤,也就是把的一個蘋果,放入列表中。并打印相關信息。定義方法用于將蘋果放入水果盤,也就是把的一個橘子,放入列表中。并打印相關信息。定義方法用于從水果盤取出一個橘子,也就是把的一個橘子,拿出來,并打印相關信息。定義方法用于從水果盤取出一個蘋果,也就是把的一個蘋果,拿出來,并打印相關信息。代碼importrandomimportthreadingimporttimeempty=threading.Semaphore(5) #盤apple=threading.Semaphore(0) #orange=threading.Semaphore(0) #mutex=threading.Event() #進程互斥訪問盤子platelist() #擬盤lock=threading.Lock() #platemutex.set() #設為deffather(basket):globalempty,mutex,lock,applewhilelen(basket0: #籃則終1.p(empty)empty.acquire() #empty1#2.p(mutex)mutex.clear() #mutex設為False線#3.putappleiflock.acquire():temp=basket.pop()plate.append(temp) #籃一個放盤

rtn)oprint(' currentplate=',plate)lock.release()#4.v(mutex)) #x設線apple.release()time.sleep(random.random())defmother(basket):globalempty,mutex,lock,orangewhilelen(basket0: #橘籃橘則終1.p(empty)empty.acquire() #empty1#2.p(mutex)mutex.clear() #mutex設為False線3.put(orange)iflock.acquire():temp=plate.append(temp)rtn)plate.'.format(temp))print(' currentplate=',plate)lock.release()#4.v(mutex)) #x設線orange.release()time.sleep(random.random())defson(count):globalempty,mutex,lock,orangeforiinrange(count):#1.p(orange)orange.acquire() #orange#2.p(mutex)mutex.clear() mutex設False線將等待#3.getorangeiflock.acquire():forfruitinplate:iffruit.startswith('Orange'):temp=fruitplate.remove(fruit)breaknen)plate.'.format(temp))print(' currentplate=',plate)lock.release()#4.v(mutex)) #x設線empty.release() #將empty1time.sleep(random.random())defdaughter(count):globalempty,mutex,lock,appleforiinrange(count):#1.p(apple)apple.acquire() #apple#2.p(mutex)mutex.clear() mutex設False線將等待#3.getappleiflock.acquire():forfruitiniffruit.startswith('Apple'):temp=fruitplate.remove(fruit)breakren)plate'.format(temp))print(' currentplate=',plate)lock.release()#4.v(mutex)) #x設線empty.release() #empty+1if name ==' main ':#apples_basket=['Apple-A','Apple-B','Apple-C',"Apple-D"] #擬籃4要放入水盤oranges_basket['Orange-A''Orange-B''Orange-C'] #擬籃3要放入水盤#創(chuàng)4線程father=threading.Thread(name='Father',target=father,args=(apples_basket father線程mother=threading.Thread(name='Mother',target=mother,args=(oranges_basket mother線程son=threading.Thread(name='Son',target=son,args=(len(oranges_basket) son線程daughter=threading.Thread(name='Daughter',target=daughter,args=(len(apples_basket) daughter線程#啟動線daughter.start()son.start()father.start()mother.start()Figure4實現(xiàn)結果. 。創(chuàng)建程 是要運行函數(shù), 是函數(shù)所需參數(shù)。創(chuàng)建信號量 是信號量的初始值。 信號量 為 信號量 ,大于,喚醒等待此信號量一個。創(chuàng)建互斥鎖, 加鎖,解鎖。的 的 的法 入。并打印相關信息。法 入。并打印相關信息。從取出拿出來并打印相關信息。法 從取出把的拿出來并打印相關信息。代碼importrandomimportmultiprocessingimporttimeempty=multiprocessing.Semaphore(5) #盤apple=multiprocessing.Semaphore(0) #orange=multiprocessing.Semaphore(0) #mutexmultiprocessing.Event() #個進程互斥訪問盤子managermultiprocessing.Manager()platemanager.list() #模擬水盤lockmultiprocessing.Lock() plate互斥鎖#設置globallock,applewhilelen(basket0: #當籃中沒有,則終止#1.p(empty)#將emptymutex設置False其它線程將等待

#3.putappleiflock.acquire():temp=plate.append(temp) #籃一個放rtn)oprint(' currentplate=',plate)lock.release()#4.v(mutex)mutex.set() mutex設置,其它線#5.v(apple)apple.release()time.sleep(random.random())defmother(basket):globalempty,mutex,lock,orangewhilelen(basket0: #當橘籃中沒有橘,則終止#1.p(empty)empty.acquire() #empty2.p(mutex)mutex.clear() mutex設置False其它進程將等待#3.put(orange)iftemp=plate.append(temp)rtn)plate.'.format(temp))print(' currentplate=',plate)lock.release()#4.v(mutex)mutex.set() mutex設置,其它進#5.v(orange)orange.release()time.sleep(random.random())defson(count):globalempty,mutex,lock,orangeforiinrange(count):#1.p(orange)orange.acquire() #orange#2.p(mutex)mutex.clear() #mutex設為False進#3.getorangeiflock.acquire():forfruitinplate:iffruit.startswith('Orange'):temp=fruitplate.remove(fruit)breaknen)plate.'.format(temp))print(' currentplate=',plate)lock.release()#4.v(mutex)) #x設進empty.release() #empty+1time.sleep(random.random())defdaughter(count):globalempty,mutex,lock,appleforiinrange(count):#1.p(apple)apple.acquire() #apple#2.p(mutex)mutex.clear() #mutex設為False進#3.getappleiflock.acquire():forfruitinplate:iffruit.startswith('Apple'):temp=fruitplate.remove(fruit)breakren)plate'.format(temp))print(' currentplate=',plate)lock.release()#4.v(mutex)) #x設進empty.release() #empty+1if name ==' main ':#apples_basket=['Apple-A','Apple-B','Apple-C',"Apple-D"] #擬籃4要放入水盤oranges_basket['Orange-A''Orange-B''Orange-C'] #擬籃3要放入水盤#創(chuàng)4進程r=u,args=(apples_basket father進程r=u,args=(oranges_basket mother進程n=u,args=(len(oranges_basket) #son進程ur=uutarget=daughterargs=(len(apples_basket) daughter進程#啟動進程daughter.start()son.st

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論