2023年操作系統(tǒng)實(shí)驗(yàn)報(bào)告理解Linux下進(jìn)程和線程的創(chuàng)建并發(fā)執(zhí)行過程_第1頁
2023年操作系統(tǒng)實(shí)驗(yàn)報(bào)告理解Linux下進(jìn)程和線程的創(chuàng)建并發(fā)執(zhí)行過程_第2頁
2023年操作系統(tǒng)實(shí)驗(yàn)報(bào)告理解Linux下進(jìn)程和線程的創(chuàng)建并發(fā)執(zhí)行過程_第3頁
2023年操作系統(tǒng)實(shí)驗(yàn)報(bào)告理解Linux下進(jìn)程和線程的創(chuàng)建并發(fā)執(zhí)行過程_第4頁
2023年操作系統(tǒng)實(shí)驗(yàn)報(bào)告理解Linux下進(jìn)程和線程的創(chuàng)建并發(fā)執(zhí)行過程_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

操作系統(tǒng)上機(jī)實(shí)驗(yàn)報(bào)告實(shí)驗(yàn)名稱:進(jìn)程和線程實(shí)驗(yàn)?zāi)康模豪斫鈛nix/Linux下進(jìn)程和線程的創(chuàng)建、并發(fā)執(zhí)行過程。實(shí)驗(yàn)內(nèi)容:.進(jìn)程的創(chuàng)建.多線程應(yīng)用實(shí)驗(yàn)環(huán)節(jié)及分析:一、進(jìn)程的創(chuàng)建下面這個(gè)C程序展示了UNIX系統(tǒng)中父進(jìn)程創(chuàng)建子進(jìn)程及各自分開活動的情況。fork()創(chuàng)建一個(gè)新進(jìn)程。系統(tǒng)調(diào)用格式:pid=fork()參數(shù)定義:ntfork()fork()返回值意義如下:0:在子進(jìn)程中,pid變量保存的fork()返回值為0,表達(dá)當(dāng)前進(jìn)程是子進(jìn)程。>0:在父進(jìn)程中,pid變量保存的f。rk()返回值為子進(jìn)程的id值(進(jìn)程唯一標(biāo)記符)。T:創(chuàng)建失敗。假如「ork()調(diào)用成功,它向父進(jìn)程返回子進(jìn)程的PID,并向子進(jìn)程返回0,即fork()被調(diào)用了一次,但返回了兩次。此時(shí)OS在內(nèi)存中建立一個(gè)新進(jìn)程,所建的新進(jìn)程是調(diào)用fork()父進(jìn)程(parentprocess)的副本,稱為子進(jìn)程(childprocess)o子進(jìn)程繼承了父進(jìn)程的許多特性,并具有與父進(jìn)程完全相同的用戶級上下文。父進(jìn)程與子進(jìn)程并發(fā)執(zhí)行。2、參考程序代碼/*process.c*/#inc1ude<stdio.h>ttinclude<sys/types.h>main(intargc,char*argv[])(intpid;/*forkanotherprocess*/pid=fork();if(pid<0){/*erroroccurred*/fprintf(stderr,"ForkFailed");exit(-1);)elseif(pid==0){/*childprocess*/execlp(zz/bin/1s〃,"Is”,NULL);)e1se{/*parentprocess*//parentwillwaitforthechi1dtocomp1ete*/wait(NULL);printf("ChildComplete");

