山东大学linux实验四CSDN,山东大学操作系统实验报告材料4进程同步实验
《山東大學(xué)操作系統(tǒng)實(shí)驗(yàn)報(bào)告材料4進(jìn)程同步實(shí)驗(yàn)》由會(huì)員分享,可在線閱讀,更多相關(guān)《山東大學(xué)操作系統(tǒng)實(shí)驗(yàn)報(bào)告材料4進(jìn)程同步實(shí)驗(yàn)(15頁珍藏版)》請(qǐng)?jiān)谌巳宋膸炀W(wǎng)上搜索。
1、實(shí)用標(biāo)準(zhǔn)文案計(jì)算機(jī)科學(xué)與技術(shù)學(xué)院實(shí)驗(yàn)報(bào)告實(shí)驗(yàn)題目:實(shí)驗(yàn)四、進(jìn)程同步實(shí)驗(yàn)學(xué)號(hào):日期:20120409班級(jí):計(jì)基地12姓名:實(shí)驗(yàn)?zāi)康?#xff1a;加深對(duì)并發(fā)協(xié)作進(jìn)程同步與互斥概念的理解,觀察和體驗(yàn)并發(fā)進(jìn)程同步與互斥 操作的效果,分析與研究經(jīng)典進(jìn)程同步與互斥問題的實(shí)際解決方案。了解Lin ux系統(tǒng)中IPC進(jìn)程同步工具的用法,練習(xí)并發(fā)協(xié)作進(jìn)程的同步與互斥操作的編程 與調(diào)試技術(shù)。實(shí)驗(yàn)內(nèi)容:抽煙者問題。假設(shè)一個(gè)系統(tǒng)中有三個(gè)抽煙者進(jìn)程,每個(gè)抽煙者不斷地卷煙并抽煙。 抽煙者卷起并抽掉一顆煙需要有三種材料: 煙草、紙和膠水。一個(gè)抽煙者有煙草, 一個(gè)有紙,另一個(gè)有膠水。系統(tǒng)中還有兩個(gè)供應(yīng)者進(jìn)程,它們無限地供應(yīng)所有三 種材料,。
2、但每次僅輪流提供三種材料中的兩種。得到缺失的兩種材料的抽煙者在 卷起并抽掉一顆煙后會(huì)發(fā)信號(hào)通知供應(yīng)者,讓它繼續(xù)提供另外的兩種材料。這一 過程重復(fù)進(jìn)行。請(qǐng)用以上介紹的IPC同步機(jī)制編程,實(shí)現(xiàn)該問題要求的功能。硬件環(huán)境:處理器:In tel? Core? i3-2350M CPU 2.30GHzx 4圖形:In tel? Sa ndybridge Mobile x86/MMX/SSE2內(nèi)存:4G操作系統(tǒng):32位磁盤:20.1 GB軟件環(huán)境:ubu ntu13.04實(shí)驗(yàn)步驟:(1) 新建定義了 producer和consumer共用的IPC函數(shù)原型和變量的ipc.h文件。(2) 新建ipc.c文件,。
3、編寫producer 和consumer 共用的IPC的具體相應(yīng)函數(shù)。(3) 新建Producer文件,首先定義producer的一些行為,利用系統(tǒng)調(diào)用,建立共享內(nèi)存區(qū)域,設(shè)定其長(zhǎng)度并獲取共享內(nèi)存的首地址。然后設(shè)定生產(chǎn)者互斥與同步的信號(hào)燈,并為他 們?cè)O(shè)置相應(yīng)的初值。當(dāng)有生產(chǎn)者進(jìn)程在運(yùn)行而其他生產(chǎn)者請(qǐng)求時(shí),相應(yīng)的信號(hào)燈就會(huì)阻止他,當(dāng)共享內(nèi)存區(qū)域已滿時(shí),信號(hào)等也會(huì)提示生產(chǎn)者不能再往共享內(nèi)存中放入內(nèi)容。(4) 新建Consumer文件,定義consumer的一些行為,利用系統(tǒng)調(diào)用來創(chuàng)建共享內(nèi)存 區(qū)域,并設(shè)定他的長(zhǎng)度并獲取共享內(nèi)存的首地址。然后設(shè)定消費(fèi)者互斥與同步的信號(hào)燈,并 為他們?cè)O(shè)置相應(yīng)的初值。當(dāng)。
4、有消費(fèi)進(jìn)程在運(yùn)行而其他消費(fèi)者請(qǐng)求時(shí),相應(yīng)的信號(hào)燈就會(huì)阻止它,當(dāng)共享內(nèi)存區(qū)域已空時(shí),信號(hào)等也會(huì)提示生產(chǎn)者不能再從共享內(nèi)存中取出相應(yīng)的內(nèi)容。運(yùn)行的消費(fèi)者應(yīng)該與相應(yīng)的生產(chǎn)者對(duì)應(yīng)起來,只有這樣運(yùn)行結(jié)果才會(huì)正確。結(jié)論分析與體會(huì):實(shí)現(xiàn)方式:Consumer :#i nclude ipc.hint main (i nt argc,char *argv) int rate = 3;int con sumerid=atoi(argv1);buff_h = 101;buff_ nu mber = 1;cget_h = 103;cget_ nu mber = 1;shm flg = IPC CREAT | 0644。
5、;buff_ptr = (char *)set_shm(buff_h,buff_ nu mber,shm_flg); cget_ptr = (int *)set_shm(cget_h,cget_ nu mber,shm_flg); prod_h = 201;pmtx_h = 202;cons_h = 301;cmtx_h = 302;sem_flg = IPC_CREAT | 0644;sem_val = buff_ nu mber;prod_sem = set_sem(prod_h,sem_val,sem_flg);sem_val = 0;con s_sem = set_sem(co ns_。
6、h,sem_val,sem_flg);sem_val = 1;cmtx_sem = set_sem(cmtx_h,sem_val,sem_flg);if(con sumerid=0)*cget_ptr=0;while(1)if(buff_ptr0-A=c on sumerid)dow n(co ns_sem);dow n(cmtx_sem);sleep(rate);if(buff_ptrO=A)printf(%dThe consumer has glue.nThe consumer getstobacco and paper n,getpid();if(buff_ptr0=B)prin tf(。
7、%d The con sumer has paper. nThe con sumer getstobacco and gluen,getpid();if(buff_ptrO=C)printf(%dThe consumerhas tobacco.nTheconsumergets glue and paper n,getpid();*cget ptr 二(*cget pt葉1);if(*cget_ptr%2=0) buff_ptrO=D; else buff_ptrO=E; up(cmtx_sem); up(prod_sem);return EXIT_SUCCESS;Producer :#i nc。
8、lude ipc.hint main (i nt argc,char *argv)int rate=3;int producerid=atoi(argv1);buff_h=101;buff_ nu mber=1;pput_h=102;pput_ nu mber=1;shm_flg=IPC_CREAT|0644;buff_ptr = (char *)set_shm(buff_h,buff_ nu mber,shm_flg); pput_ptr = (int *)set_shm(pput_h,pput_ nu mber,shm_flg);prod_h = 201;pmtx_h = 202;cons。
9、_h = 301;cmtx_h = 302;sem flg = IPC CREAT|0644;sem_val = buff_ nu mber;prod_sem = set_sem(prod_h,sem_val,sem_flg);sem_val = 0;con s_sem = set_sem(co ns_h,sem_val,sem_flg);sem_val = 1;pmtx_sem = set_sem(pmtx_h,sem_val,sem_flg);if(producerid=0)buff_ptrO=D;*pput_ptr=0;while(1)if(buff_ptr0-D=producerid)。
10、dow n( prod_sem);dow n(pmtx_sem);*pput_ptr = (*pput_pt r+1)%3;if(*pput_ptr=0)buff_ptr0 = A;prin tf(%dThe producer gives tobacco andpapern,getpid();if(*pput_ptr=1)buff_ptr0 = B;prin tf(%dThe producer gives tobacco andgluen,getpid();if(*pput_ptr=2)buff_ptr0 = C;prin tf(%d The producer gives glue and p。
11、aper n,getpid();sleep(rate);up(pmtx_sem);up(c on s_sem);return EXIT_SUCCESS;Ipc.h :#i nclude ipc.hint get_ipc_id(char *proc_file,h_t h) FILE *pf;int m,n;char lin eBUFSZ,columBUFSZ;if(pf = fope n(proc_file,r) = NULL) perror(卩roc file not ope n); exit(EXIT_FAILURE);fgets(line, BUFSZ,pf);while(!feof(pf。
12、)m = n = 0;fgets(line, BUFSZ,pf);while(l in em=)m+;while(linem !=) colu mn+ = lin em+;colum n = 0; if(atoi(colum) != h) con ti nue;n=0;while(l in em=)m+;while(linem !=) colu mn+ = lin em+;colum n = 0;m = atoi(colum);fclose(pf);return m; fclose(pf); return -1;int dow n(i nt sem_id) struct sembuf buf;。
13、buf.sem_op = -1; buf.sem_ nu mber = 0; buf.sem_flg = SEM_UNDO; if(semop(semd,&buf,1) d has priper, tobacco and has paper. tobacco and has paper. tobacco and hmm paper. tobacco and has paper + mbacco 2nd has paper, tobacco rd has paper. tobacco dilueglueueglueglueglumgluegLuegl lieglueglue0Q Ukunliku。
14、nMjenovoddeaPad-Y471A: /lab4ltkimer has tobacco.The consuner gets glut dnd paper3319 rhe consuner has Tobacco.glue and paper h日乞 tobacco.glue and paper has tobacco- glue and paper has tobacco.glue and ppor has tobacco.glue and paper has tobaccoglue and paper hmm tobacco.glue and paper has tobacco.gl。
15、ue and paper haw tobacco glue and paper has tobacco.glue /nd paperThe consumer gets 3319 The cansuner The conLjnter gets J319 The consuner The consumer sets 3319 Thp consurner The consumer gets J319 The consunerThe conurrr gets 1519 The consularThe consumer gets3319 The consumerhe consjrr gets 3319 。
16、The consunerThe conuiner gets 3319 The consumerThe consumer gets 3.319 Tht? consumeiThe consumer get呂分析:多進(jìn)程的系統(tǒng)中避免不了進(jìn)程間的相互關(guān)系。進(jìn)程互斥是進(jìn)程之間發(fā)生的一 種間接性作用,一般是程序不希望的。通常的情況是兩個(gè)或兩個(gè)以上的進(jìn)程需要 同時(shí)訪問某個(gè)共享變量。我們一般將發(fā)生能夠問共享變量的程序段稱為臨界區(qū)。 兩個(gè)進(jìn)程不能同時(shí)進(jìn)入臨界區(qū),否則就會(huì)導(dǎo)致數(shù)據(jù)的不一致,產(chǎn)生與時(shí)間有關(guān)的 錯(cuò)誤。解決互斥問題應(yīng)該滿足互斥和公平兩個(gè)原則,即任意時(shí)刻只能允許一個(gè)進(jìn) 程處于同一共享變量的臨界區(qū),而且不能。
17、讓任一進(jìn)程無限期地等待。進(jìn)程同步是進(jìn)程之間直接的相互作用,是合作進(jìn)程間有意識(shí)的行為,典型的 例子是公共汽車上司機(jī)與售票員的合作。只有當(dāng)售票員關(guān)門之后司機(jī)才能啟動(dòng)車 輛,只有司機(jī)停車之后售票員才能開車門。 司機(jī)和售票員的行動(dòng)需要一定的協(xié)調(diào) 同樣地,兩個(gè)進(jìn)程之間有時(shí)也有這樣的依賴關(guān)系,因此我們也要有一定的同步機(jī) 制保證它們的執(zhí)行次序。 信號(hào)量機(jī)制就是其中的一種。信號(hào)燈機(jī)制即利用pv操作來對(duì)信號(hào)量進(jìn)行處理。PV操作由P操作原語和V 操作原語組成(原語是不可中斷的過程),對(duì)信號(hào)量進(jìn)行操作,具體定義如下:P( S):將信號(hào)量S的值減1,即S=S-1 ;如果S30,則該進(jìn)程繼續(xù)執(zhí)行;否則該進(jìn)程置為等待狀態(tài)。
18、,排 入等待隊(duì)列。V( S):將信號(hào)量S的值加1,即S=S+1 ;如果S0,則該進(jìn)程繼續(xù)執(zhí)行;否則釋放隊(duì)列中第一個(gè)等待信號(hào)量的進(jìn)程。PV操作的意義:我們用信號(hào)量及 PV操作來實(shí)現(xiàn)進(jìn)程的同步和互斥。信號(hào)量的數(shù)據(jù)結(jié)構(gòu)為一個(gè)值和一個(gè)指針,指針指向等待該信號(hào)量的下一個(gè)進(jìn)程。信號(hào)量的值與相應(yīng)資源的使用情況有關(guān)。當(dāng)它的值大于0時(shí),表示當(dāng)前可用資源的數(shù)量;當(dāng)它的值小于0時(shí),其絕對(duì)值表示等待使用該資源的進(jìn)程個(gè)數(shù)。信 號(hào)量的值僅能由PV操作來改變。一般來說,信號(hào)量 S30時(shí),S表示可用資源的 數(shù)量。執(zhí)行一次P操作意味著請(qǐng)求分配一個(gè)單位資源,因此 S的值減1 ;當(dāng)S。(3) sleep()等linux系統(tǒng)調(diào)用缺少頭文件。 解決方法:加頭文件#includevunistd.h體會(huì)和收獲:經(jīng)過本次實(shí)驗(yàn),初步了解操作系統(tǒng)的進(jìn)程同步的過程。我對(duì)生產(chǎn)者-消費(fèi)者問題的解決辦法有了更全面的認(rèn)識(shí),同時(shí)對(duì)調(diào)試代碼更加熟練。本次實(shí)驗(yàn)最大的 體會(huì)就是,做東西要細(xì)心,在寫代碼的過程中,稍不留意就給后期調(diào)試工作帶來 很多問題。
總結(jié)
以上是生活随笔為你收集整理的山东大学linux实验四CSDN,山东大学操作系统实验报告材料4进程同步实验的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux怎么打开q7后缀的文件,ZQ7
- 下一篇: 四代内存PK大比拼:超频之王究竟是谁?