C programming

Wernerrab
Pipelining.txt

// exit // one option CMDTYPE getCmdType(const char *cmd) { if (strcmp(cmd, "exit") == 0) return EXIT; if (strcmp(cmd, "cd") == 0) return CD; if (strcmp(cmd, "pwd") == 0) return PWD; if (strcmp(cmd, "echo") == 0) return ECHO; return CMD; } // second option void exit() { if('$1' == 'exit') { $2 $3; $1; } else if('$3' == 'exit' ) { $1 $2; $2; } } // pipelining void pipes(int a, char * pipes[100], int size) { char* p1[10] // variable for command 1 char* p2[10] // variable for command 2 char* p3[10] // variable for command 3 int num = 0; // integer for amount of pipes int n1 = 0; // interger for command 1 array int n2 = 0; // integer for command 2 array int n3 = 0; // integer for command 3 array int status; // for status pid_t pd, pd2, pd3; //variables for pid to be forked for (int i=0; i<size; i++) //size of command line input { if(strcmp(pipes[i], "|") == 0) // if the pipe is found { num++; //increment the counter } else if(num == 0) // if no pipe found { p1[n1] = pipes[i]; //place the command and argument into p1 n1++; // incrementing place in command 1 array } else if(num == 1) // if 1 pipe is found { p2[n2] = pipes[i]; //place the command and argumemnt into p2 n2++; //increment place in command 2 array } else if (num == 2) // if 2 pipes are found { p3[n3] = pipes[i]; //place the command and argumemnt into p3 n3++; //increment place in command 3 array } } // end of for loop p1[n1] = NULL; //setting the place in the array to NULL p2[n2] = NULL; //setting the place in the array to NULL p3[n3] = NULL; //setting the place in the array to NULL int dir1[2], dir2[2]; // integers for directing pipe(dir1); //pipe pipe(dir2); //pipe pd = fork(); // first fork for command 1 if(pd == 0) { dup2(dir1[1] , 1); // duplicate write end of p1->p2 pipe stdout close(dir1[0]); close(dir1[1]); close(dir2[0]); close(dir2[1]); execvp(p1[0], p1); //execute command p1 } else if(pd>0) //the parent { pd2 = fork(); // fork second chil to execute grep if(pd2 == 0) { dup2(dir1[0], 0); // duplicate read end of p1->p2 to stdin of p2 if(a == 2) // if second pipe was found { dup2(dir2[1], 1); } //close bothe ends of all created dir pipes close(dir1[0]); close(dir1[1]); close(dir2[0]); close(dir2[1]); execvp(p2[0], p2); //execute command p2 } else if(pd2 > 0) { if(a == 2) { pd3 = fork(); if(pd3 == 0) { dup2(dir2[0], 0); //close bothe ends of all created dir pipes close(dir1[0]); close(dir1[1]); close(dir2[0]); close(dir2[1]); execvp(p3[0], p3); //execute command p3 } else if(pd3<0) // if error { perror("FORK FAILURE"); } } else { wait( (int *)0); } } esle { perror("FORK FAILURE"); } } // end of else if(pd>0) else { perror("FORK FAILURE"); } // only the parent gets here and close all the pipes and wait for the 3 children to finish close(dir1[0]); close(dir1[1]); close(dir2[0]); close(dir2[1]); for (int i = 0; i<(a+1); i++) { wait(&status); // waiting for the status } } // end of void pipelining