Best代写-最专业靠谱代写IT | CS | 留学生作业 | 编程代写Java | Python |C/C++ | PHP | Matlab | Assignment Project Homework代写

C语言代写 | CSE 2431 LAB 2

C语言代写 | CSE 2431 LAB 2


CSE 2431 LAB 2

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.

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: