




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、操作系統實驗報告實驗名稱:并發進程實現文件謄抄計算機科學與技術學院目錄一、實驗目的和要求2二、實驗內容2三、實驗步驟2四、實驗結果與分析34.1、小文件的拷貝34.2、大文件的拷w44.3、拷w結果檢測5五、程序源代碼65.1. 并發進程65.2. 順序進程9六、實驗體會12一、實驗目的和要求1)掌握linux操作系統的進程創建和終止操作;2)利川linux操作系統提供的信號量工具實現進程間的同步;3)掌握對共亨內存的相關操作;4)基于生產者消費者同步的原理實現雙緩沖區的謄抄。二、實驗內容任意選擇一個文本文件,采川并發進程實現文件謄抄,將其復制為另一個文件。創建get、copy、put三個進程
2、共用兩個緩沖區s、t。get進程負貴不斷地把原始文件內容送入緩沖區s屮,copy進程負責從緩沖區s屮取出上述內容復制到緩沖區t屮,而put進 程負責把上述內容從緩沖區t屮取出制到h標文件屮,用p、v操作實現這三個進程之間 的同步。三、實驗步驟1、并發進程:1)、新建一個文本文件get.txt,并在文件屮寫入10kb的數據;2)、在終端屮編譯寫好的并發進程程序文件co_pro_copy.c,輸入指令:gcc co_pro_copy.c -o co_pro_copy ;3)、編譯成功后,運行生成的可執行文件co_pro_copy: ./co_pro_copy ;4)、查看程序運行的時間,并且觀察是
3、否生成了備份文件put.txt;2、順序進程:1)、在終端中編譯寫好的并發進程程序文件pro_copy.c,輸入指令:gcc pro_copy.c -o pro_copy ;2)、編譯成功后,運行生成的可執行文件pro_copy: ./pro_copy :3)、查看程序運行的時間,并且觀察是否生成了備份文件put.txt;3、比較并發進程和順序進程的運行時間,比較兩種方式的運行速度快慢;4、將get.txt文件屮的數據加人,使get.txt文件大小達到1ookb,在來比較兩種方式的運行 速度的快慢。、實驗結果與分析4.1、小文件的拷貝如下圖圖1所示,待拷貝的小文件get. txt的大小為3.
4、1kb。filesaeltxt囲t* itool - aedil© hit get.txt propertiesbasic permissions open with蠡i!"name:type:plain text document (text/plain)size:3.1 kb (3,148 bytes)location:/home/taosir/desktopvolume:unknownaccessed:五,12月 202013 21:44:19modified:五,12 月 202013 21:44:19.6圖1小文件大小如下圖圖2所示,并發進程的文件拷貝的運行時間為
5、1.000736s,我在程序中設置三個 子進程都執行完時才結朿程序,也就是設置了三個子進程都同時完成的機制。只要其屮有個 子進程沒執行完,它就會等待is。順序進程的文件拷貝運行時間為1.001154s,也是這種等待機制實現所有子進程同時完 成。從屮可以看出順序進程與并發進程的時間差不多,并發進程的時間稍微比順序進程短一 點。ermin file edit view search terminal helpi opensave | s | undotaoslr(0>ubuntu: /desktoptaosirubuntu:/desktop$ gcc co_pro_copy.c o co_p
6、ro_copy taosirubuntu:/desktop$ gcc pro copy.c 一o pro copy taosirubuntu:/desktop$ ./co_pro_copyrun time: 0.008754waitrun time: 1.000736 taosirubuntu:/desktop$ run time: 0.009643 waitrun time: 1.001154 taosirgubuntu : -*/desktop$/pro_copy圖2小文件的運行時間4.2、大文件的拷貝如下圖圖3所示,待拷貝的大文件get. txt的大小為31. 5kb。basic per
7、missions open withname:type:size:isplain text document (text/plain) 31.5 kb (31,471 bytes)et.txtlocation: /home/taosir/desktop volume:unknownaccessed:五.12月 20 2013 21:45:19 modified:五,12月 20 2013 21:45:19e3 held圖3大文件的大小如下圖圖4所示,并發進程的時間為1.006561s,順序進程的運行時間為1.011219s,和小文件的運行吋間比較可以看到。小文件吋并發進程的運行速度就比順序進程
8、快1.71倍, 大文件時并發進程的運行速度就比順序進程快1.56倍。陰 opensaveet.txt xtaosir(q)ubuntu: “/desktoptaosirqubuntu:/desktop$ ./co_pro_copyrun time: 0.063687waitrun time: 1.006561taosirubuntu:/desktop$ ./pro_copyrun time: 0.083517waitrun time: 1.011219taosirubuntu:/desktop$ |14大文件的運行時間4.3、拷貝結果檢測從下圖圖5中可以看到待拷w文件get.txt的ia)容和
9、拷w文件put.txt的內容一致,說明 文件拷貝功能實現。5拷貝結果五、程序源代并發進程源代碼:木氺木氺木氺木木木木木木氺木氺木木木木氺木氺木氺本氺木氺木木氺木木木木木氺本氺*file_name: co_pro_copy.c *author:*date: 20lvl?/22*function:并發進程實現文件的謄抄木氺木氺氺木木木氺木氺木氺本氺木氺氺木氺木氺木木木氺木木木木木木氺木氺木氺木氺#include<stdio.h>#include<stdlib.h>#include<string.h>#include<sys/types.h>#incl
10、ude<sys/sem.h>#include<sys/shm.h>#include<sys/ipc.h>#include<sys/time.h>#include<unistd.h>const size t cache length = 1024;/采用信號燈機制實現p、v操作的數據結構和蚋數union semun int val;struct semid_ds *buf; unsigned short *array; struct seminfo *_buf;int p(int semid, int semnum) struct se
11、mbuf sops = semnum, -1, sem_undo; return (semop(semid, &sops, 1);int v(int semid, int semnum) struct sembuf sops = semnum, +1, semjjndo; return (semop(semid, &sops, 1);int main()/定義共享緩沖區,獲得一個共享內存的標志,新建內存為1kbint readbuf_id=shmget(231,cache_length,ipc_creat|0666); int op_id=shmget(232/5,ipc_cr
12、eat|0666); int writebufjd=shmget(233,cache_lengthjpc_creat|0666); int get_id ;int putjd;int copy_id;file *fin,*fout; union semun semopts; int semid; char *s,*op,*t;float exe一time; struct timeval start; struct timeval _end; getti meofday (&_startz n u ll);/創建一個信號集,其中有4個信號量 semid=semget(200,4jpc_c
13、reat |0666);/信號燈賦值semopts.val=l;semctl(semid,0,setval,semopts);semopts.val=0;semctl(semid,l,setval,semopts);semopts.val=l;semctl(semid,2,setval,semopts);semopts.val=0;semctl(semid,3,setval,semopts);/連接共享a存,獲得共享n存的地址,|aj存為可讀可寫t=(char*)shmat(writebuf_idznull,shm_r| shm_w); s=(char*)shmat(readbuf_id,nu
14、ll,shm_r| shm_w); op=(char*)shmat(opjd,null/shm_r| shm_w);/get_porcessif(get_id=fork()=0)if(fin=fopen(,'./get.txt";'rb,')=null) puts("can't find get.txt file!"); exit(-l); while(!feof(fin)p(semid'o);if(fgets(s,500,fin)=null)/單次讀入 50byte *op='g'/get_end_flag
15、v(semid,l);break;else printf("get:%s",s);v(semid,l);fclose(fin); return 0;/copy_porcessif(copy_id=fork()=0)while p(semid,l);p(semid,2);if(*op='g')*op='c'/copy_end_flagv(semid,o);v(semid,3); return 0;strcpy(t,s);v(semid,0);v(semid,3);/put_porcessif(putd=fork()=0)if(fout=fope
16、n(,./put.txt',;,wb")=null)puts("can't open the file put.txt.");exit(-l); while p(semid,3);jf(*op=,c,)v(semid,2);break;fputs(t,fout);printf("put:%s",t>v(semid,2);*op='p'/put_end_flagfclose(fout);/保證三個進程同步結束的機制,當put進程沒結束時,其他進程等待iswhile(*op!='p') slee
17、p ; puts("sleep");/釋放緩沖區shmctl(readbuf_id,ipc_rmid,o);shmctl(op_id,ipc_rmid,0);shmctl(writebufjd,ipc_rmid,o);/釋放信號燈semctl(semjd,0,ipc_rmid,co; semctl(semid,l,ipc_rmid,o); semctl(semid,2,ipc_rmid刈; semctl(semid,3,ipc_rmid,0);gettimeofday(&_end,null);exe_time = (float)(_end.tv_sec - _sta
18、rt.tv_sec)*1000000+(float)(_end.tv_usec_start.tv_usec)/1000000;printf("run time: %.6fn"/exe_time); return 0;順序進程源代碼:/本氺*氺本木本木木本本本*本木本木氺本氺本木本氺*氺本木本木本木本木木本氺本氺*file_name: pro_copy.cauthor:王濤 u201114445 計科 11 級 10 班*date: 2013/12/22function:順序進程實現文件的謄抄木*本木*木*木*木*木本木木*#include<stdio.h>#in
19、clude<stdlib.h>#include<string.h>#include<sys/types.h>#include<sys/sem.h>#include<sys/shm.h>#include<sys/ipc.h>#include<time.h#include<unistd.h>#include<sys/time.h>const size t cache length = 1024;/采用信號燈機制實現p、v操作的數據結構和函數union semun int val;struct se
20、mid_ds *buf; unsigned short *array; struct seminfo *_buf;int p(int semid, int semnum) struct sembuf sops = semnum, -1, sem_undo; return (semop(semid, &sops, 1);int v(int semid, int semnum) struct sembuf sops = semnum, +1, sem_undo; return (semop(semid, &sops, 1);int main()/定義共亨緩沖區,獲得一個共亨內存的標
21、志,新建內存為1kbint readbufjd=shmget(231/cache_length,ipc_creat|0666);intop_id=shmget(232/5,ipc_creat|0666);int writebufjd=shmget(233zcache_length,ipc_creat|0666);int get_id; int putjd; int copy_id;file *fin,*fout; union semun semopts; int semid; char *s,*op,*t; float exetime;struct timeval _start; struct
22、 timeval _end;gettimeofd ay(&_sta rt, n u ll);/創建一個信號集,其-屮有4個信號量 semid=semget(200z3jpc_creat|0666);/信號燈賦值semopts.val=0;semctl(semid,0,setval,semopts);semopts.val=0;semctl(semid,l,setval,semopts);semopts.val=l;semctl(semid,2,setval,semopts);/連接共享內存,獲得共享內存的地址,內存為可讀可寫t=(char*)shmat(writebuf_id,null
23、,shm_r| shm_w); s=(char*)shmat(readbuf_idznull,shm_r| shm_w); op=(char*)shmat(op_id,null,shm_r| shm_w);/get_porcessif(get_id=fork()=0)if(fin=fopen("./get.txt";,rb,)=null) puts("can't find get.txt file!"); exit(-l); while(!feof(fin)p(semid,2);jf(fgets(s,500,fin)=null)/單次讀入 50b
24、yte *op='g'/get_end_flagv(semid,0);break;/else printf("get:%s",s);v(semid,o);fclose(fin); return 0;/copy_porcessif(copy_id=fork()=0) while p(semid,0);if(*op=,g,)*op='c'/copy_end_flagv(semid,l);return 0;strcpy(t,s);v(semidzl);/put_porcessif(put_id=fork()=0)if(fout=fopen("./put.txt"/"wb")=null)puts("can't open the file put.txt.");exit(-l> while p(semid,l);if(*op='c')v(semid,2);break;fputs(t,fout);/printf(,put:%s,t);v
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025如何強化合同監管功能促進企業信用體系建設
- 《2025年個人租賃企業汽車合同》
- 2025投資者應警惕合同中的隱含風險
- 2024年復合管道項目資金申請報告代可行性研究報告
- 2025臨時勞動合同模板
- 2025景觀設計與施工承包合同
- 2025全面汽車租賃合同范本
- 2025房屋租賃拆遷合同模板
- 2025年履行合同勞動的基本原則
- 2025的勞動合同范本
- GB/T 32249-2015鋁及鋁合金模鍛件、自由鍛件和軋制環形鍛件通用技術條件
- GB/T 25179-2010生活垃圾填埋場穩定化場地利用技術要求
- GB/T 18705-2002裝飾用焊接不銹鋼管
- GB/T 12706.2-2020額定電壓1 kV(Um=1.2 kV)到35 kV(Um=40.5 kV)擠包絕緣電力電纜及附件第2部分:額定電壓6 kV(Um=7.2 kV)到30 kV(Um=36 kV)電纜
- GB 4351.1-2005手提式滅火器第1部分:性能和結構要求
- GA/T 850-2009城市道路路內停車泊位設置規范
- 顯微鏡檢驗報告
- 辦公室設備設施清單
- 畢業設計(論文)-霧炮除塵系統的設計
- 異常子宮出血診斷與治療指南解讀課件
- 機器學習之聚類分析課件
評論
0/150
提交評論