Answer to Question #315411 in C for annie

Question #315411

 Write a program that receives one command line argument: the name  of a text file. Your program will work as follows:  

 Start out by creating a child process.  

 The child process calls exec to run cat command with command line argument that  the parent received. 




1
Expert's answer
2022-03-21T16:10:08-0400
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>

int main(int argc, char* argv[]) {
    pid_t pid;

    if ((pid = fork()) < 0) {
        fprintf(stderr, "Error in fork\n");
    }
    else if (pid == 0) {
        execlp("cat", "cat", argv[1], NULL);
    }
    else {
        waitpid(pid, NULL, 0);
    }

    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS