In: Computer Science
Can someone please write clear and concise comments explaining what each line of code is doing for this program in C. I just need help tracing the program and understand what its doing. Thanks
#include <stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>
int join(char *com1[], char *com2[]) {
int p[2], status;
switch (fork()) {
case -1:
perror("1st fork
call in join");
exit(3);
case 0:
break;
default:
wait(&status);
return(status);
}
//child process's code
if (pipe(p) == -1){
perror("pipecall in join ");
exit(4);
}
switch (fork()) {
case -1:
perror("2nd fork
call failed in join");
exit(5);
case 0: //the writing process
(grandchild procss)
dup2(p[1], 1);
//redirect stdout to the pipe, writing end.
close(p[0]);
close(p[1]);
execvp(com1[0], com1);
perror("1st
execvp call failed in join");
exit(6);
default: //the "parent" process's
code
dup2(p[0], 0);
//redirect stdin to the pipe, reading end
close(p[0]);
close(p[1]);
execvp(com2[0], com2);
perror("2nd
execvp call failed in join");
exit(7);
}
}
int main (){
char *one[4]={ "ls", "-l", "/usr/lib",NULL};
// char *two[3]={"grep", "^d", NULL};
char *two[3]={"more", NULL};
int ret;
ret=join(one, two);
printf("\n\njoin returned.\n");
exit(0);
}
#include <stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<process.h>
//#include<sys/wait.h>
// function created to join two commands
int join(char *com1[], char *com2[]) {
int p[2], status;
// switch is used according process id which is generated by
fork
// basically it should be switch(pid){}
switch (fork()) {
case -1:
perror("1st fork call in join"); // if fork returns -1 it means
error
exit(3);
case 0:
break; // here code is executed by the first parent
default:
wait(&status); // it is executed by the parent process
return(status);
}
//child process's code
if (pipe(p) == -1){
perror("pipecall in join ");
exit(4);
}
switch (fork()) {
case -1:
perror("2nd fork call failed in join");
exit(5);
case 0: //the writing process (grandchild procss)
dup2(p[1], 1); //redirect stdout to the pipe, writing end.
close(p[0]);
close(p[1]);
execvp(com1[0], com1); /* functions
provide an array of pointers to null-terminated strings that
represent the argument list
available to the new program. The first
argument, by convention, should point to the filename
associated
with the file being executed. The array of
pointers must be terminated by a NULL pointer.*/
perror("1st execvp call failed in join");
exit(6);
default: //the "parent" process's code
dup2(p[0], 0); //redirect stdin to the pipe, reading end
close(p[0]);
close(p[1]);
execvp(com2[0], com2);
perror("2nd execvp call failed in join");
exit(7);
}
}
int main (){
char *one[4]={ "ls", "-l", "/usr/lib",NULL};// char array passed
with command status and path
// char *two[3]={"grep", "^d", NULL};
char *two[3]={"more", NULL}; // second char array woth command as input
int ret;
ret=join(one, two);// join function calling
printf("\n\njoin returned.\n");
exit(0);
}
Explanation : Comments ha added . this program represent the facility of join command in linux or Unix. But here as arguments of execvp() function proper file name ha not provided a the syntax for this function is
int execvp(const char *file, char *const argv[]);
Hence there is a need of some modification in program .