Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please put your code and makefile to compile. Thanks. Would appreciate if could be done by 11 AM Central, 3/6/17 Part 2. C/C++ program Handling

Please put your code and makefile to compile. Thanks. Would appreciate if could be done by 11 AM Central, 3/6/17

Part 2. C/C++ program

Handling File Redirection (< input, > output, >> output-append).

Design and implement C/C++ program (myfile.c) in loop: (1) to prompt for a command, (2) to read a command (from keyboard as you type), (3) to echo the command, (4) to fork a child process to process command while the parent process is waiting for child process to complete this part, and then (5) the parent is back to the command prompt for next command.

The commands are: read, write, and append, and exit. The following tasks (that is, test cases) are to be implemented.

Task1. When the command is "exit", then the program terminates with exit(0).

Task2. The "read" command will do a file-redirection for a file to be read and to be written to a temporary file. For example, "read < hello.c" will read a file "hello.c" and write it to a temporary file ("temp.txt").

Task3. The "write" command will do a file-redirection for a file ("temp.txt") to be read and written to a file. For example, "write > out1.c" will read the save temp file "temp.txt" and write it to the file ("out1.c").

Task4. The "append" command has a file-redirection for a file (temp.txt) to be appended to a file. For example, "append >> out2.c" will read the save temp file "temp.txt" and write it to the file ("out2.c").

Task5. You may have two commands with a pipe. For example, "read < hello.c | write > hello2.c" will read hello.c file and write it to hello2.c via temp.txt file.

Hint. You may use the following code as your basis for this part.

#include

#include

#include

#include

#include

#include

#include

#include

char *getinput(char *buffer, size_t buflen) {

printf("$$ ");

return fgets(buffer, buflen, stdin);

}

int main(int argc, char **argv) {

char buf[1024];

pid_t pid;

int status;

while (getinput(buf, sizeof(buf))) {

buf[strlen(buf) - 1] = '\0';

/* Place your code to check "exit". If so, then exit */

if((pid=fork()) == -1) {

fprintf(stderr, "shell: can't fork: %s ", strerror(errno));

continue;

} else if (pid == 0) {

/* child process to do each command

place your code here to handle read, write, append */

exit(EX_OK);

}

if ((pid=waitpid(pid, &status, 0)) < 0)

fprintf(stderr, "shell: waitpid error: %s ",

strerror(errno));

}

exit(EX_OK);

}

You may read more on fork() in APUE 8.3, on system() call in APUE 10.18, on shell in APUE 9.9

** Copy and paste your program code here, Makefile to compile, and the program-run results (screenshots, session log, or the part of MobaXterm "save terminal text" file to show your program runs for each test case). Submit (1) this document and (2) a zip file containing all the files including source, executable, Makefile, etc.

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2018 Dublin Ireland September 10 14 2018 Proceedings Part 1 Lnai 11051

Authors: Michele Berlingerio ,Francesco Bonchi ,Thomas Gartner ,Neil Hurley ,Georgiana Ifrim

1st Edition

3030109240, 978-3030109240

More Books

Students also viewed these Databases questions

Question

What is PE ratio?

Answered: 1 week ago