Question
1. Carefully study the examples provided fork.c, myls.c, myshell.c, etc. You need to know how they work in order for you to finish this assignment;
1. Carefully study the examples provided fork.c, myls.c, myshell.c, etc. You need to know how they work in order for you to finish this assignment;
2. Write a program called minishell that creates two child processes: one to execute 'ls' and the other to execute 'sort'. After the forks, the original parent process waits for both child processes to finish before it terminates. The standard output of 'ls' process should be piped to the input to the 'sort' process. Make sure you close the unnecessary open files for the three processes.
**i found this answer on here, but i want to know how to test this, to see if it is even correct, and to understand what the code is doing, because when i run this all that is outputed is
"Parent waiting for childdren to finish"
"Child2 excuting sort"
and the program hangs here
#include
int main() { int pipe1[2],pid1,pid2; pipe(pipe1); pid1 = fork(); if (pid1 == 0) { close(1); dup(pipe1[1]); close(pipe1[0]); close(pipe1[1]); printf("child1 executing ls "); execl("/bin/ls", "ls", 0, 0); perror("execl error"); } else { wait(&pid1); //wait for children pid2 = fork();
if (pid2 == 0) { close(0); dup(pipe1[0]); close(pipe1[0]); close(pipe1[1]); printf("child2 executing sort "); execl("/bin/sort", "sort", 0, 0); perror("execl error"); } else { /* Parent Code */ printf("Parent waiting for children to finish "); wait(0); //wait for children } } }
these are the codes to be familar with
fork.c
#include
int main() { pid_t pid;
pid = fork(); if(pid < 0) { /* an error occurred */ perror("fork failed"); exit(-1); } else if(pid == 0) { /* child process */ printf("I AM THE CHILD PROCESS... sleeping for 30 seconds "); sleep(30); printf("CHILD FINISHES "); } else { /* parent process */ printf("I AM THE PARENT PROCESS... sleeping for 60 seconds "); sleep(60); printf("PARENT FINISHES "); } return 0; }
myls.c
#include
int main() { pid_t pid;
pid = fork(); if(pid < 0) { /* an error occurred */ perror("fork failed"); exit(-1); } else if(pid == 0) { /* child process */ printf("I AM A CHILD PROCESS "); execlp("/bin/ls", "ls", "-l", NULL); /* return only when exec fails */ perror("exec failed"); exit(-1); } else { /* parent process */ wait(0); printf("PARENT PROCESS FOUND CHILD PROCESS COMPLETED "); } return 0; }
myshell.c
#include
#define MAX_ARGS 20 #define BUFSIZ 1024
int get_args(char* cmdline, char* args[]) { int i = 0;
/* if no args */ if((args[0] = strtok(cmdline, " \t ")) == NULL) return 0;
while((args[++i] = strtok(NULL, " \t ")) != NULL) { if(i >= MAX_ARGS) { printf("Too many arguments! "); exit(1); } } /* the last one is always NULL */ return i; }
void execute(char* cmdline) { int pid, async; char* args[MAX_ARGS];
int nargs = get_args(cmdline, args); if(nargs <= 0) return;
if(!strcmp(args[0], "quit") || !strcmp(args[0], "exit")) { exit(0); }
/* check if async call */ if(!strcmp(args[nargs-1], "&")) { async = 1; args[--nargs] = 0; } else async = 0;
pid = fork(); if(pid == 0) { /* child process */ execvp(args[0], args); /* return only when exec fails */ perror("exec failed"); exit(-1); } else if(pid > 0) { /* parent process */ if(!async) waitpid(pid, NULL, 0); else printf("this is an async call "); } else { /* error occurred */ perror("fork failed"); exit(1); } }
int main (int argc, char* argv []) { char cmdline[BUFSIZ]; for(;;) { printf("COP4338$ "); if(fgets(cmdline, BUFSIZ, stdin) == NULL) { perror("fgets failed"); exit(1); } execute(cmdline) ; } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started