Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement in C++ - Operating System Use fork and one of the exec (e.g. execv, execvp) calls to issue the command and wait until the

Implement in C++ - Operating System

Use fork and one of the exec (e.g. execv, execvp) calls to issue the command and waituntil the command is completed. [NOTE: To see how these work, type man 3 exec or man 3 wait. The man pages also tell you what header files to include.] If a -1 is returned from fork or exec, an error occurred. Use perror to print the command, error and exit. Print out the arguments in a for-loop before forking and executing the command. Print out the process id of the parent and child. You have to use command-line arguments. In the past, int main() had an empty parameter list. There is another version that allows programs to access command-line arguments (calling a program with options on the command line). This is int main(int argc, char* argv[]).

Expected Output:

[a@edu lab1]$ lab1 ls -l lab1 ls -l Child process pid is 6203 total 30 -rwxr-xr-x 1 a staff 8576 Sep 13 09:24 lab1 -rw-r--r-- 1 a staff 636 Sep 13 09:22 lab1.cpp -rw-r--r-- 1 a staff 11190 Sep 13 09:15 lab1.html -rw-r--r-- 1 a staff 523 Sep 13 08:59 linux.txt lab1 ls -l Parent process pid is 6202 
[a@edu cs3595]$ lab1 lss -l lab1 lss -l Child process pid is 7296 lss: No such file or directory lab1 lss -l Parent process pid is 7295

What I have so far:

#include #include

#include #include

#include

using namespace std;

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

{ for (int i=0; i

{ cout<

pid_t pid = fork();

if (pid <0) //error occurred

{ perror("No such file or directory");

return 1; }

else if (pid == 0) //child process{

printf("Child process pid is (%d) ", getpid());

execvp("ls", argv);

//printf("Child process pid is (%d) ", getpid()); }

else { //parent process

wait(NULL);

for(int i=0; i

{ cout<

printf("Parent process pid is (%d) ", getppid()); }

I have an error with execvp cannot access "ls" when doing ls -l

My output:

[l@edu lab1]$ lab1 ls -1

lab1 ls -1 Child process pid is (1072581)

lab1: cannot access 'ls': No such file or directory

lab1 ls -1 lab1 ls -1 Parent process pid is (1065733)

I cannot access the file or directory when doing ls -l. I don't know what's the problem. Help!

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

Datacasting How To Stream Databases Over The Internet

Authors: Jessica Keyes

1st Edition

007034678X, 978-0070346789

More Books

Students also viewed these Databases questions

Question

What is paper chromatography?

Answered: 1 week ago

Question

Explain the cost of capital.

Answered: 1 week ago