Starting from:

$24.99

Assignment 1: A Simple Shell Interpreter (SSI)

Programming Assignment 1 P1: A Simple Shell Interpreter (SSI)

1 Introduction
In this assignment you will implement a simple shell interpreter (SSI), using system calls and7 interacting with the system. The SSI will be very similar to the Linux shell bash: it will support8 the foreground execution of programs, ability to change directories, and background execution.9 You can implement your solution in C or C++. Your work will be tested on linux.csc.uvic.ca,10 to which you can remote login by ssh. You can also access Linux computers in ECS labs in person11 or remotely by following https://connex.csc.uvic.ca/portal/site/itsupport/12 Be sure to test your code on linux.csc.uvic.ca before submission. Many students have13 developed their programs for their Mac OS X laptops only to find that their code works differently14 on linux.csc.uvic.ca resulting in a substantial loss of marks.15 Be sure to study the man pages for the various systems calls and functions suggested in this16 assignment. These functions are in Section 2 of the man pages, so you should type (for example):17
$ man 2 waitpid18
2 Schedule19
In order to help you finish this programming assignment on time successfully, the schedule of this20 assignment has been synchronized with both the lectures and the tutorials. There are three tutorials21 arranged during the course of this assignment.22
Date Tutorial Milestones Jan 9/10 C and Linux refreshers, P1 spec go-through, design hints design and code skeleton Jan 16/17 system calls, system programming and testing code almost done Jan 23/24 final testing and last-minute help final deliverable
3 Requirements23
3.1 Basic Execution (5 marks)24
Your SSI shows the prompt25
1
SSI: /home/user 26
for user input. The prompt includes the current directory name in absolute path, e.g., /home/user.27 You can use getcwd() to obtain the current directory.28 Using fork() and execvp(), implement the ability for the user to execute arbitrary commands29 using your shell program. For example, if the user types:30
SSI: /home/user ls -l /usr/bin31
your shell should run the ls program with the parameters -l and /usr/bin—which should list the32 contents of the /usr/bin directory on the screen.33
34 Note: The example above uses 2 arguments. We will, however, test your SSI by invoking35 programs that take more than 2 arguments.36 A well-written shell should support as many arguments as given on the command line.37
3.2 Changing Directories (5 marks)38
Using the functions getcwd() and chdir(), add functionality so that users can:39
• change the current working directory using the command cd40
Note that SSI always shows the current directory at prompt.41 The cd command should take exactly one argument—the name of the directory to change into.42 The special argument .. indicates that the current directory should “move up” by one directory.43 That is, if the current directory is /home/user/subdir and the user types:44
SSI: /home/user/subdir cd ..45
the current working directory will become /home/user.46 The special argument ∼ indicates the home directory of the current user. If cd is used without47 any argument, it is equivalent to cd ∼, i.e., returning to the home directory, e.g., /home/user.48
49 Q: how do you know the user’s home directory location?50 H: from the environment variable with getenv().51 Note: There is no such a program called cd in the system that you can run directly (as you did52 with ls) and change the current directory of the calling program, even if you created one. You53 have to use the system call chdir().54
3.3 Background Execution (5 Marks)55
Many shells allow programs to be started in the background—that is, the program is running, but56 the shell continues to accept input from the user.57 You will implement a simplified version of background execution that supports executing pro-58 cesses in the background. The maximum number of background processes is not limited.59 If the user types: bg cat foo.txt, your SSI shell will start the command cat with the argument60 foo.txt in the background. That is, the program will execute and the SSI shell will also continue61 to execute and give the prompt to accept more commands.62 The command bglist will have the SSI shell display a list of all the programs, including their63 execution arguments, currently executing in the background, e.g.,:64
2
123: /home/user/a1/foo 165 456: /home/user/a1/foo 266 Total Background jobs: 267
In this case, there are 2 background jobs, both running the program foo, the first one with68 process ID 123 and execution argument 1 and the second one with PID 456 and argument 2.69 Your SSI shell must indicate to the user after background jobs have terminated. Read the man70 page for the waitpid() system call. You are suggested to use the WNOHANG option. E.g.,71
SSI: /home/user/subdir cd72 456: /home/user/a1/foo 2 has terminated.73 SSI: /home/user 74
Q: how do you make sure your SSI has this behavior?75 H: check the list of background processes every time processing a user input.76
4 Bonus Features77
Only a simplified shell with limited functionality is required in this assignment. However, students78 have the option to extend their design and implementation to include more features in a regular79 shell or a remote shell (e.g., kill/pause/resuming background processes, capturing and redirecting80 program output, handling many remote clients at the same time, etc).81 If you want to design and implement a bonus feature, you should contact the course instructor by82 email for permission one week before the due date, and clearly indicate the feature in the submission83 of your code. The credit for correctly implemented bonus features will not exceed 20% of the full84 marks for this assignment.85
5 Odds and Ends86
5.1 Compilation87
You will be provided with a Makefile that builds the sample code. It takes care of linking-in the88 GNU readline library for you. The sample code shows you how to use readline() to get input89 from the user, only if you choose to use readline library.90
5.2 Submission91
Submit a tar.gz archive named p1.tar.gz of your assignment through connex, with the Makefile92 You can create a tar.gz archive of the current directory by typing:93
tar zcvf p1.tar.gz *94
Please do not submit .o files or executable files (a.out) files. Erase them before creating the95 archive.96
3
5.3 Helper Programs97
5.3.1 inf.c98
This program takes two parameters:99
tag: a single word which is printed repeatedly100
interval: the interval, in seconds, between two printings of the tag101
The purpose of this program is to help you with debugging background processes. It acts a trivial102 background process, whose presence can be “felt” since it prints a tag (specified by you) every few103 seconds (as specified by you). This program takes a tag so that even when multiple instances of it104 are executing, you can tell the difference between each instance.105 This program considerably simplifies the programming of the part of your SSI shell. You can106 find the running process by ps -ef and kill -9 a process by its process ID pid.107
5.3.2 args.c108
This is a very trivial program which prints out a list of all arguments passed to it.109 This program is provided so that you can verify that your shell passes all arguments supplied on110 the command line — Often, people have off-by-1 errors in their code and pass one argument less.111
5.4 Code Quality112
We cannot specify completely the coding style that we would like to see but it includes the following:113
1. Proper decomposition of a program into subroutines — A 500 line program as a single routine114 won’t suffice.115
2. Comment—judiciously, but not profusely. Comments serve to help a marker. To further116 elaborate:117
(a) Your favorite quote from Star Wars or Douglas Adams’ Hitch-hiker’s Guide to the Galaxy118 does not count as comments. In fact, they simply count as anti-comments, and will result119 in a loss of marks.120 (b) Comment your code in English. It is the official language of this university.121
3. Proper variable names—leia is not a good variable name, it never was and never will be.122
4. Small number of global variables, if any. Most programs need a very small number of global123 variables, if any. (If you have a global variable named temp, think again.)124
5. The return values from all system calls listed in the assignment specification125 should be checked and all values should be dealt with appropriately.126
If you are in doubt about how to write good C code, you can easily find many C style guides on the Net.127 The Indian Hill Style Guide is an excellent short style guide.

More products