操作系統(tǒng)實驗報告-shell_第1頁
操作系統(tǒng)實驗報告-shell_第2頁
操作系統(tǒng)實驗報告-shell_第3頁
操作系統(tǒng)實驗報告-shell_第4頁
操作系統(tǒng)實驗報告-shell_第5頁
已閱讀5頁,還剩9頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、操作系統(tǒng)實習報告 日期: 年 月 日 實習題目: shell程序完成人:姓名:組號:學號實習內(nèi)容簡要 述本實驗要實現(xiàn)一個簡單的命令解釋器,也就是Linux中的shell程序。實驗程序起名為ysh,要求其設(shè)計類似于目前流行的shell解釋程序,如bash、csh、tcsh,但不需要具備那么復(fù)雜的功能。ysh程序應(yīng)當具有如下一些重要的特征:l 能夠執(zhí)行外部程序命令,命令可以帶參數(shù)。l 能夠執(zhí)行fg、bg、cd、history、exit等內(nèi)部命令。l 使用管道和輸入輸出重定向。l 支持前后臺作業(yè),提供作業(yè)控制功能,包括打印作業(yè)的清單,改變當前運行作業(yè)的前臺/后臺狀態(tài),以及控制作業(yè)的掛起、中止和繼續(xù)運

2、行。 除此之外,在這個實驗中還須做到:l 使用make工具建立工程。l 使用調(diào)試器gdb來調(diào)試程序。提供清晰、詳細的設(shè)計文檔和解決方案主要代碼結(jié)構(gòu)(附注釋)#include <stdio.h>#include <ctype.h>#include <string.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/wait.h>#include <math.

3、h>#include <signal.h>#include <stdlib.h>#include "ysh.h"#define NO_PIPE -1#define FD_READ 0#define FD_WRITE 1int is_founded(char * cmd) int k = 0; while(envpathk != NULL) strcpy(buf,envpathk); strcat(buf,cmd); if(access(buf,F_OK) = 0) return 1; k+; return 0;void getenviron(i

4、nt 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 = (char *)malloc(strlen(buff) + 1); strcpy(

5、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; char

6、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,

7、j,k,is_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 <= len;i+) if(inputi = ' ') | (inputi = 't') | (inputi = '0') | (inputi = '|') | (inputi = '>') | (inputi = 'n') if(inputi = 

8、9;|') | (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 = 0;j = 0; if(j = 0)continue; elsebufj+ = '0'if(flag = 0) argvli_cmdk = (

9、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+ = NULL; for(i = 0;i <= 10;i+) fdiFD_RE

10、AD = 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

11、; 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

12、 = 0) if(pipe_in = 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 a

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

14、envhis.start + 1;i <= 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(

15、route != NULL) if(chdir(route) < 0) printf("cd;%s Error file or directory!n",route); void jobs_cmd() struct NODE * p; int i = 1; p = head; if(head != NULL) do printf("%d %d %st%sn",i,p -> pid,p -> state,p -> cmd); i+; p = p -> link;while(p != NULL); else printf(&qu

16、ot;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 ->

17、link = p; 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 o

18、ut; if(p = head) head = head -> link; else 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 = hea

19、d; while(p -> pid != pid1) && (p -> link != NULL) p = p -> link;if(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;

20、 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 bg_cmd(int job_num) struct NODE * p; int i = 0; p = head; for(i = 1;i < job_num;i+) p = p -> link; kill(p -> pid,SIGCONT); strc

21、py(p -> state,"running");void fg_cmd(int job_num) struct NODE * p; int i = 0; p = head; for(i = 1;i < job_num;i+) p = p -> link; strcpy(p -> state,"running"); strcpy(input,p -> cmd); pid1 = p -> pid; signal(SIGTSTP,ctrl_z); kill(p -> pid,SIGCONT); waitpid(p -

22、> 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 = SA_SIGINFO;sigaction(SI

23、GCHLD,& 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 =

24、(char *)malloc(sizeof(char) * (input_len + 1);strcpy(input,buf); for(i = 0,j = 0,k = 0;i <= input_len;i+) if(inputi = '<') | (inputi = '>') | (inputi = '|') if(inputi = '|') pipel(input,input_len); add_history(input); free(input);else redirect(input,input_len

25、); add_history(input); free(input);is_pr = 1;break; if(is_pr = 1) continue;for(i = 0,j = 0,k = 0;i <= input_len;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 = &#

26、39;&') && (inputi + 1 = '0') is_bg = 1; continue; bufj+ = inputi; if(strcmp(arg0,"exit") = 0) add_history(input); printf("Bye bye!n"); free(input); break; if(strcmp(arg0,"history") = 0) add_history(input); history_cmd(); free(input); continue;if(

27、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,"jobs") = 0) add_history(input); jobs_cmd(); free(inpu

28、t); 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); continue;if(strcmp(arg0,"fg") = 0) add_hi

溫馨提示

  • 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論