Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. void execNodes(node_t* root, node_t *n) {
  2. // if node has no children, run leafcounter, file is Output_...
  3. // else, fork child processes and run aggregate_votes
  4. char** args = malloc(sizeof(char)*1024);
  5.  
  6. if (root->num_children == 0) {
  7. for (int i = 0; i < num_names + 3; i++) {
  8. if (i == 0) {
  9. args[i] = "leafcounter";
  10. } else if (i == 1) {
  11. args[i] = root->name;
  12. } else if (i == 2) {
  13. char str[100];
  14. strcpy(str, "Output_");
  15. strcat(str, root->name);
  16. args[i] = str;
  17. } else {
  18. args[i] = candidate_information[i - 3];
  19. }
  20. }
  21.  
  22. /*
  23. for (int i = 0; i < num_names + 3; i++) {
  24. printf("%s\n", args[i]);
  25. }
  26. */
  27.  
  28. execv("leafcounter", args);
  29. } else {
  30. for (int i = 0; i < root->num_children; i++) {
  31. pid_t pid = fork();
  32.  
  33. if (pid == 0) {
  34. // child process (call execNodes on child)
  35. node_t* child = findNodeByID(n, root->children[i]);
  36. printf("%s\n", child->name);
  37. execNodes(child, n);
  38. } else if (pid > 0) {
  39. // parent process(aggregate_votes on children)
  40. int status;
  41. waitpid(pid, &status, 0);
  42. } else {
  43. printf("mission failed we'll get em next time\n");
  44. exit(1);
  45. }
  46. }
  47.  
  48. for (int i = 0; i < (root->num_children + num_names + 3); i++) {
  49. if (i == 0) {
  50. args[i] = "aggregate_votes";
  51. } else if (i == 1) {
  52. char* str = malloc(sizeof(char)*100);
  53. sprintf(str, "%d", root->num_children);
  54. args[i] = str;
  55. } else if (i >= 2 && i < (root->num_children + 2)) {
  56. char* str = malloc(sizeof(char)*100);
  57. node_t* child = findNodeByID(n, root->children[i - 2]);
  58. strcpy(str, "Output_");
  59. strcat(str, child->name);
  60. args[i] = str;
  61. } else if (i >= (root->num_children + 2) && i < (root->num_children + 3)) {
  62. char* str = malloc(sizeof(char)*100);
  63. strcpy(str, "Output_");
  64. strcat(str, root->name);
  65. args[i] = str;
  66. } else {
  67. args[i] = candidate_information[i - (root->num_children + 3)];
  68. }
  69. }
  70.  
  71. /*
  72. for (int i = 0; i < root->num_children + num_names + 3; i++) {
  73. printf("%s\n", args[i]);
  74. }
  75. */
  76.  
  77. if (strcmp(root->name, "Who_Won") == 0) {
  78. args[0] = "find_winner";
  79. execv("find_winner", args);
  80. } else {
  81. execv("aggregate_votes", args);
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement