進(jìn)程控制和守護(hù)進(jìn)程的創(chuàng)建_第1頁(yè)
進(jìn)程控制和守護(hù)進(jìn)程的創(chuàng)建_第2頁(yè)
進(jìn)程控制和守護(hù)進(jìn)程的創(chuàng)建_第3頁(yè)
進(jìn)程控制和守護(hù)進(jìn)程的創(chuàng)建_第4頁(yè)
進(jìn)程控制和守護(hù)進(jìn)程的創(chuàng)建_第5頁(yè)
已閱讀5頁(yè),還剩10頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、深圳大學(xué)實(shí)驗(yàn)報(bào)告課程名稱:linux系統(tǒng)編程實(shí)驗(yàn)項(xiàng)目名稱學(xué)院專業(yè):軟件工程指導(dǎo)教師:報(bào)告人學(xué)號(hào):2011150026 班級(jí):一班實(shí)驗(yàn)時(shí)間:201424實(shí)驗(yàn)報(bào)告提交時(shí)間:2014/4/26教務(wù)處制一、實(shí)驗(yàn)?zāi)繕?biāo):1. 掌握fork()系統(tǒng)調(diào)用及進(jìn)程的相關(guān)概念2. 掌握wait()和waitpid()系統(tǒng)調(diào)用3. 掌握進(jìn)程組,會(huì)話進(jìn)程等概念和setsid()系統(tǒng)調(diào)用4. 掌握文件重定向的技巧5. 掌握創(chuàng)建守護(hù)進(jìn)程的步驟及其實(shí)現(xiàn)二、實(shí)驗(yàn)環(huán)境與工件1. 湖邊linux實(shí)驗(yàn)室2. fedora 13三、實(shí)驗(yàn)內(nèi)容與步驟下面的程序會(huì)用到如下程序段:從命令行獲取數(shù)字參數(shù),參考實(shí)現(xiàn)見下圖:nt naln(lnt

2、argc. char »argv ) id_t childpid 0;nt 1tn;(argc !2) fprintf(stderr t nusage : %s processes n"targv 0);匚門匚ui; 1;n«atoi(argv 1);1.編例實(shí)現(xiàn)創(chuàng)建n個(gè)子進(jìn)程pl, p2,,pn,其中,各進(jìn)程之間的關(guān)系是:p1 是調(diào)用進(jìn)程的子進(jìn)程,p(k+1)是pk的子進(jìn)程。請(qǐng)打印各進(jìn)程本身的進(jìn)程號(hào)、 父進(jìn)程號(hào),子進(jìn)程號(hào)。參考運(yùn)行結(jié)果如下。要求:(1)每個(gè)父進(jìn)程都要等待linuxyzhlocalhost $ gcc exi.c - o exilinuxyzhlo

3、calhost$ . /exi3 1:1processid:3227,parentid::3068childid::3228 1:2processid:3228.parentid::3227,childid::3229 1:3processid:3229.parentid::3228,childid::0linuxyzhlocalhost$ ./exi6i:1processid:3230.parentid:3068childid:3231i:2processid:3231,parentid:3230,childid:3232i:3processid:3232.parentid:3231,chi

4、ldid:3233i:4processid:3233,parentid:3232,childid:3234i:5processid:3234.parentid:3233,childid:3235i:6processid:3235,parentid:3234,childid:0子進(jìn)程退出后才能退出;(2) n通過命令行參數(shù)傳入;(3)附上源代碼截圖 和運(yùn)行結(jié)果截圖。(20分) linuxyzh薊ocalhost $ #include <stdio.h>#include <stdlib.h>#include <unistd.h>#inelude <sys/

5、types.h>#include <sys/wait.h>int main(int argc ,char *argv)pid_tpl;int i,n;if(argc!=2)程序名為1,參數(shù)為2fprintf(stderr,"usage:%s processes xn'argvfo);return 1;in=atoi(argv 1);/參數(shù)轉(zhuǎn)化為整數(shù)for(i=0;i<n;i+)pl=fork();if(pl>0)/ 父進(jìn)程printf("process id:%d,parent id:%d,child id:%dn",getp

6、id(),getppid(),p 1); break;1else if(pl<0)/fork 錯(cuò)誤printf("fork error!n");iif(i=n)/最后一個(gè)子進(jìn)程printf("process id:%d,parent id:%d,child 0n”,ge中id(),getppid();if(pl>0)waitpid(pl,null,o);return 0;2.編例實(shí)現(xiàn)創(chuàng)建n個(gè)子進(jìn)程pl, p2,,pn,其中,各進(jìn)程之間的關(guān)系是: p1,pn都是調(diào)用進(jìn)程的子進(jìn)程。請(qǐng)打印各進(jìn)程本身的進(jìn)程號(hào)、父進(jìn)程號(hào), 子進(jìn)程號(hào)。參考運(yùn)行結(jié)果如下。要求:(1

7、)每個(gè)父進(jìn)程都要等待子進(jìn)程退出 后才能退出;(2) n通過命令行參數(shù)傳入;(3)附上源代碼截圖和運(yùn)行結(jié)果 截圖。(20分)linuxyzhlocalhost $ gcc ex2. c o ex2linuxyzhlocalhost $ /ex2 41:4processid5453parentid:5449childid: 01:3processid:5452parentid:5449childid:01:2processid5451parentid:5449childid:01:1processid:5450parentid:5449childid: 01:0processid5449child

