《系统软件课程设计-操作系统》实验报告.doc_第1页
《系统软件课程设计-操作系统》实验报告.doc_第2页
《系统软件课程设计-操作系统》实验报告.doc_第3页
《系统软件课程设计-操作系统》实验报告.doc_第4页
《系统软件课程设计-操作系统》实验报告.doc_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

系统软件课程设计实验报告专业 计算机科学与技术 姓名 胡伟强 班级 计121 学号 10123522 教师 叶 琪 华东理工大学信息科学与工程学院2015年6 月20 日一.课程设计题目Shell编程二.课程设计成员胡伟强 计121 10123522三.课程设计内容和要求学习如何编写一个Linux的外壳Shell,即命令解释程序。学习怎样接受命令、解释命令、执行命令,特别是采用创建子进程的方式来执行一个程序,以及父进程如何继续子进程的工作。要求:1、编写一个C语言程序作为Linux内核的shell命令行解释程序,所执行的结果需和系统命令行方式保持一致。2、对用户编写的shell增加后台运行功能。即用户可以使用”&”作为一个命令结束,以启动下一个命令。3、修改程序,增加I/O重定向功能。即用户可以使用”和”|”符号改变程序/文件的输入和输出。四. 软件系统设计terminal_prompt()函数read_command()函数Parsing()函数main()函数execute()函数调用builtin_command()函数child_process_handler()函数parse_info_init()函数1、函数调用图:2、 函数功能说明:函数名功能说明void execute(void)执行函数,程序的主体Void child_process_handler(int sig)处理后台运行进程函数,用于清除后台运行的子进程,以防其变成僵尸进程void terminal_prompt(char*)终端提示符函数int read_command(char *,char *,char*)读取命令函数,从用户输入中读取命令和参数,分别放入command和parameters中,作为exec族函数执行。int parsing(char *,int,struct parse_info *)语法分析函数,主要处理后台执行,重定向,管道功能实现int parse_info_init(struct parse_info *info)初始化结构体struct parse_info *infoint builtin_command(char *command, char *parameters)内建命令函数,实现了exit、cd、help命令5. 算法设计1. 程序框架#define TRUE 1while(TRUE) type_prompt();read_command(command,parameters); if(fork()!=0) /* Parent code */waitpid(-1,&status,0); else /* Child code */execve(command,parameters,0);2.程序主体功能execute()函数的实现#include myshell.hvoid execute(void) int status,i; char *command = NULL;/命令 char *parameters;/参数 int ParaNum;/参数个数 char promptMAX_PROMPT;/提示 struct parse_info info; pid_t ChdPid,ChdPid2; parameters = malloc(sizeof(char *)*(MAXARG+2); buffer = malloc(sizeof(char) * MAXLINE); if(parameters = NULL | buffer = NULL)/内存分配失败 printf(mysh error:malloc failed.n); return; if(signal(SIGCHLD,child_process_handler) = SIG_ERR)/父进程调用将SIGCHLD绑定到sig_handler()函数,这样fork出来的子进程在终止时会自动清理掉 perror(signal() error); while(TRUE) int pipe_fd2,in_fd,out_fd; terminal_prompt(prompt);/终端提示符函数 ParaNum = read_command(&command,parameters,prompt);/读入命令 if(-1 = ParaNum) continue; ParaNum-;/命令数减一 parsing(parameters,ParaNum,&info);/对于后台执行、管道、重定向的初始化操作 if(builtin_command(command,parameters)/内建命令 continue; if(info.flag & IS_PIPED) /管道 if(pipe(pipe_fd)0)/pipe_fd0:读管道;pipe_fd1:写管道 printf(mysh error:pipe failed.n); exit(0); if(ChdPid = fork()!=0) /mysh主进程 if(info.flag & IS_PIPED) if(ChdPid2=fork() = 0) /要求管道进程必须为mysh进程的子进程 close(pipe_fd1); close(fileno(stdin); dup2(pipe_fd0, fileno(stdin); close(pipe_fd0); if(execvp(mand2,info.parameters2)=-1)printf(%sn,strerror(errno); else close(pipe_fd0); close(pipe_fd1); waitpid(ChdPid2,&status,0); /wait command2 if(info.flag & BACKGROUND)/后台执行 printf(后台进程pid:%un,ChdPid); int i; for(i=0;iMAXPIDTABLE;i+) if(child_process_pidi=0) child_process_pidi = ChdPid; /存储子进程号break; if(i=MAXPIDTABLE) printf(Too much background processesnThere will be zombine process); else waitpid(ChdPid,&status,0);/等待命令1结束 else /mysh的子进程 if(info.flag & IS_PIPED) /命令2不为空 if(!(info.flag & OUT_REDIRECT) / 仅管道 close(pipe_fd0); close(fileno(stdout); dup2(pipe_fd1, fileno(stdout); close(pipe_fd1); else /输出重定向和管道 close(pipe_fd0); close(pipe_fd1);/关闭管道,发送一个EOF信号给command2 if(info.flag & OUT_REDIRECT) out_fd = open(info.out_file, O_WRONLY|O_CREAT|O_TRUNC, 0666); else out_fd = open(info.out_file, O_WRONLY|O_APPEND|O_TRUNC, 0666); close(fileno(stdout); dup2(out_fd, fileno(stdout); close(out_fd); else if(info.flag & OUT_REDIRECT) / 仅输出重定向 out_fd = open(info.out_file, O_WRONLY|O_CREAT|O_TRUNC, 0666); close(fileno(stdout); dup2(out_fd, fileno(stdout); close(out_fd); if(info.flag & IN_REDIRECT)/输入重定向 in_fd = open(info.in_file, O_CREAT |O_RDONLY, 0666);/0666表示, 拥有者,组,其他人都拥有可读可写权限 close(fileno(stdin); dup2(in_fd, fileno(stdin); close(in_fd); if(execvp(command,parameters)=-1)printf(%sn,strerror(errno); free(parameters);/释放parameters free(buffer);/释放buffer6. 上机实现情况及运行结果(包括中间和最终结果)1、编译mysh,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论