CS代写 CSE 2431 LAB 2

CSE 2431 LAB 2

1. Purpose
a. Gain experience with the concept of processes and Linux forking

Copyright By PowCoder代写 加微信 powcoder

b. Create, manage, and terminate processes within C code
c. Gain additional familiarity with Linux

2. Submission (source code only):
Please don’t submit executables as they will unnecessarily waste space in Carmen.

You should remove your debug print statements and ‘bad’ code before you submit your lab (e.g., don’t just comment them out, actually delete them).

3. Introduction
This lab assignment asks you to build a simple command-line interface (SHELL) using the C programming language that accepts user commands, creates a child process, and executes the user commands in the child process. The command-line interface provides users a prompt after which the next command is entered. The example below illustrates the prompt sh> and the user’s next command: cat prog.c. This command displays the contents of file prog.c on the terminal using the UNIX cat command.
sh> cat prog.c
One technique for implementing a shell interface is to have the parent process first read what the user enters on the command line (i.e., cat prog.c), and then create a separate child process that performs the command. Unless otherwise specified, the parent process waits for the child to exit before continuing. This is similar to what is illustrated in this figure:

However, UNIX shells typically also allow the child process to run in the background – or concurrently – by specifying the ampersand (&) at the end of the command. Thus, by rewriting the above command as follows, the parent and child processes now run concurrently:
sh> cat prog.c &
The child process is created using the fork() system call and the user’s command is executed by using one of the system calls in the exec() family. For more details about the system call, you can use the man command for online documentation.

4. A Simple Shell (Assignment Details)
A program template for a simple command line shell is supplied in Carmen in the file myshell.c.
This template program is composed of two functions: main() and setup().
The setup() function reads in the user’s next command (which can be up to 80 characters), and then parses it into separate tokens that are used to fill the argument vector for the command to be executed.
If the command is to be run in the background, it will end with ‘&’, and setup() will update the parameter background so the main() function can act accordingly. This program is terminated when the user enters Ctrl-D in which case setup() invokes exit() directly without return to the main() function.
The main() function presents the prompt ‘COMMAND-> ’ and then invokes setup(), which waits for the user to enter a command. The contents of the command entered by the user are loaded into the args array. For example, if the user enters ‘ls –l’ at the command prompt, args[0] will be set to the string ‘ls’ and args[1] will be set to the string ‘–l’. By “string”, we mean a null-terminated, C-style string variable.
You must modify the main() function to create a child process and execute the command entered by the user. To do this, you need to update the main() function so that upon returning from setup(), a child process is forked. After that, the child process executes the command specified by a user via execvp().
As noted above, the setup() function loads the contents of the args array with the command specified by the user. This args array will be passed to the execvp() function, which has the following interface:
execvp(char *command, char *params[]);
where command represents the command to be performed and params stores the parameters to this command. You can find more information on execvp() by issuing the command man execvp. Note, you should check the value of background to determine if the parent process is to wait for the child to exit or not, and the execvp() definition of parameters for its arguments might not match your natural thought for command line parameters.
You must also change the prompt from ‘COMMAND-> ’ to ‘COMMAND [#] >’ where # is the input command number. Start your count with 1. Thus, your initial prompt should be:
COMMAND [1] >

You must implement the ‘cd’ command. Your implementation of the ‘cd’ commend should:
A. Use the system call ‘chdir’
B. Display a customized error message if no directory is specified. (e.g., Do not go to ~ like a normal shell would. Catch this entry, do not call chdir, report this as an error, and stay where you are.)
C. Display a customized error message if something fails in the chdir system call
D. Note: Your CD command will not be tested via background processing to simplify the specialized error handling listed in B & C above. Put another way, the graders will not test things like ‘cd temp &’.

Your code should be commented in a professional manner.

5. Compile Instructions
If you didn’t install gcc during Lab1, you’ll need to do something like the following to install it:
sudo apt-get install build-essential

The code can be compiled with Makefile provided by simply typing:
or directly using the command:
gcc -g -o myshell myshell.c

Note: the grader will compile and test your solution in an Ubuntu VM using the gcc compiler. Any program that does not compile may receive a zero. The grader will not spend any time fixing or debugging your code while grading. It is your responsibility to make sure it compiles, runs, and is well tested.
If you run make and get one of the following warnings, you are experiencing clock skew. A fix for this was in the Lab1 write-up.
Warning: File ‘Makefile’ has modification time 3679 s in the future
make: warning: Clock skew detected. Your build may be incomplete.
Once compiled cleanly, you can run your program by the command:
The ‘./’ is required since by default, the current directory is not in the search path for commands on Linux.

6. Testing your program
Since your program calls execvp, your shell should be able to run any program, so be careful what you enter. Here are some suggestions to help you get started on testing. Examples like this will be used during grading. See the Rubric.

A. List current directory contents via a background process
COMMAND-> ls -la &
Output: Note that by using the background option, your prompt may be output before the background task completes, and the first line of the background output may appear on the command line prompt. This is the appropriate visual, you are intermixing process outputs to the same screen. We’ll be looking for this intermixing to make sure you’re really running things concurrently. If you press enter (e.g., no command), you can force a command prompt refresh after the output.

B. Put in a non-existent command
COMMAND-> this-will-fail
Output: Your program should include a message that includes something from the input so the user knows what failed, not just the error status codes. It is sufficient to include args[0] in your message, you do not need to echo the complete input since the setup() function parses it into lots of pieces and I don’t want you worrying about changing setup().

C. Make a new directory temp; copy your myshell.c into the new directory; move into the new directory, print the test file; then move back up to the original directory.
COMMAND-> mkdir temp
COMMAND-> cp myshell.c temp/test.c
COMMAND-> cd temp
COMMAND-> cat test.c
COMMAND-> cd ..

D. Start a second myshell program
COMMAND-> ./myshell
Output: Starts a second myshell in the first myshell program. The command prompt should be waiting for user’s input. When you press Ctrl-D, you should go back into your first myshell, still having a ‘COMMAND-> ’ waiting for input. Once you’ve modified the command prompt to include command numbers, this action will be more obvious.

E. Sprinkle in other commands like you’ve seen in demos, such as
COMMAND-> w
COMMAND-> ps -f

Output: The ‘w’ will show ‘who’ is running the process. The ps should sow information about the running and zombie processes. Remember, in this lab, zombie processes are expected. When you execute commands like you’ve seen in the class demos, the output should be similar.

7. Submission
You only need to submit your myshell.c file to Carmen.

CSE 2431 Lab 2

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com