操作系统实验报告-shell

上传人:博****1 文档编号:428516651 上传时间:2023-05-26 格式:DOC 页数:14 大小:79KB
返回 下载 相关 举报
操作系统实验报告-shell_第1页
第1页 / 共14页
操作系统实验报告-shell_第2页
第2页 / 共14页
操作系统实验报告-shell_第3页
第3页 / 共14页
操作系统实验报告-shell_第4页
第4页 / 共14页
操作系统实验报告-shell_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《操作系统实验报告-shell》由会员分享,可在线阅读,更多相关《操作系统实验报告-shell(14页珍藏版)》请在金锄头文库上搜索。

1、操作系统实习报告 日期: 年 月 日 实习题目: shell程序完成人:姓名:组号:学号实习内容简要 述本实验要实现一个简单的命令解释器,也就是Linux中的shell程序。实验程序起名为ysh,要求其设计类似于目前流行的shell解释程序,如bash、csh、tcsh,但不需要具备那么复杂的功能。ysh程序应当具有如下一些重要的特征:l 能够执行外部程序命令,命令可以带参数。l 能够执行fg、bg、cd、history、exit等内部命令。l 使用管道和输入输出重定向。l 支持前后台作业,提供作业控制功能,包括打印作业的清单,改变当前运行作业的前台/后台状态,以及控制作业的挂起、中止和继续运

2、行。 除此之外,在这个实验中还须做到:l 使用make工具建立工程。l 使用调试器gdb来调试程序。提供清晰、详细的设计文档和解决方案主要代码结构(附注释)#include #include #include #include #include #include #include #include #include #include #include #include ysh.h#define NO_PIPE -1#define FD_READ 0#define FD_WRITE 1int is_founded(char * cmd) int k = 0; while(envpathk !=

3、NULL) strcpy(buf,envpathk); strcat(buf,cmd); if(access(buf,F_OK) = 0) return 1; k+; return 0;void getenviron(int n,char * s) int i = 0,j = 0,k = 0; char c; char buff80; char * p; while(c=si) != =) buffi+ = c; buffi+ = 0; if(strcmp(buff,PATH) = 0) while(si != 0) if(si = :) buffj+ = /; buffj = 0; p =

4、(char *)malloc(strlen(buff) + 1); strcpy(p,buff); envpathk+ = p; envpathk = NULL; j = 0; i+; else buffj = si; j+; i+; else fprintf(stderr,No match);int getline(int fd,char * buf) int i = 0; char c; while(read(fd,& c,1) bufi+ = c; if(c = n) bufi-1 = 0; return i; return i;void init_environ() int fd,n;

5、 char buf80; if(fd = open(ysh_profile,O_RDONLY,660) = -1) printf(init environ variable error!n); exit(1); while(n = getline(fd,buf) != 0) getenviron(n,buf); envhis.start = 0; envhis.end = 0; head = end = NULL;int pipel(char * input,int len) char * argv1030; char * filename0; int i,j,k,is_bg = 0; int

6、 li_cmd = 0; int fd101,pipe_in = -1; int pipe_out = -1,flag = 0; pid_t pid; for(i = 0,j = 0,k = 0;i ) | (inputi = n) if(inputi = |) | (inputi = )if(inputi = ) flag =1;if(j 0 ) bufj+ = 0; argvli_cmdk = (char *)malloc(sizeof(char) * j); strcpy(argvli_cmdk,buf); k+;argvli_cmdk = (char *)0; li_cmd+; k =

7、 0;j = 0; if(j = 0)continue; elsebufj+ = 0;if(flag = 0) argvli_cmdk = (char *)malloc(sizeof(char) * j); strcpy(argvli_cmdk,buf); k+; else filename0 = (char *)malloc(sizeof(char) * j); strcpy(filename0,buf); j = 0;else if(inputi = &) & (inputi = 0) is_bg = 1;continue; bufj+ = inputi; argvli_cmdk+ = N

8、ULL; for(i = 0;i = 10;i+) fdiFD_READ = NO_PIPE;fdiFD_WRITE = NO_PIPE; for(i = 0;i li_cmd;i+) if(pipe(fdi) = -1) printf(Can not open pipe!n); return 0; for(i = 0;i li_cmd;i+) if(is_founded(argvi0) = 0) printf(Can not found command!n); break; if(i != 0) pipe_in = fdi - 1FD_READ; else pipe_in = NO_PIPE

9、; if(i != li_cmd) pipe_out = fdiFD_WRITE; else if(flag = 1) if(pipe_out = open(filename0,O_WRONLY | O_CREAT | O_TRUNC,S_IRUSR | S_IWUSR) = -1) printf(Can not open %sn,filename0);return 0; else pipe_out = NO_PIPE; if(pid = fork() 0) printf(Fork failed!n); return 0; if(pid = 0) if(pipe_in = NO_PIPE) c

10、lose(pipe_in);if(pipe_out = NO_PIPE) close(pipe_out);if(pipe_out != NO_PIPE) dup2(pipe_out,1); close(pipe_out); if(pipe_in != NO_PIPE) dup2(pipe_in,0); close(pipe_in);execv(buf,argvi); else if(is_bg = 0) waitpid(pid,NULL,0);close(pipe_in);close(pipe_out); return 0;void add_history(char * inputcmd) envhis.end = (envhis.end + 1) % HISNUM; if(envhis.end = envhis.start) envhis.start = (envhis.start + 1) % HISNUM; strcpy(envhis.his_cmdenvhis.end,inputcmd);void h

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 幼儿/小学教育 > 小学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号