exit(0);3、編譯和運(yùn)營$gccprocess.c-oprocesss4、運(yùn)營$./processexit(0);編輯如圖所示:xubii磔hen^@localhosl~文件⑹編料且查看《山終端0?轉(zhuǎn)到(@幫助(旦)#include<stdio.h>#include<sys/types.h>rmin(intargc.char*argy)intpid;pid=fork();if(pid<0){fprintf(sderr,"ForkFailed*):exit(-l);}eIseif(pid==0){execlp(w/bin/Is*,'1s'.NLLL);}else{uait(NLLL);printf(*QiiIdConpIete*);exit(O)0}1運(yùn)營如圖所示:viprogress.cgccprogress.c-oprocesss./process./processs[xubingsheng^locaIhostxubingsheng]$[xubingsheng^'1ocaIhostxubingsheng]$[xubingsheng^>locaIhostxubingsheng]$bash:./process:沒仃那個(gè)文件或目錄:[xubingsheng^locaIhostxubingsheng]$viprogress.cgccprogress.c-oprocesss./process./processsprocesssprogress.cChiIdConpIete[xubingsheng^locaIhostxubingsheng]$I思考:(1)系統(tǒng)是如何創(chuàng)建進(jìn)程的?1,申請空白PCB(進(jìn)程控制塊);2,為新進(jìn)程分派資源;3,初始化PCB;4,將新進(jìn)程插入就緒隊(duì)列;⑵擴(kuò)展程序,在父進(jìn)程中輸出1至I]5,在子進(jìn)程中輸出6T0,規(guī)定父子進(jìn)程并發(fā)輸出;記錄實(shí)驗(yàn)結(jié)果,并給出簡樸分析。實(shí)驗(yàn)結(jié)果如圖:二、多線程應(yīng)用編寫unix/Linux下的多線程程序,需要使用頭文獻(xiàn)pthread,h,連接時(shí)需要使用庫libpthread.ao下面是一個(gè)最簡樸的多線程程序examp1e1.Co下面的示例中,要使用至兩個(gè)函數(shù),pthread_createpthread_join,并聲明了一個(gè)pthread_t型的變量。函數(shù)pthread_create用來創(chuàng)建一個(gè)線程,它的原型為:externintpthread_createP((pthread_t*_thread,constpthread_attr_t*_attr,void*(*start_routine)(void*),void*arg));第一個(gè)參數(shù)為指向線程標(biāo)記符的指針,第二個(gè)參數(shù)用來設(shè)立線程屬性,第三個(gè)參數(shù)是線程運(yùn)營函數(shù)的起始地址,最后一個(gè)參數(shù)是運(yùn)營函數(shù)的參數(shù)。這里,我們的函數(shù)thread不需要參數(shù),所以最后一個(gè)參數(shù)設(shè)為空指針。第二個(gè)參數(shù)我們也設(shè)為空指針,這樣將生成默認(rèn)屬性的線程。當(dāng)創(chuàng)建線程成功時(shí),函數(shù)返回0,若不為0則說明創(chuàng)建線程失敗,常見的錯(cuò)誤返回代碼為EAGAIN和EINVALo前者表達(dá)系統(tǒng)限制創(chuàng)建新的線程,例如線程數(shù)目過多了;后者表達(dá)第二個(gè)參數(shù)代表的線程屬性值非法。創(chuàng)建線程成功后,新創(chuàng)建的線程則運(yùn)營參數(shù)三和參數(shù)四擬定的函數(shù),本來的線程則繼續(xù)運(yùn)營下一行代碼。函數(shù)pthread.join用來等待一個(gè)線程的結(jié)束。函數(shù)原型為:externintpthread_joinP((pthread_t_th,void**threadreturn));第一個(gè)參數(shù)為被等待的線程標(biāo)記符,第二個(gè)參數(shù)為一個(gè)用戶定義的指針,它可以用來存儲被等待線程的返回值。這個(gè)函數(shù)是一個(gè)線程阻塞的函數(shù),調(diào)用它的函數(shù)將一直等待到被等待的線程結(jié)束為止,當(dāng)函數(shù)返回時(shí),被等待線程的資源被收回。一個(gè)線程的結(jié)束有兩種途徑,一種是象我們上面的例子同樣,函數(shù)結(jié)束了,調(diào)用它的線程也就結(jié)束了;另一種方式是通過函數(shù)Pthread_exit來實(shí)現(xiàn)。它的函數(shù)原型為:externvoidpthreadexitP((void*retval))—attribute((noreturn));唯一的參數(shù)是函數(shù)的返回代碼,只要pthread_join中的第二個(gè)參數(shù)threadreturn不是NULL,這個(gè)值將被傳遞給threadreturn。2、參考程序代碼/*thread.c*/inc1ude<stdio.h>include<pthread.h>voidthrcad(void)(inti;for(i=0;i<3;i++)printf("Thisisapthread.\n");)intmain(intargc,char*argv[])|pthread_tid;inti,ret;ret=plhread_create(&id,NULL,(void*)thread,NULL);if(ret!=0){printf("Crealepthreaderror!\n");exit(1);)for(i=0;i<3;i++)printf("ThisisthemainprocessAn");pthreadjoin(id,NULL);retum(0);)3、編譯和運(yùn)營編譯此程序:

gccexamplel.c-Ipthread-oexample11pthread:使用線程庫運(yùn)營examplel,得到如下結(jié)果:Thisisthemainprocess.Thisisapthread.Thisisthemainprocess.Thisisthemainprocess.Thisisapthread.Thisisapthread.再次運(yùn)營,也許得到如下結(jié)果:ThisisapthreadaThisisthemainprocess.^ThisisapthreadaThisisthemainprocessqThisisapthread.Thisisthcmainprocess.編輯過程如圖所示:文件⑹編輯?查看出終端⑴轉(zhuǎn)到(9幫助(H)#include<stdio.h>#include<pthread.h>voidthread(void)]inti:for(i=0;i<3;i++)printf(*Tliisisapthread.\n');Iintmiin(intargc,char*argv[]){pthread_tid;inti,ret;ret=pthread_create(Aid,NILL.(void*〉thread,MLL);if(ret!=O){printf('Createpthreaderror!\n");exit(1):Ifor(i=0;i<3;i++)printf('Thisisthenninprocess.\n');pthread_join(id.NLLL);thread.c>[(2轉(zhuǎn)換]22Lthread.c>[(2轉(zhuǎn)換]22L,373c執(zhí)行如圖所示:thread.c:13:id'undeclared(firstuseinthisfunction)[xubingsheng^locaIhostxubingsheng]$vithread.c[xubingsheng@locaIhostxubingsheng]$gccthread.c-Ipthread-othreadthread.c:Infunctionmiin':thread.c:11:pthread_tid'undeclared(firstuseinthisfunction)thread.c:11:(Eachundeclaredidentifierisreportedonlyoncethread.c:11:foreachfunctionitappearsin.)thread.c:13:id'undeclared(firstuseinthisfunction)[xubingsheng^locaIhostxubingsheng]$vithread.c[xubingsheng@locaIhostxubingsheng]$gccthread.c-Ipthread-othread[xubingsheng^locaIhostxubingsheng]$./threadThTllThThThThThTllThThThThThTllThThThThisisapthread.ThTllThThThThisisapthread.isisapthread.isistherminprocess.isisthenninprocess.isistherminprocess.實(shí)驗(yàn)總結(jié):在實(shí)驗(yàn)中很多粗心導(dǎo)致的問題,比如指令輸錯(cuò)字母,代碼寫錯(cuò)字母,沒有注意是否需要空格等。通過課堂的理論知識學(xué)習(xí)和實(shí)驗(yàn)課的上機(jī)實(shí)驗(yàn),讓我更能理解操作系統(tǒng)的知識。4、思考⑴程序運(yùn)營后,進(jìn)程thread中有幾個(gè)線程存在?3個(gè)(2)為什么前后兩次運(yùn)營結(jié)果不同樣?單核的cpu在解決多線程時(shí)每次只能執(zhí)行一跳指令,也就是說無論你的程序有多少個(gè)線程,每一時(shí)刻執(zhí)行的也只是一個(gè)線程里的代碼,cpu會輪流給每個(gè)線程分派時(shí)間片,時(shí)間

溫馨提示

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

評論

0/150

提交評論