void pipeRun(char * s, int totalPipes, char ** prePipe, char ** postPipe, int prePipeCount, int postPipeCount){ int fd[2]; pid_t pid1; pid_t pid2; parsePipe(totalPipes, s, &prePipe, &prePipeCount, &postPipe, &postPipeCount); #if TESTER printf("------->DEBUG: in main prePipeCount = %d<------\n", prePipeCount); printf("------->DEBUG: in main postPipeCount = %d<------\n", postPipeCount); printargs(prePipeCount, prePipe); printargs(postPipeCount, postPipe); #endif if( (pid1 = fork()) == 0){ //in child 1 pipe(fd); if( (pid2 = fork()) == 0){ //child 2 close(fd[0]); close(1); close(2); dup2(fd[1], 1); close(fd[1]); //run first command execvp(prePipe[0],prePipe); exit(-1); } //end if waitpid(pid2, NULL, 0); close(fd[1]); // stop write end of pipe close(0); dup2(fd[0],0); close(fd[0]); //run 2nd command execvp(postPipe[0], postPipe); exit(-1); }//end if waitpid(pid1, NULL,0); clean(prePipeCount, prePipe); clean(postPipeCount, postPipe); }
int parseArgs(int argc, const char **argv) { int i; int required_args = 5; // voter name, controller name, rep_type, timeout and priority controller_name = (char*) (argv[1]); rep_type = reptypeToEnum((char*)(argv[2])); rep_count = reptypeToCount(rep_type); voting_timeout = atoi(argv[3]); voter_priority = atoi(argv[4]); if (voting_timeout == 0) { voting_timeout = PERIOD_USEC; } if (argc < required_args) { puts("Usage: VoterD <controller_name> <rep_type> <timeout> <priority> <fd_in:fd_out:time> <...>"); return -1; } else { for (i = 0; (i < argc - required_args && i < PIPE_LIMIT); i++) { parsePipe(argv[i + required_args], &ext_pipes[pipe_count]); // TODO: WRONG! Maybe. Should ignore non-numbers to deserialize? pipe_count++; } if (pipe_count >= PIPE_LIMIT) { printf("VoterD: Raise pipe limit.\n"); } // Need to have a similar setup to associate vote pipe with input? for (i = 0; i < pipe_count; i++) { if (ext_pipes[i].timed) { if (ext_pipes[i].fd_in != 0) { timer_start_index = i; } else { timer_stop_index = i; } } } } return 0; }