logo Use SA10RAM to get 10%* Discount.
Order Now logo

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

expert
Pam AnnEngineering
(5/5)

880 Answers

Hire Me
expert
Brady GonzalesEnglish
(5/5)

803 Answers

Hire Me
expert
Akhil SachdevaFinance
(5/5)

696 Answers

Hire Me
expert
Dolll JuttLaw
(4/5)

935 Answers

Hire Me
C Programming

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.

Related Questions

. The fundamental operations of create, read, update, and delete (CRUD) in either Python or Java

CS 340 Milestone One Guidelines and Rubric  Overview: For this assignment, you will implement the fundamental operations of create, read, update,

. 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

Retail Transaction Programming Project  Project Requirements:  Develop a program to emulate a purchase transaction at a retail store. This

. The following program contains five errors. Identify the errors and fix them

7COM1028   Secure Systems Programming   Referral Coursework: Secure

. Accepts the following from a user: Item Name Item Quantity Item Price Allows the user to create a file to store the sales receipt contents

Create a GUI program that:Accepts the following from a user:Item NameItem QuantityItem PriceAllows the user to create a file to store the sales receip

. The final project will encompass developing a web service using a software stack and implementing an industry-standard interface. Regardless of whether you choose to pursue application development goals as a pure developer or as a software engineer

CS 340 Final Project Guidelines and Rubric  Overview The final project will encompass developing a web service using a software stack and impleme

Get Free Quote!

324 Experts Online