Programming in c. create new shell to perform the functions

1 answer below »
Programming in c. create new shell to perform the functions


CSCI 360: Introduction to Operating System 1 | P a g e Assignment 1: Simple Unix Shell Objectives 1. Learn Unix Shell. 2. Learn Unix Parent and Child Processes. 3. Learn Unix Input and Output redirections. 4. Learn Unix Pipe. 5. Learn Unix fork(), exec(), wait(), open(), close(), dup(), and pipe() system calls. 6. Implement a Simple Shell Application using Unix system calls. Specifications This assignment consists of designing a C program to serve as a shell interface that accepts user commands and then executes each command in a separate process. A shell interface gives the user a prompt, after which the next command is entered. The example below illustrates the prompt “CSCI360>” and the user’s next command: “cat prog.c”. (This command displays the file prog.c on the terminal using the UNIX cat command.) CSCI360> 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 (in this case, 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. However, UNIX shells typically also allow the child process to run in the background, or concurrently. To accomplish this, we add an ampersand (&) at the end of the command. Thus, if we rewrite the above command as CSCI360> cat prog.c & the parent and child processes will run concurrently. The separate child process is created using the fork() system call, and the user’s command is executed using one of the system calls in the exec() family. The outline of a C program that provides the general operations of a command-line shell is given below. The main() function presents the prompt “CSCI360>” to accept user commands and takes the steps after the command from the user has been read. The main() function continually loops until the user enters “exit” at the prompt. Your program must terminate when the user enters ‘exit’ command. CSCI 360: Introduction to Operating System 2 | P a g e Your program must fork a child process to execute the command specified by the user. This will require parsing what the user has entered into separate tokens and storing the tokens in an array of character strings, char* args[]. For example, if the user enters the command ps –ael at the CSCI360> prompt, the values stored in the args array are: args[0] = "ps" args[1] = "-ael" args[2] = NULL This args array will be passed to the execvp() function, which has the following prototype: execvp(char *command, char *params[]); Here, command represents the command to be performed and params stores the parameters to this command. For this project, the execvp() function should be invoked as execvp(args[0], args). If the user hit enter at the prompt instead of a command, the program must do nothing but output the prompt again so that user can type a command. If the user type a valid UNIX command, the program has to perform it in a child process. If the user type a command with output (>) redirection, the program must handle it in its child process. For example, in order to get the list of subdirectories and files in the current directory into a file named “dir-list” instead of standard output, the user may type CSCI360>ls > dir-list If the user type a command with input (<) redirection,="" the="" program="" must="" handle="" it="" in="" its="" child="" process.="" for="" example,="" in="" order="" to="" get="" the="" number="" of="" entries="" in="" the="" current="" directory="" using="" above="" “dir-list”="" file,="" which="" was="" populated="" with="" the="" list="" of="" subdirectories="" and="" files="" in="" the="" current="" directory,="" the="" user="" may="" type="" csci360="">wc -l< dir-list="" if="" the="" user="" type="" two="" commands="" with="" a="" pipe="" (|)="" between="" them,="" the="" program="" must="" handle="" them="" in="" two="" separate="" child="" processes="" opening="" a="" pipe="" between="" these="" child="" processes.="" for="" example,="" in="" order="" to="" get="" to="" get="" the="" number="" of="" entries="" in="" the="" current="" directory,="" the="" user="" may="" type="" csci360="">ls | wc -l The program must create one child process for the command “ls” and another child process for the command “wc”. The program must open a pipe between ls’s child process and wc’s child process. CSCI 360: Introduction to Operating System 3 | P a g e If the user type “!!” instead of a valid UNIX command, the program must execute the most recent command from the command history, if there is any. Remember, command “!!” should never be added into command history. Usually, the parent process wait for the child processes to complete. Be sure to check whether the user included an ampersand (&) at the end of the command to determine whether or not the parent process needs to wait for the child to exit. If ampersand (&) is included, parent process does not need to wait for the child processes to complete, all processes run concurrently. In this assignment, you don’t need to implement concurrent processing with any sort of redirections. Your program must keep track of the most recent commands and their order in a command history cache. Your history cache will have a maximum limit, however, it must keep track of the most recent commands and their order. If the history cache is full, the new command must be added by replacing the oldest command. When user enters ‘history’ command your program must display the commands from the history cache in the order they were entered, the oldest command first and be numbered 0. Remember, command “history” itself must not be added into the history. Tasks 1. You will submit this assignment using GIT submission system. A central repository named ‘assignment1” has been created for this assignment. 2. Create your own fork of assignment1 on the central GIT repository using following command: ssh csci fork csci360/assignment1 csci360/$USER/assignment1 3. Create a folder named csci360 in your home folder. 4. Go into your csci360 folder and create a clone of your forked assignment1 repository using following command: git clone csci:csci360/$USER/assignment1 5. Repository assignment1 has been organized as follows: CSCI 360: Introduction to Operating System 4 | P a g e assignment1 bin build include resource src Makefile README example bin A README file template has been placed in the root of the application development folder. The README file gives a general idea of the application, technologies used in developing the application, how to build and install the application, how to use the application, list of contributors to the application, and what type of license is given to the users of the application. You will need to complete the README file before your final submission. The specifications or header file (360shell.h) has been placed in include sub folder. All source codes (360shell.c and main.c) go into src sub folder. The main.c file in the src folder will use the functions from your source code in 360shell.c. You need to complete main.c file. All object files will be placed in build sub folder and executable files in bin sub folder by make. Data files are usually placed in resource folder. In this assignment there is no data file in resource folder. A Makefile has been placed in the root of the application development folder. The Makefile does the followings and you don’t need to and must not modify Makefile: a) Defines and uses macros for each GCC flag that you are going to use to compile the codes and objects. b) Defines and uses macros for each sub folder of the application, e.g., src, include, resource, build, and bin. c) Uses GCC debug flag to facilitate debugging using gdb. CSCI 360: Introduction to Operating System 5 | P a g e d) Uses GCC include flag to specify the path of application’s custom header files so that the code does not need to specify relative path of these header files in #include pre-processor macro. e) Creates individual object file (*.o) into build folder from each source (*.c) file of src folder. f) Links the necessary object files from the build folder into a single executable file in bin folder. g) Runs the main executable of the application from bin folder. h) Cleans or removes files from both build and bin folders using PHONY target named clean. 6. One example executable (360shell) of this application has been placed in the example/bin folder. You can run this example executable to get an idea what is expected from you in this assignment. To run the example executable type the following from the assignment root folder: make run-example This example executable has been built and tested in Linux Debian machines available in the labs. Run this executable in other kind of machines at your own risks. Make clean command will not delete this example executable and you should not delete it either. 7. Type following at the command prompt to clean previously built artefacts (binary and object files) of the application: make clean 8. Type following at the command prompt to build the application from your own source code: make 9. Type following at the command prompt to run your own application: make run 10. Make sure you can compile, link, run, and test this application error and warning free. 11. Complete the README file. Therefore, you need to give the general description of the application, technologies that are used in the application, how a user can build (compile and link) and install the application, how a user can run the application after the installation. Mention instructor’s name and your name in the list of contributors. Give GPL license to the users to use the application. You can google to find README examples if you are not sure how to write one. 12. Organize and comment your code to make it easy to understand. Make sure you have typed your name and student number in the top comment
Answered 2 days AfterSep 11, 2022

Answer To: Programming in c. create new shell to perform the functions

Aditi answered on Sep 12 2022
53 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here