Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using C++ to build a Microshell. Here is the requirement of my program and thnak you in advance. Write a Linux program in C++ to

Using C++ to build a Microshell.

Here is the requirement of my program and thnak you in advance.

Write a Linux program in C++ to build a microshell using fork, exec, pipe and dup.

Your shell should do the following:

1. Print the prompt myshell> and wait for your input;

2. Execute the command you type in after the prompt and print a new prompt.

3. The shell understands the commands quit and q as the special commands to exit.

4. The shell understands a special symbol ||, by which you can pipe the output of one command to the next command. To keep things simple, this assignment only requires one pipe between two commands, e.g., cat myfile || sort. It does not require multiple pipes as a full Unix shell would. Note that the standard Unix/Linux pipe is |, which is different from what your microshell will understand.

Input file:

A text file with the name myfile.txt is required to produce the sample output. Its contents are:

Illinois

USA

DeKalb

Sample output:

hopper%>./hw3.exe

myshell>less myfile.txt || sort

DeKalb

Illinois

USA

myshell>quit

hopper%>./hw3.exe

myshell>less myfile.txt || grep DeKalb

DeKalb

myshell>ls

(same output as from running this command in the regular shell)

myshell>ls -Als || less

(same output as from running this command in the regular shell)

myshell>xyz

(your error message including the erroneous command xyz)

myshell>ls -Als || xyz

(your error message including the erroneous command xyz)

myshell>q

Your TA may use different commands or a different file to test your program.

Testing suggestions:

Write your program in the following order. Test each iteration before you go on to the next. Your TA has been instructed not to discuss later sections with you until you have completed the earlier sections correctly.

1. First, make sure that your shell can read the input correctly.

2. Second, write and test the execution of simple commands without a pipe.

3. Finally, add the pipe processing.

Programming suggestions:

A. Here is a basic outline.

Loop:

Read input.

Check for quitting.

Tokenize input.

Fork a new process, or two if input contains a pipe (and check for fork error).

If the input contains a pipe, set up pipe and dup.

In the child/children:

Do execvp (or a different version of exec) in the child.

If execvp fails, print couldnt execute and the command that failed, then

exit(127).

In the parent:

Do waitpid on the child/children.

If waitpid fails, print waitpid error.

Exit(0).

B. How to implement this outline

1. Use istringstream() to parse the command line.

2. Remember that execvp() takes an array of pointers to the arguments, so declare the array.

3. The parent process needs to call waitpid() to wait for the completion of the command(s).

4. Use pipe() with the help of dup() to set up the communication between the child processes. The system call dup() is used to duplicate a file descriptor so that you can replace the standard input or output of a process by the file descriptors of a pipe.

Dup() is not needed when you are writing your own code, e.g., for communicating between parent and child, because your own code can write directly to the virtual file represented by the pipe. When you are executing a system program such as ls that is expecting to write to standard output, dup() is needed to get ls to write to the pipe instead of standard output.

5. You need to close all the unneeded file descriptors of the pipes. You need to close the two pipe file descriptors in the parent process, the read end of the pipe for the first child process, and the write end of the pipe for the second child process. After dup() is called in each child process, you can close the write end for the first process and the read end for the second process since they are no longer needed.

C. Technical details for pipe()

You need to declare a pair of file descriptors and then call pipe().

Example:

int pfd[2]; // array of file descriptors

if (pipe(pfd) == -1) // error handling { ... }

Normally a process that calls pipe() will then call fork(), creating an IPC (interprocess communication) channel.

D. Technical details for dup()

A dup() call allocates the next free entry in the file descriptor table and copies the contents of the entry being duplicated.

Processes use file descriptor 0 as the standard input and file descriptor 1 as the standard output. If we do the following in a process:

close(1);

dup(pfd[1]);

then a pointer to the write end of the pipe is copied into entry 1 (i.e., the first available entry). In other words, the standard output of the process is now the write end of the pipe.

You can now close the original file descriptor for the write end of the pipe with

close(pfd[1]);

If you only plan to use one of the two file descriptors for the pipe, close the other one.

E. Technical details for execvp()

Execvp() is one of the six exec functions. The syntax of execvp is:

int execvp(const char *filename, char *const argv[]);

You will need to build the argv[] array.

Your call could be something like this:

execvp(cmd_args[0], cmd_args);

where cmd_args[0] contains the name of the command. Further entries in cmd_args contain the arguments followed by a NULL, i.e., a binary 0.

The execvp function replaces your program with a new program. (The system function runs a new program, then continues running your program, so you should not use system here.)

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

Genetic Databases

Authors: Martin J. Bishop

1st Edition

0121016250, 978-0121016258

More Books

Students also viewed these Databases questions

Question

1. Who will you assemble on the team?

Answered: 1 week ago

Question

Did the team members feel that their work mattered

Answered: 1 week ago