8、ren ids: 5450 5451 !5452 5453linuxyzhlocalhost $ ./ex2 6i:4processid5458parentid:5454childid:0i:5processid:5459parentid:5454childid:0i:3processid5457parentid:5454childid0i:6processid:5460parentid:5454childid:0i:2processid5456parentid:5454childid0i:1processid:5455parentid:5454childid:0i: o process id

9、: 5454 children ids: 5455 5456 5457 5458 5459 5460 linuxyzhlocaihost -$ |#include <stdio.h>#include <stdlib.h>#include <unistd.h> #include <sys/types.h> #include <sys/wait-h> int main(int argc,char *argv)pid.tpl;int i,n;if(argc!=2)/ 錯(cuò)誤輸出fprintf(stderr,husage :%s process

10、esnn,argv0);return 1;n = atoi(argvl 1 );/參數(shù)轉(zhuǎn)換成整數(shù)pid_t plarrn;for(i=0;i<n;i+)pl=fork();if(pl<=0) break;/子進(jìn)程或者fork錯(cuò)誤elseplarri=pl;/保存父進(jìn)程的子進(jìn)程的進(jìn)程號(hào)if(pl<0)printf(mfork errorn");exit(-l);else if(pl=o)子進(jìn)程printf("process id:%d,parent id:%d,child id:%dn ”,getpid(),getppid(),pl); elseprint#

11、"process id:%d,parent id:%d,child ld:%dn m,getpid(),getppid(),p 1);if(pl>0)for(i=0;i<n;i+) waitpid(p 1 arri,null,o);return 0;3.編例實(shí)現(xiàn)創(chuàng)建n=2d-2個(gè)子進(jìn)程pl, p2,,pn,其中,d是樹的深度且d>=l, pl和p2是調(diào)用進(jìn)程的子進(jìn)程,p3和p4是p1的子進(jìn)程,p5和p6是p2的 子進(jìn)程。也就是各進(jìn)程之間的關(guān)系是:pi是p2i+1和p2i+2的父進(jìn)程,也是 組成二叉樹的關(guān)系,如下圖所示。對(duì)于不是葉子的進(jìn)程,將其id和父進(jìn)程 id打印出

12、來。參考運(yùn)行結(jié)果如下。要求:(1)每個(gè)父進(jìn)程都要等待子進(jìn)程 退出后才能退出;(2) d通過命令行參數(shù)傳入;(3)附上源代碼截圖和運(yùn)行 結(jié)果截圖。(30分)圖 1. n=23-2=6如:$./a.out 1ianprocessno1withpid4147andppid3420$./a.out 2iamprocessno1withpid4150andppid3420iamprocessno2withpid4151andppid4150iamprocessno3withpid4152andppid4150$./a.out 3iamprocessno1withpid4158andppid3420iam

13、processno2withpid4159andppid4158iamprocessno3withpid4160andppid4158iamprocessno4withpid4161andppid4159iamprocessno6withpid4162andppid4160iamprocessno5withpid4167andppid4159iamprocessno7withpid4168andppid4160提示,本題有一定的難度,參考流程見下圖:f c>r ( 1 =0 ; 1 v depth ;!. + *)print!(hi am process no %5d with pid

14、%5d and ppid %dnh , numb, gexpid()f getppid();switch (pidl = fork()<case 0:numb =2 numb ; br<?ak ;case -1 :perror ( fork n ; exit (1): default :switch < pid2 =f ork () ) -case 0:numb =2»numb t ; breas :case 1.:perror ( n f ork ' ) ; ex 11 (1);def ault :#include <stdio.h>#incl

15、ude <stdlib.h>#include vunistd.h>#include <sys/types.h>#include <sys/wait.h>int main(int argcchar *argv) pid_tpl,p2;int num=l,n4; if(argc!=2)/ 錯(cuò)誤輸出 fprintf(stderr,"usage :%s processesn'*,argv0); return 1;n= atoi(argvl);/參數(shù)轉(zhuǎn)換成整數(shù)for(i=0;i<n;i+)printf(hi am process no :

16、%2d; with pid:%5d; ppid:%5d.nu,num,getpid(),getppid(); switch(pl=fork()case 0:num*=2;break;case -1:printfc'fork error*1);exit(-l);default:switch(p2=fork() case 0:num=num*2+l;break;case -1:printf(mfork error'1);exit(-l);default:waitpid(p 1 ,null,0);waitpid(p2,null,0);exit(l);return 0;4.請(qǐng)根據(jù)課件描

17、述的守護(hù)進(jìn)程創(chuàng)建步驟編程實(shí)現(xiàn)對(duì)守護(hù)進(jìn)程的創(chuàng)建,參照下面的 實(shí)現(xiàn),如果有興趣,可以自己添加個(gè)性化功能(自行發(fā)揮,如寫文件。等)。 請(qǐng)附上源代碼及運(yùn)行結(jié)果。(20分)運(yùn)行及測(cè)試方法如下:$./a.out$ps - axj | grep a.out1 27109 27109 27109 ? -1 ss 500 0:00 ./a.outgrep a.out參考實(shí)現(xiàn):(不一定完全正確,請(qǐng)調(diào)試)include <stdio.h>#include <unistd.h>include <sys/types.h>include <sys/stat.h>#indud

18、e <fcntl.h>intdaemon (int noctidir, int noctose)pw_t papd = fork ();f如果創(chuàng)it進(jìn)用恢畋*/if (pid < 0)<perror (“fork”);return -1;/父/if (pid != 0)exit (0);廣成為會(huì)話領(lǐng)頭進(jìn)程”p»d = setsld();if (pid < -1)perror ("setskf);return -1;/ wtfl 錄修凹飯根fl錄*/if (! nochdir)didlr cf);廠將怎準(zhǔn)需入惟血覽定向到空設(shè)備吟if (! nod

19、ose)<int fd;fd = open (7dev/nuirr o rdwr. 0);if (fd f= -1)<dup2 (fd, std1n.fileno);dup2 (fd, stdoutfileno);dup2 (fd, stderr.f1leno);if (fd > 2)close (fd);umask (0027);return 0;int main(wid)ddemon(0r0);sleep(1000);return 0;#include <stdio.h>#include <stdlib.h>#include <unistd.

20、h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>int daemon (int nochdirjnt noclose) pid_t pid;pid=fork();if(pid<0)/進(jìn)程創(chuàng)建失敗perrorcfork errorm);return -1;if(pid!=o) exit(o); 創(chuàng)建守護(hù)進(jìn)程第一步,父進(jìn)程退出,子進(jìn)程孤兒 pid=setsid();第二步,調(diào)用setsid,使得該進(jìn)程成為領(lǐng)頭進(jìn)程if(pid=-l)調(diào)用失敗perrorc'setsi

21、d error");return -1;if(!nochdir)第三步,更改工作目錄到“/”chdir(vh);if(!noclose)第四步,關(guān)閉文件描述符,并重定向標(biāo)準(zhǔn)輸入、輸出、錯(cuò)誤int fd;fd=open(vdev/nuiro_rdwr,0);if(fd!=l)dup2(fd,stdin_fileno); dup2(fd,stdout_fileno);dup2(fd,stderr_f!leno);if(fd>2) close(fd);umask(0027); 第五步,設(shè)置守護(hù)進(jìn)程的文件創(chuàng)建掩碼return 0;1int main(void)daemon(0,0);s

22、leep(looo);return 0;<t> <f> <f> <f> <f> <t> <f> <t> <t> <t> <t> <t> <t> <t> <t> <f> <f> <t> <t> <f> <f> <t> <t> <t> <t> <t> <f> <t>

23、<f> <t> <f> <f> <t> <t> <f> <t> <t> <t> <t> <f> <f> <t> <f> <t> <t> <f> <t> <t> <f> <t> <f> <f> <t> <f> <2 rj> rj> rj> rj> rj>

24、 rj、rj> r|> rj> rj> r|> rj> rj> rj> rj> rj、rj> rj> rj、rj> rj> rj> r|> rj> rj> r|> rj> rj> rj> rj rj> rjw rj> r|> rj> rj> rj、rj、rj> rj、rj> rj> rj、rj> rj、rj> rj> rj、rj> rj、rj> rj、rj> rj、rj> rj&g

25、t; rjw rj> rjw rj> rj> rj>完成以上過程并取得正確結(jié)果:90分以下為幫助各位在幫同學(xué)找錯(cuò)的過程中進(jìn)一步理解進(jìn)程控制彷“口% 彷弘弘弘弘彷弘彷弘% 弘口* rtwrtw rtw* *rt«* *rjw */附加題:針對(duì)以上題號(hào)相應(yīng)題目,以下程序都有錯(cuò),你知道錯(cuò)在哪里嗎?請(qǐng)指出出錯(cuò)的地方并說明原因。(10分)第1題:activitiesmoa 1933warn w«4print f(: f x(ml;acfivitietmm 133c y tab width 8 vinsgm w«4“ edit vltwtoo<&

26、gt; oocum«n<s m«pd op«n v afinclude <4tdi>.h> finclude ««tdlib.h> firn lode «mktd.h>lnt mint lnt orgc tchar eargv( |)lf(arqc'-)fprintf(stderrr s return :n-«tol(«rgv( j);for(i- ;y)pldaformo; ir(pi(k)pxror(<orelse if(pld> )一 wvi .1.9&

27、#171;tpld()sippid()md):l.c (/dkt)fmt edit view march tools docurntnts h«pfor(l-if (a-1)if(pid)fprintf(«tdtrr,二rc:“5“5 ¥【):return ;dtoi(argw(】);pld«for*(); lf(pld< )perror( f八 fai j.n );else if (pid» )print f( 1»%1 !' %d brek;prlntf( i-5kl i n %d mywaltpld(pldtmil< i # ):father is %di %dn .i.o«tpia()<o«tppid()fpid):. ildn-9n99otpld() .etppldo);c y tjb width 8 vln 18. col 1inswgm丄丄29(none) desktop$ ./i 41=1 if 2728.1=2 pm 2729.1=3m 2730.1=4m 2

溫馨提示

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

最新文檔

評(píng)論

0/150

提交評(píng)論