版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验5进程管理及操作实验目的1. 了解进程号,进程组号,会话号。2. 清楚进程两个牛成子进程两数z间的差别实验内容a)修改程序getpgid_example.c,使结果没有父进程为1的情况。并说明原因b)有3个进程,其中一个为父进程,其余为父进程的子进程,分别打印这三个进程的 进程号,父进程号,进程组号。c)有3个进程,其中一个为父进程,其余为父进程的子进程,其中一个子进程运行“is 1“指令,另一个子进程在暂停5s后界常退出,父进程不阻塞自己,并等待子进程 的退出信息,待收到该信息,父进程返回。d)请修改提供的源码,实现简单的myshell,实验结果实验总结# 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);/* 对输入命令进行解析* /void do_cmd(int ,char a100256);/* 执行命令*/int find_command(char *);厂查找命令屮的可执行程序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所指向的内存释放* /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中的命令,将结果存入arglist屮,命令以回车符号n结朿*/* 如输入命令为“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;/*标识命令屮是否冇后台运彳亍标识符* /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、;/*查看命令行是否有后台运行符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中,管道后面的部分是一个可执行的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说明是子进程,在子进程中执行输入的命令”/ 厂输入的命令屮不含>,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;厂若命令屮有&,表示后台执行,父进程直接返回,不等待子进程结束*/if (background = = 1) printf("process id %dn'*,pid); return;厂父进程等待子进程结束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;广使当前目录下的程序可以运行,如命令”./fork”可以被正确解释和执行*/if (strncmp(command,"./',2) = = 0) command = comma
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 八年级数学下册 25.3 一次函数(第1课时)(原卷版)
- 智能养生壶2.0时代:从加热工具到健康管理中枢转型
- 药物临床试验质量管理规范(GCP)解读
- 2026年浙江单招电子商务运营策划题库含答案
- 十五五数据要素市场化:家庭服务机器人隐私保护与合规挑战
- 知乎盐选专栏创作与知识付费变现
- 数字对讲机:公共安全升级与产业数字化推动下的专业通信增长市场
- 2026年汽车4S店数字化转型与客户体验优化方案
- 2025年山东海阳职业学院单招综合素质考试模拟试卷【考试直接用】附答案详解
- 2024年河南固始职业学院高职单招职业适应性测试考试题库含答案详解【典型题】
- 2026年湖北武汉警务辅助人员招聘考试试卷-含答案解析
- 2026工会社会化工作者综合能力测试题库及答案
- (正式版)DB22∕T 2522-2016 《北方精养池塘鱼菜共生操作技术规程》
- 2026山东滨城区国有企业招聘工作人员13人笔试备考题库及答案详解
- 2026届重庆市八中中考语文模试卷含解析
- 中华财险四川分公司招聘笔试题库2026
- 2026年物业管理机器人应用创新报告
- 2026年高二数学寒假自学课(沪教版)专题02 数列难点总结(原卷版)
- 民宿入住须知与安全告知指导手册
- 电梯安全知识课件教学
- 2024年长春金融高等专科学校辅导员考试笔试真题汇编附答案
评论
0/150
提交评论