操作系统实验报告-shell(共14页).doc

上传人:飞****2 文档编号:12249111 上传时间:2022-04-24 格式:DOC 页数:14 大小:53.50KB
返回 下载 相关 举报
操作系统实验报告-shell(共14页).doc_第1页
第1页 / 共14页
操作系统实验报告-shell(共14页).doc_第2页
第2页 / 共14页
点击查看更多>>
资源描述

《操作系统实验报告-shell(共14页).doc》由会员分享,可在线阅读,更多相关《操作系统实验报告-shell(共14页).doc(14页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

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

3、(envpathk != 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+ = /; bu

4、ffj = 0; p = (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_enviro

5、n() int fd,n; 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,i

6、s_bg = 0; int 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;

7、 li_cmd+; k = 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; arg

8、vli_cmdk+ = NULL; 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

9、_in = NO_PIPE; 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

10、 = NO_PIPE) close(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

11、* 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 history_cmd() int i,j = 0; if(envhis.start = envhis.end) return; else if(envhis.start envhis.end) for(i = envhis.start + 1;i =

12、envhis.end;i+) printf(%dt%sn,j,envhis.his_cmdi); j+; else for(i = envhis.start + 1;i HISNUM;i+) printf(%dt%sn,j,envhis.his_cmdi); j+; for(i = 0;i = envhis.end;i+) printf(%dt%sn,j,envhis.his_cmdi); j+; void cd_cmd(char * route) if(route != NULL) if(chdir(route) pid,p - state,p - cmd); i+; p = p - lin

13、k;while(p != NULL); else printf(No jobs!n);void add_node(char * input_cmd,int node_pid) struct NODE * p; p = (struct NODE *)malloc(sizeof(struct NODE); p - pid = node_pid; strcpy(p - state,input_cmd); strcpy(p - state,running); p - link = NULL; if(head = NULL) head = p; end = p; else end - link = p;

14、 end = p; void del_node(int sig,siginfo_t * sip) struct NODE * q; struct NODE * p; int id; if(sig_z = 1) sig_z = 0;goto out; id = sip - si_pid; p = q = head; if(head = NULL) goto out; while(p - pid != id & p - link != NULL) p = p - link; if(p - pid != id) goto out; if(p = head) head = head - link; e

15、lse while(q - link != p) q = q - link;if(p = end) end = q; q - link = NULL;else q - link = p - link; free(p); out:return;void setflag() sig_flag = 1;void ctrl_z() struct NODE * p; int i = 1; if(pid1 = 0) goto out; if(head != NULL) p = head; while(p - pid != pid1) & (p - link != NULL) p = p - link;if

16、(p - pid = pid1) strcpy(p - state,stopped);else add_node(input,pid1); strcpy(end - state,stopped); else add_node(input,pid1); strcpy(end - state,stopped); sig_z = 1; kill(pid1,SIGSTOP); for(p = head;p - pid != pid1;p = p - link) i+; printf(%dt%st%sn,i,end - state,end - cmd); pid1 = 0;out:return;void

17、 bg_cmd(int job_num) struct NODE * p; int i = 0; p = head; for(i = 1;i link; kill(p - pid,SIGCONT); strcpy(p - state,running);void fg_cmd(int job_num) struct NODE * p; int i = 0; p = head; for(i = 1;i link; strcpy(p - state,running); strcpy(input,p - cmd); pid1 = p - pid; signal(SIGTSTP,ctrl_z); kil

18、l(p - pid,SIGCONT); waitpid(p - pid,NULL,0);int main() init_environ(); while(1) char c; char * arg20; int i = 0,j = 0,k = 0; int is_pr = 0,is_bg = 0; int input_len = 0,path; int pid = 0,status = 0; struct sigaction action;action.sa_sigaction = del_node;sigfillset(& action.sa_mask);action.sa_flags =

19、SA_SIGINFO;sigaction(SIGCHLD,& action,NULL);signal(SIGTSTP,ctrl_z); path = get_current_dir_name();printf(ysh%s ,path);while(c = getchar() = ) | (c = t) | (c = EOF) ;if(c = n) continue;while(c != n) bufinput_len+ = c; c = getchar();bufinput_len = 0; input = (char *)malloc(sizeof(char) * (input_len +

20、1);strcpy(input,buf); for(i = 0,j = 0,k = 0;i = input_len;i+) if(inputi = ) | (inputi = |) if(inputi = |) pipel(input,input_len); add_history(input); free(input);else redirect(input,input_len); add_history(input); free(input);is_pr = 1;break; if(is_pr = 1) continue;for(i = 0,j = 0,k = 0;i = input_le

21、n;i+) if(inputi = ) | (inputi = 0) if(j = 0) continue; else bufj+ = 0; argk = (char *)malloc(sizeof(char) * j); strcpy(argk+,buf); j = 0; else if(inputi = &) & (inputi + 1 = 0) is_bg = 1; continue; bufj+ = inputi; if(strcmp(arg0,exit) = 0) add_history(input); printf(Bye bye!n); free(input); break; i

22、f(strcmp(arg0,history) = 0) add_history(input); history_cmd(); free(input); continue;if(strcmp(arg0,cd) = 0) add_history(input); for(i = 3,j = 0;i = input_len;i+) bufj+ = inputi; bufj = 0; arg1 = (char *)malloc(sizeof(char) * j); strcpy(arg1,buf); cd_cmd(arg1); free(input); continue;if(strcmp(arg0,j

23、obs) = 0) add_history(input); jobs_cmd(); free(input); continue;if(strcmp(arg0,bg) = 0) add_history(input); for(i = 0;i = input_len;i+) if(inputi = %) break; i+; for(;i = input_len;i+) bufj+ = inputi; bufj = 0; arg1 = (char *)malloc(sizeof(char) * j); strcpy(arg1,buf); bg_cmd(atoi(arg1); free(input)

24、; continue;if(strcmp(arg0,fg) = 0) add_history(input); for(i = 0;i = input_len;i+) if(inputi = %) break; i+; for(;i = input_len;i+) bufj+ = inputi; bufj = 0; arg1 = (char *)malloc(sizeof(char) * j); strcpy(arg1,buf); fg_cmd(atoi(arg1); free(input); continue;if(is_pr = 0) argk = (char *)malloc(sizeof

25、(char); argk = NULL; if(is_founded(arg0) = 0) printf(This command is not founed!n);for(i = 0;i = k;i+) free(argi);continue; add_history(input);if(pid = fork() = 0) if(is_bg = 1) while(sig_flag = 0) signal(SIGUSR1,setflag);sig_flag = 0; else pid1 = pid; if(is_bg = 1) add_node(input,pid1);kill(pid,SIG

26、USR1);pid1 = 0; if(is_bg = 0)waitpid(pid,& status,0); if(is_bg = 1) sleep(1);for(i = 0;i k;i+) free(argi); free(input); return 0;结果分析(或错误原因分析)通过这几周的实习,基本上掌握了简单的Linux环境下的shell编程,了解和熟悉了gcc、gdb和make。学会熟练使用man帮助手册。通过此次的实习,在利用Linux中的一些常用命令和一些语句结构来编写shell程序过程中,感觉到了难度,程序的逻辑性不难,只是在于对Linux中的一些命令运用不熟练,总是记不清楚,以至于造成shell编程的难度,以后似乎要多多练习。专心-专注-专业

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