Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is a continuation of my previous question. I will copy the code we had before. I need a couple of things added. I am

This is a continuation of my previous question. I will copy the code we had before. I need a couple of things added.

I am programming in C using VIM and terminal under Ubuntu Linux.

Here is the previous code:

#include #include #include #include #include #include

void main(int argc , char *argv[]) {

// rc is used to store the recieved value of fork int rc;

// variable used to collect status of a program int status;

// pointer to command char *cmd;

// pointers to arguments to command char *myArgv[3];

// if there are three or more arguments like ./program cmd argv1 argv2 ... if (argc >= 3) { cmd = argv[1]; myArgv[ 0 ] = cmd; myArgv[ 1 ] = argv[ 2 ]; myArgv[ 2 ] = NULL; } else { // not enough args we will run ls -al command cmd = "ls"; myArgv[ 0 ] = cmd; myArgv[ 1 ] = "-al"; myArgv[ 2 ] = NULL; }

// Printing the PID of parent process printf("Parent: I am pid %d ", getpid() );

// Here we are forking and creating a child process rc = fork();

if (rc > 0) { // I am the parent

printf("Parent: waiting on pid %d ", rc);

// here we passed the pid of child, and we are waiting till it terminates // the return status is in status variable. waitpid(rc, &status, 0);

if(WIFEXITED(status)) printf("Child's exit code %d ", WEXITSTATUS(status)); else printf("Child did not terminate with exit ");

} else if (rc == 0) { // I am the child

printf("Child: I am pid %d about to exec() %s ", getpid(), cmd); execvp(cmd, myArgv); // control will only return if execvp fails printf("Child: execvp() failed...exiting "); exit(1); } else { // an error occured, no child created errno=EINVAL; perror("Parent: a bad thing happened:"); exit(1); }

return ;

}

I need to have you add a few things. First of all, if someone entered something that was made up like "nosuchprogram" the program should respond with execvp() failed: No such file or directory. It should call perror() and errno needs to be set first. The it should continue. The rest of the program loops.

Any errors from execvp() should be reported with a call to perror()

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

Database Systems For Advanced Applications 9th International Conference Dasfaa 2004 Jeju Island Korea March 2004 Proceedings Lncs 2973

Authors: YoonJoon Lee ,Jianzhong Li ,Kyu-Young Whang

2004th Edition

3540210474, 978-3540210474

More Books

Students also viewed these Databases questions

Question

What is the rationale behind state antitakeover legislation?

Answered: 1 week ago