logo Use CA10RAM to get 10%* Discount.
Order Nowlogo
(5/5)

Understand basic process-related Unix system calls and use them for a basic shell implementation.

INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS

Implement a Basic Shell

Objective

Understand basic process-related Unix system calls and use them for a basic shell implementation.

Specification

 

This basic shell is a command line interpreter that accepts input from the user and executes the commands. Similar to the well-known shells such as bash or tcsh, this shell can execute commands, redirect the standard input or standard output of commands to files, pipe the output of commands to other commands.

 

When the shell is ready to accept commands, a user can type commands. Commands are alphanumeric tokens (e.g., ls, ps, cat) that represent programs that should be executed. This shell should search for these programs in the directories determined by the PATH environment variable. Commands can have arguments. Thus, tokens that follow a command (separated by white space) are treated as the arguments to this command (e.g., cat x).

 

In addition to execute commands with their arguments, your shell supports the standard Unix I/O redirection meta-characters with '<' and '>', command pipeline with '|' and a simple signal handling.

 

More details:

 

Detailed Specifications on I/O redirection and pipeline

 

In addition to commands and their arguments, your shell understand the following meta-characters: '<', '>', '|'.

 

The meta-character '<' must be followed by a token that represents a file name. It indicates that the command before this meta-character reads its input from this file (instead of stdin of the shell).

Thus, the meta-character '<' must follow the name of a command. For example, cat < file.

 

The meta-character '>' must be followed by a token that represents a file name. It indicates that the command before this meta-character writes its output to this file (instead of stdout of the shell).

Thus, the meta-character '>' must follow the name of a command. For example, ls > file.

 

The meta-character '|' (i.e., pipe sign) allows multiple commands to be connected. When the shell encounters the '|' character, the output of the command before the pipe sign must be connected to the input of the command after the pipe sign. This requires that there is a valid command before and after the pipe. Also, note that there can be multiple pipe signs on the command line. For example, your shell has to be able to process an input such as cat f | sort | wc. With this command, the output of the cat command is redirected to the input of sort, which in turn sends its output to the input of the wc program. With regard to white spaces separating the meta-character from the commands, the same rules as above apply.

In case of errors (e.g., invalid input, command not found, ...) your shell should display an error and wait for the next input.

To exit the shell, the user may type "exit" or Ctrl-C. The shell should also exit if it encounters the End-of-File from the input stream and no characters have been read.

Your shell is supposed to collect the exit codes of all processes that it spawns.

 

Simplification and Restriction

 You may assume there is a white space separating all tokens. There are at most 3 commands connected by pipe '|'.

 

For each command (or a sequence of commands connected by a pipe), you may assume that there is at most one input redirection, and at most one output redirection. When they appear, input redirection appears before output redirection. For example, "cat < temp1 > temp2" and "cat < temp1 | sort > temp2" are valid, but "cat > temp2 < temp1" and "cat < temp2 < temp1" are not valid.

 

You may assume that the maximum length of individual tokens never exceeds 32 characters, and that the maximum length of an input line never exceeds 512 characters.

 

How to Test

 

You may test your program to run a sequence of commands using "shell < testfile'' and an example of this test file is:

 

ls > temp

cat < inputfile | sort | wc > temp cat < temp > temp1

rm temp

ls | sort | uniq exit

Handling Errors

 

When a call to exec()fails, you should call perror on the name of the binary which failed to execute. This will print the executable name, along with the relevant error message.

 

char *argv[] = {"bad_file", NULL}; execvp(argv[0], argv);

/* everything below this line will only execute if perror fails */ perror(argv[0]);

 

This will output the following:

bad_file: No such file or directory

What to Submit

Your shell implementation should use the fork() system call and the execvp() system call (or one of its variants) to execute commands, and dup2() for I/O pipes and redirection. It should also

use waitpid() or wait() to wait for a program to complete execution. You might also find the documentation for signals useful to be able to collect the status of processes that exit when running in the background.

 

Hints and Suggestions:

 

  1. Here is a a quick guide on C UNIX/Linux processes and I/O redirection. A simple shell needs to figure out what the user is trying to do. To read a line from the user, you may use fgets(3). If a valid command has been entered, the shell should fork to create a new (child) process, and the child process should exec the command.

  2. Before calling exec to begin execution, the child process may have to close stdin (file descriptor 0) or stdout (file descriptor 1), open the corresponding file or pipe (with open for files, and pipe for pipes), and use dup2(2) to make it the appropriate file descriptor. After calling dup2, close the old file

  3. The main challenge of calling execvp is to build the argument list correctly. If you

use execvp, remember that the first argument in the array is the name of the command itself, and the last argument must be a null pointer.

  1. The easiest way to redirect input and output is to follow these steps in order: (a) open (or create) the input or output file (or pipe). (b) close the corresponding standard file descriptor (stdin or stdout). (c) use dup2 to make file descriptor 0 or 1 correspond to your newly opened file. (d) close the newly opened file (without closing the standard file descriptor).

  2. When executing a command line that requires a pipe, the pipe must be created before forking the child processes. Also, if there are multiple pipes, the command(s) in the middle may have both input and output redirected to pipes. Finally, be sure the pipe is closed in the parent process, so that termination of the process writing to the pipe will automatically close the pipe and send an EOF (end of file) to the process reading.

(5/5)
Attachments:

Related Questions

. Introgramming & Unix Fall 2018, CRN 44882, Oakland University Homework Assignment 6 - Using Arrays and Functions in C

DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma

. The standard path finding involves finding the (shortest) path from an origin to a destination, typically on a map. This is an

Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t

. Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual

Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th

. SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

. Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Um e HaniScience

541 Answers

Hire Me
expert
Muhammad Ali HaiderFinance

864 Answers

Hire Me
expert
Husnain SaeedComputer science

737 Answers

Hire Me
expert
Atharva PatilComputer science

883 Answers

Hire Me