Docsity

Shell Project, Part 3 | Operating Systems | COMPSCI 453, Lab Reports of Operating Systems

Toggle sidebar
bg1
COMPSCI 453/552: Operating Systems
Shell Project, part 3 (115 points)
Due Date -On Class Home Page–
1 A Not-so Mini Shell
In the last release of the mini-shell we will work on memory management and also, optionally, add
the capability to execute pipelines.
First, we will replace malloc with your own memory management scheme based on the Buddy
system algorithm discussed in class. Additionally, we will also add a few other minor features to
the shell like redirection of input/output, ability to execute multiple commands per line and a new
built-in history command.
A total of 15 points are reserved for the documentation and style of coding. Use your README file
to communicate the structure of the files, the significant highlights of your project, test cases, the
limitations and features, things that can still be improved etc.
2 Requirements
2.1 Minor new features (20 points)
•Multiple commands per line. (5 points). The shell supports multiple commands separated
by a semi-colon on a single command line. Interaction between ampersand and semi-colon:
The bash shell treats the following as a syntax error...
ls & ; date
However, it does treat the following correctly as one background and one foreground com-
mand:
ls & date
We will support the former (that is always use a semi-colon style) command line. Here are
some test cases:
;
ls; sort < /etc/passwd > /tmp/log; who
ls > /tmp/ls.out; sort < /etc/passwd;;;
ls; ls -l /usr/bin | sort | wc -l ; date
•Built-in history command. (5 points) The shell supports a history command. We will
use a new built-in command history <n>, that prints out to standard output the last n
commands the user has typed in prefixed by the index of the command in the history. If the
argument is skipped, then history prints out all the commands the user has typed in since
starting the session.
pf3
pf4
pf5

Partial preview of the text

Download Shell Project, Part 3 | Operating Systems | COMPSCI 453 and more Lab Reports Operating Systems in PDF only on Docsity!

COMPSCI 453/552: Operating Systems

Shell Project, part 3 (115 points)

Due Date -On Class Home Page–

1 A Not-so Mini Shell

In the last release of the mini-shell we will work on memory management and also, optionally, add the capability to execute pipelines.

First, we will replace malloc with your own memory management scheme based on the Buddy system algorithm discussed in class. Additionally, we will also add a few other minor features to the shell like redirection of input/output, ability to execute multiple commands per line and a new built-in history command.

A total of 15 points are reserved for the documentation and style of coding. Use your README file to communicate the structure of the files, the significant highlights of your project, test cases, the limitations and features, things that can still be improved etc.

2 Requirements

2.1 Minor new features (20 points)

  • Multiple commands per line. (5 points). The shell supports multiple commands separated by a semi-colon on a single command line. Interaction between ampersand and semi-colon: The bash shell treats the following as a syntax error... ls & ; date However, it does treat the following correctly as one background and one foreground com- mand: ls & date We will support the former (that is always use a semi-colon style) command line. Here are some test cases:

; ls; sort < /etc/passwd > /tmp/log; who ls > /tmp/ls.out; sort < /etc/passwd;;; ls; ls -l /usr/bin | sort | wc -l ; date

  • Built-in history command. (5 points) The shell supports a history command. We will use a new built-in command history , that prints out to standard output the last n commands the user has typed in prefixed by the index of the command in the history. If the argument is skipped, then history prints out all the commands the user has typed in since starting the session.

dash: history 1 ls 2 w 3 cd 4 c 5 ls 6 w 7 rm plugin131_02.trace 8 ls 9 cs 10 ls 11 cd lab-solns/ 12 ls 13 cd p 14 ls 15 cd ../p 16 ls dash:

Use the GNU readline library for implementing the history command. Check the header file /usr/include/readline/history.h to find helper functions to implement the history command.

  • Input/Output Redirection. (10 points). Use the following syntax for redirecting files.

