


版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、實驗三:生產(chǎn)者與消費者實驗目的1. 學習和掌握操作系統(tǒng)中進程之間的通信;2. 理解和掌握使用信號量機制來是想進程之間的同步和互斥;3. 學習使用創(chuàng)建文件對象,并利用文件映射對象來實現(xiàn)數(shù)據(jù)通信。二、實驗容10? 一個大小為6的緩沖區(qū),初始為空,每個緩沖區(qū)能存放一個長度若為 個字符的字符串。? 2個生產(chǎn)者-隨機等待一段時間,往緩沖區(qū)添加數(shù)據(jù),-若緩沖區(qū)已滿,等待消費者取走數(shù)據(jù)后再添加 -重復12次? 3個消費者-隨機等待一段時間,從緩沖區(qū)讀取數(shù)據(jù)-若緩沖區(qū)為空,等待生產(chǎn)者添加數(shù)據(jù)后再讀取 -重復8次說明:?顯示每次添加和讀取數(shù)據(jù)的時間及緩沖區(qū)的狀態(tài)?生產(chǎn)者和消費者用進程模擬,緩沖區(qū)用共享存來實現(xiàn)三
2、、實驗環(huán)境1. Windows下:Win dows8 ,Visual studio 20132. Linux 下:Linux Ubuntu 4,gcc四、程序設計與實現(xiàn)1. Win dows 下:A.主要函數(shù)說明:(1) PROCESSNF ORMATIONStartClo ne(i ntn Clo nelD) 功能:用來創(chuàng)建5個相同的進程,前兩個為生產(chǎn)者,后 三兩個為消費者,賦予其不同的ID值,返回進程的信 息。(2)CreateSemaphore();功能:創(chuàng)建3個信號量:full ,empty,mutex。來互斥 的訪問緩沖區(qū),實現(xiàn)通信。(3)CreateFileM appi ng ()
3、功能:在當前運行的進程中創(chuàng)建文件映射對象,來模擬共享緩沖區(qū)MapViewOfFile ()功能:在此文件映射上創(chuàng)建視圖映射到當前應用程序的 地址空間B 程序流程圖開始初始化:創(chuàng)建的子進程個數(shù)為 0nClo ne=0;申請緩沖區(qū)P(empty)P(mutex)0n Clo ne32n Clo ne6申請緩沖區(qū)把產(chǎn)品放入緩沖 區(qū):將0置為1釋放緩沖區(qū):V(mutex)V(full)隨機等待Y12次放完了?nClone=?P(full)P(mutex)創(chuàng)建3個信號量:full ,empty,mutex1創(chuàng)建共享內(nèi)存1T等待子進程執(zhí)行 完畢1It釋放緩沖區(qū):V(mutex)V(empty)1隨機等待N
4、從緩沖區(qū)取產(chǎn) 品:將1置為0Y12次取完了?結(jié)束每個子進程結(jié)束實驗代碼如下:Win dows:/實驗三.cpp :定義控制臺應用程序的入口點創(chuàng)建5個相同nClon e+司的子進程/#i nclude stdafx.h #i nclude #in elude #in elude static HANDLEMutexMappi ng=INVALID HANDLE VAL;UEint num=O;HANDLBpHa ndle10;struct bufint num;int read;int write;int buffer5;BOOIStartClo ne()int i;BOOlbCreateOK;
5、PROCESS_INFORMATpONTCHAzFilename MAX PATHGetModuleFileName(NULLszFilename, MAX PAT)HTCHARzCmdLine MAX_PATHfor ( i = 0; i 3; i+)spri ntf(szCmdL ine,%s con sumer %d ,szFile name,i);STARTUPINFO;ZeroMemory( reinterpret cast(&si), sizeof (si);si.cb=sizeof (si);bCreateOK二CreateProcess (szFile name,szCmdLi
6、 ne,NULLNULLFALSE,CREATE_DEFAULT_ERROR_MODENULLNULL&si,&pi);if (!bCreateOK)return false ;IpHa ndle num =pi.hProcess;nu m+;for ( i = 0; i 2; i+)spri ntf(szCmdL ine,%s productor %d ,szFile name,i);STARTUPINFO;ZeroMemo以 re in terpret_cast(&si), sizeof (si);si.cb= sizeof (si);bCreateOK二CreateProcess (sz
7、File name,szCmdLi ne,FALSECREATE_DEFAULT_ERROR_MODE&si,&pi);if (!bCreateOK)return falseIpHa ndle num =pi.hProcess;nu m+;return true ;void Parent()printf( Creati ng the child process and waited child process to quit.n );hMutexMapping二 CreateMutex (NULL true , mutex);HANDLEMapping二 CreateFileMapping (
8、NULLNULLPAGE_READWRITE0,sizeof (LONG map);if (hMapping!二 INVALID_HANDLE_VAL)JELPVOIDpData二MapViewOfFile(hMapp ing,FILE_MAP_ALL_ACCESS0);if (pData!二 NULLZeroMemory(pData, sizeof (LONG;struct buf *pn Data= rein terpret castvstruct buf*(pData);pn Data-read=O;pn Data-write=0;pn Data-num=0;memset(pnData-
9、buffer,O,sizeof (pnData-buffer);Unm apViewOfFile(pData);CreateSemaphore( NULL3,3, EMPTY);CreateSemaphore( NULL0,3, FULL);BOOlbCreateOK二StartClo ne();if (!bCreateOK)/pri ntf(Create child process failed. n);elsepri ntf(Create child process success. n);ReleaseMutex(hMutexMappi ng);void Productor( int n
10、)int j;printf( Productor is runnin g.n);hMutexMapping= OpenMuteX MUTEX_ALL_ACCEST8e , mutex);HANDLEMapping=OpenFileMapping (FILE MAP ALL ACCESSNULL map);if (hMapping二二 INVALID HANDLE VAL)JEprin tf(error n );HANDLEemEmpty =Ope nSemaphoreSEMAPHORE_ALL_ACCEASSEEMPTY);HANDLEemFull =Ope nSemaphoreSEMAPHO
11、RE ALL ACCEASSEFULL);for ( int i = 0; i 6; i+)WaitForSi ngleObject(semEmpty,INFINITE);SY STEMTIMSt;GetSystemTime(&st);srand(unsigned )time(0);Sleep(ra nd()/6);WaitForSi ngleObject(hMutexMappi ng,INFINITE);LPVOIDpFile二MapViewOfFile(hMapp ing,FILE_MAP_ALL_ACCESS0);if (pFile!二 NULLstruct buf *pn Data=
12、rein terpret_cast (pFile);pn Data-bufferp nData-write=1;pn Data-write=(p nData-write+1)%3;pn Data-nu m+;printf(%02d:%02d:%02d生產(chǎn)者%d生產(chǎn)成功緩沖區(qū)中剩余 %個 ,st.wHour,st.wMinute,st.wSecond,n,pnData-num);for (j = 0; j bufferj);printf(n);匸Unm apViewOfFile(pFile);pFile=NULLReleaseSemaphore(semFull, 1,NULL;ReleaseMu
13、tex(hMutexMappi ng);printf(生產(chǎn)者%d生產(chǎn)完畢n , n);void Consumer( int n)int j;printf(Con sumer is runnin g.n);hMutexMapping= OpenMuteX MUTEX ALL ACCESr8e , mutex);HANDLEMapping=OpenFileMapping (FILE MAP ALL ACCESSNULLmap);if (hMapping二二 INVALID_HANDLE_VAL)JEprintf( error n );HANDLEemEmpty =Ope nSemaphoreSEM
14、APHORE ALL ACCESSSEEMPTY);HANDLEemFull =Ope nSemaphoreSEMAPHORE_ALL_ACCEASSEFULL);for ( int i = 0; i (pFile);pn Data-bufferp nData-read=O;pn Data-read=(p nData-read+1)%3;pnData-num-;printf(%02d:%02d:%02d消費者%d消費成功緩沖區(qū)中剩余 %個 ,st.wHour,st.wMinute,st.wSecond,n,pnData-num);for (j = 0; j bufferj);printf(n)
15、;Unm apViewOfFile(pFile);pFile= NULLReleaseSemaphore(semEmpty,1, NULL;ReleaseMutex(hMutexMappi ng);printf( 消費者%d消費完畢n , n);匚int main( int argc , char * argv)if ( argc1 &strcmp(argv1, productor )=0)Productor(atoi(argv 2);else if ( argc 1 &strcmp(argv 1, consumer )=0)Consumer(atoi(argv 2);elsePare nt(
16、);WaitForMultipleObjects(num,lpHandle,true , INFINITE); return 0;Linux下代碼:/主進程#in elude #i nclude #in elude head.hint mai n()創(chuàng)建信號量集合int id = semget(SEM_ALL_KEY, 3, IPC_CREAT|0660);/初始化信號量集合semctl(id, SEM_EMPTY, SETVAL, 3);semctl(id, SEM_FULL, SETVAL, 0);semctl(id, SEM_MUTEX, SETVAL, 1);int shmid = c
17、reateQueue();/ 創(chuàng)建共享主存if(shmid0)perror(create shm error.);exit(1);/生成生產(chǎn)者進程if(fork()=0)execl(producer, produced, 0);if(fork()=0)execl(producer, produced, 0);/生成消費者進程if(fork()=0)execl(c on sumer, con sumerl, 0);if(fork()=0)execl(c on sumer, con sumer2, 0);if(fork()=0)execl(c on sumer, con sumer3, 0);/等
18、待子進程int stat,i;for(i=0;i5;i+)wait(&stat);removeQueue(shmid);/ 釋放共享主存return 0;/*生產(chǎn)者*/#in clude head.hmain (i nt argc,char * argv)struct queue *buf,*out;int semd = semget(SEM_ALL_KEY,3, IPC_CREAT|0660);int i,j;buf = getQueue();/ 打開共享主存out = buf;for(i=0;ibuf+;/ 產(chǎn)品數(shù) +1(buf + buf-buf) -buf = 1; /放入產(chǎn)品后緩沖區(qū)
19、置1/獲取當前時間struct timeval curtime;gettimeofday(&curtime,NULL);/輸出信息printf(No.%s put product %ld:%ld ,argv0,curtime.tv_sec,curtime.tv_usec);prin tf(Now the buffer is );for(j=1;jbuf);prin tf(n);v(semd, SEM_MUTEX); /V(MUTEX)v(semd, SEM_FULL); /V(FULL),釋放一個 FULL,即往里面寫了一個數(shù)據(jù)/隨機sleep 一會int ran=ran dom()%5;sl
20、eep(ra n);/*消費者*/#in clude head.hmain (i nt argc,char * argv)struct queue *buf,*out;int semd = semget(SEM_ALL_KEY, 3, IPC_CREAT|0660);int i,j;buf = getQueue();打開共享主存out = buf;for(i=0;ibuf) -buf = 0; /取出產(chǎn)品后緩沖區(qū)置0buf-buf-; / 產(chǎn)品數(shù)-1/獲取當前時間struct timeval curtime;gettimeofday(&curtime,NULL);/輸出信息printf(No.
21、%s get product %ld:%ld ,argv0,curtime.tv_sec,curtime.tv_usec);prin tf(Now the buffer is );for(j=1;jbuf);prin tf(n);v(semd, SEM_MUTEX);/V(MUTEX)v(sem_id, SEM_EMPTY);/V(EMPTY)/隨機等待一段時間int ran=ran dom()%5; sleep(ra n);Windows運行截圖:reating nsutmertlie cliild process is running.and ualted clkild process t
22、o qruxtconsume i* jonsumer oductor toductar 2;-17:36 2;47;3& 2:47:41 2:47:41 2:47:1 2:47:4& 2:47:46 2:47:46 2:47:52 2:47:52 2:47:52 2:47:57 2:47:57 2:47:57 2:48:03 2:48:03 2:48:03$產(chǎn)產(chǎn)費產(chǎn)費產(chǎn)費產(chǎn)費產(chǎn)費產(chǎn)費產(chǎn)費產(chǎn)費功功功功功功功功功 成成4成磁成成磁成成成4 :產(chǎn)產(chǎn)WOP產(chǎn)WOP產(chǎn)費產(chǎn)賽產(chǎn)費產(chǎn)裁 nffng生生墾羣墾消生卑消生卑消1 i _J n n B 1 0 urun嘗穀郭者者書者者書者者書區(qū)區(qū)區(qū)區(qū)區(qū)區(qū)區(qū)區(qū)區(qū)區(qū)區(qū)
23、區(qū)區(qū)區(qū)區(qū)區(qū)區(qū)AxlxIXITTXrAXIVATXA!0 1110 0 0Linux下截圖:0111000110 0 0 11100B0111090fTiAinCDfti ufrtitrpradueercqlocalhMt文件舊 編輯舊 查看阿 機索 終箱 輕叭H)cqloc al host 下 K$ . /nain ducorl 4o .prod Mo . consiiie rl slo .consumers Un prndijrrl Jo produce r2 4q,consumerl,consumer3 4a ,producerl lo .p r uJ匚總2 lo . consn
24、總 rz Jo . consno rl Mo.p roducerl Uo JprodijGer2 Mo p roducor2. consi jfiqH g cpnsLiinml,consumer? Jo .producrl 4o.consumers Mop roduce rz lo. consume r2 ln prndurrlput pur get get put put get got put put get got pJt put put 滬t qat gmt put gst pjt get putproduct produci p roduct p roduc: p rnduc- producr p roducT produc: produc: p i uduc 1 p roduct p roduc : prcxjucl p roducT product prnduc p roducl pro
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 鄉(xiāng)鎮(zhèn)市政設施管理制度
- 食堂人員工作服管理制度
- 產(chǎn)科接種各項管理制度
- 企業(yè)基本安全管理制度
- 企業(yè)消毒衛(wèi)生管理制度
- 云南宿舍日常管理制度
- 人力服務公司管理制度
- vivo員工管理制度
- 企業(yè)人員聘用管理制度
- 企業(yè)物業(yè)完善管理制度
- 浙江省紹興市諸暨市2023-2024學年五年級下學期期末數(shù)學試卷
- 重慶市大足縣2023-2024學年四年級數(shù)學第二學期期末聯(lián)考試題含解析
- 煤礦調(diào)度智能化培訓課件
- 基于PLC的啤酒發(fā)酵自動控制系統(tǒng)
- 重慶市沙坪壩區(qū)2022-2023學年八年級下學期期末英語試題
- 思辨與創(chuàng)新智慧樹知到期末考試答案章節(jié)答案2024年復旦大學
- 手術(shù)室-標準側(cè)臥位擺放
- (正式版)JBT 9229-2024 剪叉式升降工作平臺
- 合伙人退出協(xié)議書
- (高清版)DZT 0208-2020 礦產(chǎn)地質(zhì)勘查規(guī)范 金屬砂礦類
- 2024年春江蘇開放大學先進制造技術(shù)第一次過程性考核作業(yè)答案
評論
0/150
提交評論