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

535 Answers

Hire Me
expert
Muhammad Ali HaiderFinance

584 Answers

Hire Me
expert
Husnain SaeedComputer science

765 Answers

Hire Me
expert
Atharva PatilComputer science

607 Answers

Hire Me
April
January
February
March
April
May
June
July
August
September
October
November
December
2025
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
SunMonTueWedThuFriSat
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
00:00
00:30
01:00
01:30
02:00
02:30
03:00
03:30
04:00
04:30
05:00
05:30
06:00
06:30
07:00
07:30
08:00
08:30
09:00
09:30
10:00
10:30
11:00
11:30
12:00
12:30
13:00
13:30
14:00
14:30
15:00
15:30
16:00
16:30
17:00
17:30
18:00
18:30
19:00
19:30
20:00
20:30
21:00
21:30
22:00
22:30
23:00
23:30