cmd < inputfile cmd > outputfile cmd < inputfile > outputfile For input redirection, try to open the file specified. If the file doesn’t exist or the user does not have permission to open it for reading the redirection should fail and a new prompt displayed. For output redirection, create a new file if it doesn’t already exist. Note that open creates a new file by bit-wise AND-ing the mode provided as an argument to the open() system call and the users umask. So if we want to create the output file to have read and write access for the user, and read access for the group and others, we can specify the mode as S IRUSR | S IWUSR | S IRGRP | S IROTH as an argument to open() and the open() will take care of restricting the permissions further depending on the users mask. The following session illustrates the above ideas. kohinoor:dash dash> ls > log dash> ls -l log -rw-r--r-- 1 amit faculty 264 Nov 13 12:26 log dash> chmod +x log dash> ls -l log -rwxr-xr-x 1 amit faculty 264 Nov 13 12:26 log dash> ls > log

Hints. Read Section 5.4 (Address Arithmetic) and Section 8.7 (Example–A Storage Allocator) from the C book by Kernighan and Ritchie to help you get started.

2.3 Pipes (50 points extra credit)

2.3.1 Basic Pipes (25 points)

The syntax for specifying a pipeline (in BNF) is as follows:

[ | ]

Here is an example of a three command pipeline.

cmd1 | cmd2 | cmd

Your implementation should be able to support any size pipeline (that is, you may have a constant that limits the size of the pipeline but to change this limit should require only changing this constant and nothing else in the code. Make this constant at least 10.)

2.3.2 Pipes and Signals (15 points)

The interrupt character (ctrl-c) should kill the whole pipeline if it is running in the foreground. It should not affect any pipelines running in the background.

If the user types the suspend character (ctrl-z under Linux) at the terminal, then the whole pipeline should suspend. Then the fg command should restart the pipeline. Make sure that you restart all the processes in the pipeline (and not just the last one!). Similarly, the bg command should continue the pipeline in the background.

2.3.3 Pipes and Redirection (5 points)

The shell supports both redirection and pipes in the same command. Redirection should be sup- ported only at either ends of the pipe. For example:

sort < /etc/passwd| wc last | sort | uniq -c | sort -rn > /tmp/log

2.3.4 Pipes in the Background (5 points)

The shell supports starting a pipeline in the background by having an ampersand after the last command. For example, the following pipeline runs in the background (and is not affected by signals).

ls | sort | wc & last | sort | uniq -c | sort -rn > /tmp/log &

3 Hints on Testing and Development.

  • Implement input and output redirection first as it is considerably simpler than implementing pipelines or memory management. Test out your implementation by trying simple commands with input redirection, with output redirection, and with both input and output redirection. Here are some simple test cases. dash> ls -sl > log dash> more < log ... dash> sort < log > junk dash> more junk ...
  • Finish adding other minor features. Test your shell fully at this point.. Run it under valgrind to make sure it is in good shape before attempting either pipes or memory management. Once you have a working shell, check it in and save a copy!
  • Buddy memory management system. Now implement the Buddy system as a separate class and test it using small programs. A good simple test would be use your linked lists and the TestList program with your memory manager. Once you are confident in your Buddy memory manager, then integrate it into your shell and then test shell again to make sure that it still works as well as before.
  • Implement pipeline of a fixed size (say three) first. Don’t worry about signal handling right now. You may also find it easier to separate the handling of pipelines from handling of single commands. The following is a good test for your three command pipeline implementation. cat test3.data | sort | wc where test3.data is a very large data file that you can make a link to from ∼amit/cs453/lab/ch on onyx.
  • Now try to build in signal handling for pipes. The interrupt signal should kill the whole pipeline and not just the last process. The stop signal should stop all the processes in the pipeline. The stop signal should also give the whole pipeline a job number (just like stopping a single command).
  • Now get the fg and bg commands to work for pipelines. The test pipeline given above is a reasonable test case (if you can hit Ctrl-Z quickly!)
  • Next get redirection and pipes to work together.
  • Finally generalize the pipeline code to handle pipelines with arbitrary number of commands. Remember that a process should not be started until the writing end of the pipe is available! Make sure to test it out on pipelines of various sizes. Here are some test cases: date | wc cat /usr/share/dict/words | wc -l