simple shell part 1 due date (75%): april, 2002 part 2 due date (25%): apr 5, 2002

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

Upload: naomi-kelly

Post on 29-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

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

Simple Shell

Part 1 Due date (75%): April, 2002

Part 2 Due date (25%): Apr 5, 2002

Page 2: 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

• Part2 : 25% extending the project part1 for handling pipe– % ls | wc

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

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

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

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

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

Forking a child process

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

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); }

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

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);

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

Forking a child process#include <stream.h>#include <string>#include <unistd.h>#include <sys/wait.h>#define MAXLINE 80#define WHITE "\t \n"#define MAXARG 20typedef 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; }}