So for this program I am asked to input the names of several programs that will be used in each child processes that will link up to pipes
Create pipes, create childs, parent closes pipe ends and wait for childs to finish
I am sure the fork,child, and pipe are fun but if you want to critique go ahead but the focus is mainly on the exec.
However this is just a small part of my code that I have issue with before creating several more childs and modifying the pipes. I came across an error in my exec that I wanted to resolve before continuing
myParent
int main(int argc, char* argv[]){
int i = 0;
int max_line_length = 101;
char * catprog = (char*) malloc(sizeof(char*));;
char * datafile = (char*) malloc(sizeof(char*));
char * numstrings = (char*) malloc(sizeof(char*));
int nstrings;
if(argc != 1){
printf("%s \n", "This takes no command line arguments");
exit(EXIT_FAILURE);
}
printf("%s ","Filename of spliter program:");
fgets(catprog, max_line_length, stdin);
printf("%s ","File name of data file:");
fgets(datafile, max_line_length, stdin);
printf("%s ","Number of strings to sort:");
fgets(numstrings, max_line_length, stdin);
if(!(nstrings = atoi(numstrings))){
printf("%s \n","This is an invalid number.");
exit(EXIT_FAILURE);}
int halfnstrings = nstrings/2;
pid_t pid1 = fork();
if(pid1 < 0){ //failed fork1
fprintf(stderr, "Fork 1 failed\n");
exit(EXIT_FAILURE);
}
else if(pid1 == 0){//child 1
char start[max_line_length];
char end[max_line_length];
sprintf(start, "%d", 1);
sprintf(end, "%d", halfnstrings);
printf("%s",catprog); //checking if catprog is correct
printf("%s",datafile); //checlomg of datafile
if((execlp(catprog, catprog, datafile, start, end, NULL)) == -1){
printf("%s \n","Cannot exec");
exit(EXIT_FAILURE);
}
_exit(EXIT_SUCCESS);
}
else{
wait(NULL);
exit(EXIT_SUCCESS);
}
return 0;
}
myCat
int main(int argc, char * argv[]){
int startline;
int endline;
int max_line_length = 101;
if(argc != 4){
printf("%s \n", "Format is: myCat <data_file> <start_line> <end_line>");
exit(EXIT_FAILURE);
}
if(!(startline = atoi(argv[2]))){
printf("%s \n","Invalid Number");
exit(EXIT_FAILURE);
}
endline=atoi(argv[3]);
if(!(endline) || endline<startline){
printf("%s \n","Invalid Number");
exit(EXIT_FAILURE);
}
path = argv[1];
if(NULL == (pdata=fopen(path,"r"))){
printf("%s \n", "Cannot open file.");
exit(EXIT_FAILURE);
}
So when I compile this and run it prints out cannot exec hence I believe that i did not input exec properly.
The inputs are:
split program is: myCat
merge program is: myMerge
data file: data.txt
num of strings: whatever number of strings to sort
myCat works by taking the data file, start line, and end line: myCat data.txt 2 7
myCat takes data file and from the start line reads it until it reaches the end line and outputs the string lines read
So my question is does white space affect execl?
execl(); each argument is separate, and does not need white space to separate it. You code is not closing anywhere near enough pipe file descriptors. The parent will need to close them all after launching the children. Each child will need to close them all after hooking (duplicating) its standard input or standard output or both to the relevant pipe descriptors.myCatso we have to invent something to run. It won't be the first time. I can useal— argument list. It'll list its arguments, one per line. Since you're not plumbing your pipes at all, the standard output of the parent will receive the standard output of the child, and the pipes are wholly irrelevant. Where do you print all the data? Did you remember to remove newlines? DoesmyCatwork OK with blanks in arguments?char * catprog = (char*) malloc(sizeof(char*));;has allocated 4 or 8 bytes, but you tellfgets()you allocated101bytes. You're lying to your compiler; rest assured it will find a way to get its own back. Also, when debugging, you can't afford to ignore the information fromwait(). You need to useint corpse; int status; while ((corpse = wait(&status)) > 0) printf("PID %d status 0x%.4X\n", corpse, status);to see what happens with your children.