實驗5:進(jìn)程管理及操作_第1頁
實驗5:進(jìn)程管理及操作_第2頁
實驗5:進(jìn)程管理及操作_第3頁
實驗5:進(jìn)程管理及操作_第4頁
實驗5:進(jìn)程管理及操作_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、實驗5進(jìn)程管理及操作實驗?zāi)康?. 了解進(jìn)程號,進(jìn)程組號,會話號。2. 清楚進(jìn)程兩個牛成子進(jìn)程兩數(shù)z間的差別實驗內(nèi)容a)修改程序getpgid_example.c,使結(jié)果沒有父進(jìn)程為1的情況。并說明原因b)有3個進(jìn)程,其中一個為父進(jìn)程,其余為父進(jìn)程的子進(jìn)程,分別打印這三個進(jìn)程的 進(jìn)程號,父進(jìn)程號,進(jìn)程組號。c)有3個進(jìn)程,其中一個為父進(jìn)程,其余為父進(jìn)程的子進(jìn)程,其中一個子進(jìn)程運(yùn)行“is 1“指令,另一個子進(jìn)程在暫停5s后界常退出,父進(jìn)程不阻塞自己,并等待子進(jìn)程 的退出信息,待收到該信息,父進(jìn)程返回。d)請修改提供的源碼,實現(xiàn)簡單的myshell,實驗結(jié)果實驗總結(jié)# in elude< st

2、dio.h># in elude< stdlib.h># in elude< unistd.h># in eludev string.h># in elude< sys/types.h># in eludev dire nt.h>#define normal 0 廣一般的命令*/# define out_redirect 1 /* 輸出重定向*7 #define inedirect 2 /* 輸入重定向*/ #define have_pipe 3 /* 命令屮有管道*/void print_prompt();/* 打印提示符*/void g

3、etnput(char *);廠得到輸入的命令*7void explain_input(char * ,int * ,char a 100 256);/* 對輸入命令進(jìn)行解析* /void do_cmd(int ,char a100256);/* 執(zhí)行命令*/int find_command(char *);廠查找命令屮的可執(zhí)行程序tint main(int argc,char * * argv)int i;int argcount = 0;char arglist100256;char * * arg = null;char * but = null;but = (char * )mallo

4、c(256);if (buf = null)perror("malloc failed");exit(-1);while(1)廠將buf所指向的空間清零*/memset(buf,0,256);print_prompt();get_input(buf);廣若輸入的命令為exit或logout則退出本程序* /if (strcmp(buf,"exitn") = = 0 11 strcmp(buf,"logoutn") = = 0)break;for (i= 0;i< 1 00;i+ + )arglistio = ,0,;argco

5、imt = 0;explai n_ input(buf5&argco un t,arglist); do_cmd(argco un t,arglist);廣將buf所指向的內(nèi)存釋放* /if (but != null)free(buf);but = null;exit(o);void print_prompt()printf(”myshell$”);廠獲取用戶輸入*/void get_input(char * but) int len 二 0;int ch;ch = getchar();while (len <256 && ch != 'n')bu

6、flen+ + = ch;ch = getchar();if (len = = 256)printf("commond is too long n”);exit(-1);廠輸入的命令過長則退岀程序*/buflen = 叭len+ + ;buflen = ao1;廣解析buf中的命令,將結(jié)果存入arglist屮,命令以回車符號n結(jié)朿*/* 如輸入命令為“is -i /tmp”,則 arglist 0 , arglist 1 , arglist 2分別為 ls,l 和/t mp*/void explain_input(char * buf,int * argcount,char argl

7、ist100256) char * p = but;char * q = but;int number = 0;while(1)if (p0 = 'njbreak;if (p0 = - )p+ +;else q = p;number = 0;while (q0 != 1 ') && (q0 != 'nj) number+ +;q+ + ;/str ncpy( arg listargco unt ,p,number+ 1);arglist* argcountnumber = '0*;* argcount = * argcount + 1;p = q

8、; void do_cmd (int argcount,char arglist 100256)int flag = 0;int how = 0;廠用于指示命令屮是否含冇int background = 0;/*標(biāo)識命令屮是否冇后臺運(yùn)彳亍標(biāo)識符* /int status;int i;int fd;char *argargcount+ 1;char * argnextargcount+1 ;char * file;pid_t pid;/*將命令取出*/for (i=0;i<argcount;i+ + )argi = (char * )arglisti;argargco unt = null

9、;/*查看命令行是否有后臺運(yùn)行符rfor (i=0;i<argcount;i+ + )if (strncmp(argij'&”,1) = 0) if (i = argcount - 1)background = 1; argargcount-1 = null;break;else printf(hwrong commandrtj;return;for (i=0;argi != null;i+ + ) if (strcmp(argi,">") = = 0) flag+ +;how = out_redirect;if (argi+1 = null)f

10、lag+ +;if (strcmp(argi,"<") = = 0) flag+ +;how = in_ redirect;if (i = 0)flag+ +;if (strcmp(argi;t') = 0) flag+ +;how = have_pipe;if (argi+1 = null)flag+ +;if (i = 0)flag+ +;/*flag大于1,說明命令中含有多個>,v,|符號,本程序是不支持這樣的命令的, 或者命令格式不對,如“is -i /tmp >h*/if (flag > 1)printf("wrong c

11、ommandn");return;if (how = outedirect) /*命令只含有一個輸出重定向符號>"7for (i=0;argi != null;i+ + )if (strcmp(argi,">") = = 0)file = argi+ 1 ;argi = null;if (how = = in_redirect) /*命令只含有一個輸入重定向符號v*/ for (i=0;argi != null;i+ + )if (strcmp(argi,<") = 0)file = argi+ 1;argi = null;

12、if (how = = have_pipe) /*命令只含有一個管道符號/*把管道符號后面的部分存入argnext中,管道后面的部分是一個可執(zhí)行的shell命令*/for (i=0;argi != null;i+ + )if (strcmp(argi;t,) = 0)argi = null;int j;for (j=i+1;argj != null;j+ + )argnextj-i-1 =argj;argn extj-i-1 = argj;break;if (pid = fork() < 0) printf(hfork errornh); return;switch(how) case

13、0:/*pid為0說明是子進(jìn)程,在子進(jìn)程中執(zhí)行輸入的命令”/ 廠輸入的命令屮不含>,v和|*/if (pid = 0)if (!(find_command(arg0)printf("%s:command not foundn",arg0);exit(o);execvp(argo,arg);exit(o);break;case 1:if (pid = 0)if (!(find_command(arg0)printf("%s: comm and not foundn",arg0);exit(o);fd = open(file,o_rdwr| o_cre

14、at| o_trunc,0644); execvp(argo,arg);exit(o);break;case 2:廣輸入的命令中含有重定向符v */if (pid = 0)if (!(find_command(arg0)printf("%s: comm and not foundn",arg0);exit(o);dup2(fd,0);execvp(argo,arg);exit(o);break;case 3:/ *輸入的命令中含有管道* /if (pid = 0)int pid2;int status2;int fd2;if (pid2 = fork() < 0)pr

15、intf("fork2 errorn");return;else if (pid2 = = 0)if (!(find_command(arg0)printf("%s: comm and not foundnh,arg0);exit(o);fd2 = open(7tmp/youdonotknowfileh,o_wronly| o_creat| o_trunc,0644); dup2(fd2,1);execvp(argo,arg);exit(o);if (waitpid(pid2,&status2,0) = = -1)printf("wait for

16、 child process errorn");if (!(find_command(argnext0)printf("%s: comm and not foun dn",argnexto);exit(o);fd2 = open(,7tmp/youdonotknowfile,o_rdonly);dup2(fd2,0);execvp(arg next0 ,arg next);if (remove("/tmp/youdonotknowfile")printf("remove errorn");exit(o);break;defa

17、ult:break;廠若命令屮有&,表示后臺執(zhí)行,父進(jìn)程直接返回,不等待子進(jìn)程結(jié)束*/if (background = = 1) printf("process id %dn'*,pid); return;廠父進(jìn)程等待子進(jìn)程結(jié)束rif (waitpid(pid,&status,o) = = -1)printf("wait for child process errorrt);int find_command(char * command)dir *dp;struct dirent * dirp;char *path =“/bin”,”/usr/bin”,null;廣使當(dāng)前目錄下的程序可以運(yùn)行,如命令”./fork”可以被正確解釋和執(zhí)行*/if (strncmp(command,"./',2) = = 0) command = comma

溫馨提示

  • 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)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論