1 / 8

Simple Shell

Simple Shell. Part 1 Due date (75%): April, 2002 Part 2 Due date (25%): Apr 5, 2002. Description. a basic shell program that supports commands with < and > I/O re-direction. 3 built-in commands (pwd, cd and exit) > ./myshell % ps -ef % ls > ls.out % wc < ls.out % pwd % exit

jenn
Download Presentation

Simple Shell

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Simple Shell Part 1 Due date (75%): April, 2002 Part 2 Due date (25%): Apr 5, 2002

  2. Description • a basic shell program that supports commands with < and > I/O re-direction. • 3 built-in commands (pwd, cd and exit) • > ./myshell • % ps -ef • % ls > ls.out • % wc < ls.out • % pwd • % exit • Part2 : 25% extending the project part1 for handling pipe • % ls | wc

  3. What to hand in • Submit a tar file using the following command • %tar cvf p1.tar README typescript *.C *.h Makefile • A README file with: • Your name and your partner's name • If you have not fully implemented all shell functionality then list the parts that work (and how to test them if it is not obvious) so that you can be sure to receive credit for the parts you do have working. • All the source files needed to compile, run and test your code (Makefile, .c or c++ files, optional test scripts). Do not submit object or executable files. • Output from your testing of your shell program. • simple Unix commands • built-in commands • I/O redirection • error conditions

  4. Suggestions • Two main modules • Command parsing • Command execution to handle built-in cmd or forking a child process to execute other command and I/O redirection

  5. Forking a child process

  6. Unix system calls • getenv: get the value of an environment variable • path = getenv("PATH"); • cwd = getenv("PWD"); • chdir: change the current working directory (use this to implement cd) • fork-join: create a new child process and wait for it to exit: if (fork() == 0) { // the child process } else { // the parent process pid = wait(&status); }

  7. System calls (continued) • execv: overlay a new process image on calling process execvp( full_path_name, command_argv_list, 0) • access: check to see if a file is accessible in some way. access(full_path_name_of_file, X_OK | F_OK) • open, close, dup: for I/O Redirection //also see fopen() freopen() // to re-direct stdout to file foo int fid = open(foo, O_WRONLY|O_CREAT); close(fid); pipe(fid); dup(fid); dup2(fid, 0); close(fid);

  8. #include <stream.h> #include <string> #include <unistd.h> #include <sys/wait.h> #define MAXLINE 80 #define WHITE "\t \n" #define MAXARG 20 typedef char *String; main() { int pid; union wait status; int i = 0; char cmd[MAXLINE]; String argl[MAXARG]; cout << "enter your command % "; cin.getline(cmd, MAXLINE); // fill in arguments argl[i++] = strtok(cmd, WHITE); while ( i < MAXARG && (argl[i++] = strtok(NULL, WHITE)) != NULL); // forking a child process if (fork() == 0) { // the child process if (execvp (argl[0], argl) == -1) { cerr << "error executing a given command" << endl; } } else { // the parent process pid = wait(&status); cout << "the child's execution is completed" << endl; } } Forking a child process

More Related