Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this assignment, I need it to work with the Linux Ubuntu operating system. The code language used is C. Notes for this assignment: Ignore

For this assignment, I need it to work with the Linux Ubuntu operating system. The code language used is C.

Notes for this assignment:

Ignore the "Submission" instructions, skip that part.

The code for shell.c is provided first following the instructions. The start of the file is marked with "START OF shell.c" end of the file is marked with "END OF shell.c".

After that, the test.txt file is provided.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

START OF shell.c

#include #include #include

#define MAX_LINE 80 /* 80 chars per line, per command, should be enough. */

/** * setup() reads in the next command line, separating it into distinct tokens * using whitespace as delimiters. setup() sets the args parameter as a * null-terminated string. */

void setup(char inputBuffer[], char *args[],int *background) { int length, /* # of characters in the command line */ i, /* loop index for accessing inputBuffer array */ start, /* index where beginning of next command parameter is */ ct; /* index of where to place the next parameter into args[] */ ct = 0; /* read what the user enters on the command line */ length = read(STDIN_FILENO, inputBuffer, MAX_LINE);

start = -1; if (length == 0) exit(0); /* ^d was entered, end of user command stream */ if (length

case '&': *background = 1; inputBuffer[i] = '\0'; break; default : /* some other character */ if (start == -1) start = i; } } args[ct] = NULL; /* just in case the input line was > 80 */ }

int main(void) { char inputBuffer[MAX_LINE]; /* buffer to hold the command entered */ int background; /* equals 1 if a command is followed by '&' */ char *args[MAX_LINE/2+1];/* command line (of 80) has max of 40 arguments */

while (1){ /* Program terminates normally inside setup */ background = 0; printf("COMMAND-> "); setup(inputBuffer, args, &background); /* get next command */

/* the steps are: (1) fork a child process using fork() (2) the child process will invoke execvp() (3) if background == 0, the parent will wait, otherwise returns to the setup() function. */ } } END OF shell.c

START OF test.txt

How to Use the Simple Test Cases:

1. Create a `test' directory into your home directory by

$ mkdir test

2. Copy this lab1-test.txt file into the test directory by

$ cp lab1-test.txt ~/test/

3. Copy YOUR MODIFIED `shell.c' into the test directory by

$ cp shell.c ~/test/

4. Compile your `shell.c' in test directory, you can use makefile if you like, or simply use

$ gcc -g shell.c -o mysh

Now, if there is no error, you can begin to test your small shell, ;-).

$ ./mysh

* Simple Test Cases

---- COMMAND-> mv lab1-test.txt help Results: The `lab1-test.txt' file in the `test' directory should be renamed as `help'. ----

---- COMMAND-> cat help Results: The content of this file should be printed onto the screen. ----

---- COMMAND-> ./mysh Results: Start a second `mysh' shell in the first `mysh' shell. The command prompt should be waiting for user's input. When you press `Ctrl+d', you should go back into your first `mysh', still having a "COMMAND-> " waiting for input. ----

* NOTES

If your shell program can pass the above three test cases, congratulations! You already have a good start point for future labs. Remember, I have more challenging test cases for grading purpose, so before you submit your program, test it with more cases. ;-).

Hint: We usually judge whether a program is good or not by three criteria, o Correctness o Performance (in a real system) o Readability

Therefore, if your program works, try to improve its performance. If its performance is good, add enough comments if you have time to improve the readability.

Thank you. END OF test.txt

CSC 4421 - Computer Operating Systems Lab1-UNIX Shell (Part I) Group Size: 1 , which means you are required to finish this lab assignment by yourself. Goal: This lab helps you understand the concept of processes, how to create, and how to terminate a process. In addition, you are expected to get familiar with system calls related to processes and file operations. Introduction: As the first step of the UNIX Shell project, this lab assignment asks you to build a simple SHELL interface that accepts user commands, creates a child process, and executes the user commands in the child process. The SHELL 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 content of the 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 creates a separate child process that executes the command. Unless otherwise specified, the parent process waits for the child to exit before continuing. This is similar in functionality to what is illustrated in Figure 1. However, UNIX shells typically also allow the child process to run in the background - or concurrently - as well by specifying the ampersand (\&) at the end of the command. By rewriting the above command as sh> cat prog.c \& the parent and child process now run concurrently. The separate 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). A Simple Shell A C program that provides the basic operations of a command line shell is supplied in the file shell.c, which you can download from Canvas. This 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 and setup( ) then invokes exit(). 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 Is 1 at the COMMAND-> prompt, args[0] will be set to the string ls and args[1] will be set to the string -I. (By "string", we mean a null-terminated, C-style string variable.) \#include > \#define MAX_LINE 80 /** setup( reads in the next command line string stored in inputBuffer, separating it into distinct tokens using whitespace as delimiters. setup( modifies the args parameter so that it holds pointers to the null-terminated strings that are the tokens in the most recent user command line as well as a NULL pointer, indicating the end of the argument list, which comes after the string pointers that have been assigned to args. */ void setup (char inputBuffer [], char *args[],int *background) \{ /** full code available in the file shell.c */ \} int main(void) \{ char inputBuffer [MAX_LINE]; / buffer to hold the command entered */ int background; / * equals 1 if a command is followed by ' s/ char *args[MAX_LINE/+1]; /* command line arguments */ while (1)\{ background =0; printf(" 'OMMAND-> n); setup(inputBuffer; args, \&background); /* get next command */ /* the steps are: (1) fork a child process using fork() (2) the child process will invoke execvp() (3) if background ==0, the parent will wait, othenwise returns to the setupe function. */ This lab assignment asks you to create a child process and execute the command entered by a user. To do this, you need to modify the main() function in shell.c so that upon returning from setup(), a child process is forked. After that, the child process executes the command specified by a user. 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 needs to wait for the child to exit or not. Test: We provide several test cases and readme in the file "lab1-test.txt" on Canvas, you need to follow the instructions in the file, and compare your results with the expected output in the file. Submission: Put all your modification in the file "lab1-shell.c" and only submit your revised file to Canvas. At the beginning of the file "lab1-shell.c", you need to add a readme section that tells me your full name, how to compile your file, and how to run your compiled program. You need to make sure your instructions work

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Antipatterns Avoiding The Pitfalls Of Database Programming

Authors: Bill Karwin

1st Edition

1680508989, 978-1680508987

More Books

Students also viewed these Databases questions