Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi I\'m working on c system calls involving pipes, fork, and exec. I fork()\'d a

ID: 3536038 • Letter: H

Question

Hi I'm working on c system calls involving pipes, fork, and exec.

I fork()'d a child process and created pipes between them and am able to send argument argv[1] to the child. I want the child to take that filename provided from argv[1] and perform an execl("/bin/cat","cat",(char *) 0);

How do I route the filename piped to the child to the execl?

Enclose is my code for clearity :

#include <unistd.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>


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

int fds[2];

char buffer[100];

int status;


if(pipe(fds) == -1){

perror("pipe creation failed");

exit(EXIT_FAILURE);

}


switch (fork()){


case 0://child

close(fds[1]); //close stdout so can only do stdin

read(fds[0],buffer,strlen(argv[1]));

printf("from parent: %s ",argv[1]);

execl("/bin/cat","cat",(char*)0);

perror("cat failed");

exit(20);

break;


case -1: //fork failure

perror("fork failure");

exit(EXIT_FAILURE);


default: //parent

close(fds[0]); //close stdin so only can do stdout

write(fds[1],argv[1], strlen(argv[1]));

}

return (EXIT_SUCCESS);

}


Explanation / Answer

ur program is correct. BUT in execl("/bin/cat","cat",(char*)0); u miss to pass the file name.

u shd write execl("/bin/cat","cat",buffer,(char*)0); the content of the fill will be display in terminal.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote