Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void execNodes(node_t* root, node_t *n) {
- // if node has no children, run leafcounter, file is Output_...
- // else, fork child processes and run aggregate_votes
- char** args = malloc(sizeof(char)*1024);
- if (root->num_children == 0) {
- for (int i = 0; i < num_names + 3; i++) {
- if (i == 0) {
- args[i] = "leafcounter";
- } else if (i == 1) {
- args[i] = root->name;
- } else if (i == 2) {
- char str[100];
- strcpy(str, "Output_");
- strcat(str, root->name);
- args[i] = str;
- } else {
- args[i] = candidate_information[i - 3];
- }
- }
- /*
- for (int i = 0; i < num_names + 3; i++) {
- printf("%s\n", args[i]);
- }
- */
- execv("leafcounter", args);
- } else {
- for (int i = 0; i < root->num_children; i++) {
- pid_t pid = fork();
- if (pid == 0) {
- // child process (call execNodes on child)
- node_t* child = findNodeByID(n, root->children[i]);
- printf("%s\n", child->name);
- execNodes(child, n);
- } else if (pid > 0) {
- // parent process(aggregate_votes on children)
- int status;
- waitpid(pid, &status, 0);
- } else {
- printf("mission failed we'll get em next time\n");
- exit(1);
- }
- }
- for (int i = 0; i < (root->num_children + num_names + 3); i++) {
- if (i == 0) {
- args[i] = "aggregate_votes";
- } else if (i == 1) {
- char* str = malloc(sizeof(char)*100);
- sprintf(str, "%d", root->num_children);
- args[i] = str;
- } else if (i >= 2 && i < (root->num_children + 2)) {
- char* str = malloc(sizeof(char)*100);
- node_t* child = findNodeByID(n, root->children[i - 2]);
- strcpy(str, "Output_");
- strcat(str, child->name);
- args[i] = str;
- } else if (i >= (root->num_children + 2) && i < (root->num_children + 3)) {
- char* str = malloc(sizeof(char)*100);
- strcpy(str, "Output_");
- strcat(str, root->name);
- args[i] = str;
- } else {
- args[i] = candidate_information[i - (root->num_children + 3)];
- }
- }
- /*
- for (int i = 0; i < root->num_children + num_names + 3; i++) {
- printf("%s\n", args[i]);
- }
- */
- if (strcmp(root->name, "Who_Won") == 0) {
- args[0] = "find_winner";
- execv("find_winner", args);
- } else {
- execv("aggregate_votes", args);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement