Question
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.
START OF shell.c
#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 entersStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started