CMPUT 379 Assignment 1 Mini Shell solution




5/5 - (8 votes)

In computing, a shell is a user interface for access to an operating system’s services. In general, operating system shells use either a command-line interface (CLI) or graphical user interface (GUI), depending on a computer’s role and particular operation. It is named a shell because it is the outermost layer around the operating system kernel. [Wikipedia] This assignment is for you to build a CLI shell interface. It will accept a set of simple commands that your program will execute. The assignment has several important goals, including exposing you to systems programming, running multiple processes, resource management, and process communication. The shell being built in this assignment has three important characteristics: • Minimalist functionality: only a small set of features is being required for the assignment. The intent is to maximize the pedagogical value for the effort expended. Hopefully in doing this assignment you will appreciate how easy it is to add useful functionality to the shell that you build. • Simple interface: command lines for the shell are simple and structured. Clearly no widelyused shell would accept such constraints. The intent here is to reduce the amount of work needed to program the parsing of command lines. You won’t learn much if all your effort goes to writing code to implement the command line interface. • As defined in the assignment, some of the implementation details may differ from what would be seen if one were trying to build a high-performing, widely-used product. Here the emphasis is to have the student explore using a number of system interfaces, even if the solution might not be the best in the given context. Again, the intent is to maximize the pedagogical value for the effort expended. Specifications You will write a program called shell379 that accepts and executes the following commands. Some of the commands accept an integer parameter (). exit End the execution of shell379. Wait until all processes initiated by the shell are complete. Print out the total user and system time for all processes run by the shell. jobs Display the status of all running processes spawned by shell379. See the print format below in the example. kill Kill process . resume Resume the execution of process . This undoes a suspend. sleep Sleep for seconds. suspend Suspend execution of process . A resume will reawaken it. wait Wait until process has completed execution. If none of the above commands is input, then the resulting input string is to be executed by shell379. * Spawn a process to execute command with 0 or more arguments . 1 and are each one or more sequences of nonblank characters. There are three special arguments that a command may have: & If used, this must be the last argument and indicates that the command is to be executed in the background. fname This argument is the “>” character followed by a string of characters, a file name to be used for program output. The above syntax is overly restrictive, again to limit the amount of programming. Sample Output shell379 input lines are shown in bold. All output is in regular font. Runner and sleeper are programs I wrote to test out the shell; they are not part of the assignment. SHELL379: jobs Running processes: Processes = 0 active Completed processes: User time = 0 seconds Sys time = 0 seconds SHELL379: cat input 15 200000000000 SHELL379: time runner output & 1 The “*” is often used in shell programming to indicate “0 or more” of something. The “+” can mean “1 or more”, depending on the context. SHELL379: jobs Running processes: # PID S SEC COMMAND 0: 56188 R 0 runner output & Processes = 1 active Completed processes: User time = 11 seconds Sys time = 3 seconds SHELL379: sleeper 5 & SHELL379: sleeper 6 & SHELL379: jobs Running processes: # PID S SEC COMMAND 0: 56188 R 10 runner output & 1: 56190 R 0 sleeper 5 & 2: 56192 R 0 sleeper 6 & Processes = 3 active Completed processes: User time = 11 seconds Sys time = 3 seconds SHELL379: wait 56188 SHELL379: jobs Running processes: # PID S SEC COMMAND 0: 56192 R 0 sleeper 6 & Processes = 1 active Completed processes: User time = 22 seconds Sys time = 6 seconds SHELL379: runner adds an entry to the table. Kill ends a process and removes it from the table. As jobs finish, they are removed from the table. Resume/suspend change the execution of a process, and this state change has to be updated in the table. The jobs command displays two sets of times. Under the heading “Completed processes”, the times given are the total execution times of all completed processes. Under the “Running processes” heading, the time given for each process is the current amount of execution time used. For this assignment, you are to get the information from the ps command and use a pipe to access the data. Some of the system calls you might consider for your implementation include exec (note that there are multiple variations of exec), fork, getrusage, kill, popen, signal (again, there are multiple sig-related calls), times, wait and perror. Note that there may be different implementation solutions for each of the shell379 commands. You are not allowed to use the system call system. Your program must be implemented in C or C++. Create a makefile to compile your program and produce an executable called shell379. Your program must consist of at least three source code files and at least one header file. Your code should be logically organized between these files. To make things simple, we will use some constants in your program. Again, this is to minimize the programming effort: LINE_LENGTH 100 // Max # of characters in an input line MAX_ARGS 7 // Max number of arguments to a command MAX_LENGTH 20 // Max # of characters in an argument MAX_PT_ENTRIES 32 // Max entries in the Process Table A useful resource for programming this assignment is Chapter 5 of Three Easy Pieces. Warning During your development of a solution to this assignment, an incorrect implementation may leave processes running in the background after you log out. It is your responsibility to make sure that you clean up all processes. Students who leave processes running on machines may be penalized. Grading Here are some important things to watch out for: • You will be penalized if your program leaves processes running after it exits. • Your program output should match that given in this document. • Your makefile should do the minimum amount of work required to produce an executable. • All assignment assessments will be done on the undergraduate lab machines. Before you submit your assignment, please verify that your program compiles and runs on the lab machines. This assignment is to be done individually. If you use resources/ideas that are not your own, you must document them and their source in your program. Submission Submit the following as a single ZIP file: • All source code files (C, C++, headers), and • makefile. The assignment is due no later than 10:00 PM on Sunday, October 3. It can be submitted Late assignments received before 10:00 PM on Monday, October 4 will have their assignment grade lowered by 20%.