Implementing a Tiny Shell in Operating Systems - Assignment | CSC 1600, Assignments of Operating Systems

Material Type: Assignment; Professor: Damian-Iordache; Class: Operating Systems; Subject: Computer Science; University: Villanova University; Term: Unknown 1989;

Typology: Assignments

Pre 2010

Uploaded on 02/25/2010

koofers-user-w20
koofers-user-w20 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSC 1600 – Implementing a Tiny Shell
This is a group assignment. The goal of this assignment is to expand your understanding of how a shell
works by write a shell similar to, but much simpler, than the one you run on Unix (the bash shell).
A shell is an interactive command-line interpreter that runs programs on behalf of the user. A shell
repeatedly prints a prompt, waits for a command line on stdin, and then carries out some action, as
directed by the contents of the command line.
The command line is a sequence of ASCII text words delimited by whitespace. The first word in the
command line is either the name of a built-in command (such as exit or quit) or the pathname of an
executable file. The remaining words are command-line arguments. If the first word is a built-in command,
the shell immediately executes the command in the current process. Otherwise, the word is assumed to be the
pathname of an executable program. In this case, the shell forks a child process, then loads and runs the
program in the context of the child. For example, typing the command line
bash > ls -l -d
runs the ls program. By convention, the shell ensures that when the ls program begins executing its main
routine
int main(int argc, char *argv[])
the argc and argv arguments have the following values:
argc == 3
argv[0] == “/bin/ls”
argv[1] == “-l”
argv[2] == “-d”
An outline of the shell code is given on the next page. However, feel free to use your own code design.
pf3
pf4
pf5

Partial preview of the text

Download Implementing a Tiny Shell in Operating Systems - Assignment | CSC 1600 and more Assignments Operating Systems in PDF only on Docsity!

CSC 1600 – Implementing a Tiny Shell

This is a group assignment. The goal of this assignment is to expand your understanding of how a shell works by write a shell similar to, but much simpler, than the one you run on Unix (the bash shell).

A shell is an interactive command-line interpreter that runs programs on behalf of the user. A shell repeatedly prints a prompt, waits for a command line on stdin, and then carries out some action, as directed by the contents of the command line.

The command line is a sequence of ASCII text words delimited by whitespace. The first word in the command line is either the name of a built-in command (such as exit or quit) or the pathname of an executable file. The remaining words are command-line arguments. If the first word is a built-in command, the shell immediately executes the command in the current process. Otherwise, the word is assumed to be the pathname of an executable program. In this case, the shell forks a child process, then loads and runs the program in the context of the child. For example, typing the command line

bash > ls -l -d

runs the ls program. By convention, the shell ensures that when the ls program begins executing its main routine

int main(int argc, char *argv[])

the argc and argv arguments have the following values:

argc == 3 argv[0] == “/bin/ls” argv[1] == “-l” argv[2] == “-d”

An outline of the shell code is given on the next page. However, feel free to use your own code design.

Outline of the tiny shell code

  1. Get the process identifier and use it as a shell prompt (use getpid).
  2. In an infinite loop
    • Print the shell prompt (the equivalent of “starting the shell”).
    • Read a user command into a finite size buffer with fgets. If user input is “quit” or “exit”, terminate the program. You will need to use strcmp or strcmp to compare these strings against the user input.
    • Split the user command into tokens to extract the command name and the command arguments.
    • Fork out a child process to execute the user command.
    • The parent process must wait for the child process to finish executing the command. Infinite loop ends here.
  3. The child process:
    • Try to execute the command (use execvp).
    • If the command could not be executed, print out an error message.

Recall:

If you have a printf statement AFTER an execv call, that printf statement will get executed ONLY if execv fails. Otherwise, execv will delete the image of the calling process (including the printf line)!

  1. To find information on a library routine, look at the manual pages (using the Unix command man). Be sure to check the return code of all routines for errors!
  2. Defensive programming is an important concept in computer systems. A computer system cannot simply fail when it encounters an error: it must check all parameters before it trusts them. In general, there should be no circumstances in which your C program will dump a core. Therefore, your program must respond to all input in a reasonable manner – that is, print an understandable error message and either continue processing or exit.

Here is an example. Suppose that your program reads in commands from the user into a local buffer called command:

#define MAXCMD 20 char usercmd[MAXCMD];

However, the user command may be longer than the buffer you have allocated for it. In this case, your program should (a) Read and discard the remaining characters in the line (use fgets in a loop) (b) Print out an error message

To check if the user has typed in a line longer than MAXCMD:

if(usercmd[strlen(usercmd)-1] != '\n') // line longer than the buffer size // read the rest of the line and ignore it

To get rid of the extra characters in the input line:

if(usercmd[strlen(usercmd)-1] != '\n') do { fgets(usercmd, MAX_CMD, stdin); } while(usercmd[strlen(usercmd)-1] != '\n'); // now return to the shell prompt

Sample output

Hi, I am shell 12715.

12715 > ls

test.c test a.out

12715 > pwd

/mnt/a/mdamian/test

12715 > hi

Command not found.

12715 > Loooooooooooooooooooooooooooooooooooooong

Command too long! Use 20 characters or less.

12715 > quit

Bye

Submission guidelines

Hand in a printout of your code, a sample output and a short README files documenting your program. The README should have three sections:

  1. Your name, your Unix account name, and the directory where the code is placed. In the case of group work, please list the names of all students involved in writing the code.
  2. An explanation of any design decisions that you made (for example, any interesting functions you used that were not specified in the assignment).
  3. Any bugs and/or deviations in your program from the assignment specification.

Your C source file (tinyshell.c) and the README file should be in directory ~/csc1600/tinyshell.

Have fun!