Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "pvm3.h"
  3. #include <stdlib.h>
  4.  
  5. #define NAMELEN 3
  6.  
  7. // Jakub Plachta, Marcel Janotta
  8.  
  9. void parentProgram();
  10. void childProgram();
  11.  
  12. main (int argc , char** argv)
  13. {
  14. printf("test\n");
  15. if (pvm_parent() == PvmNoParent)
  16. parentProgram();
  17. else
  18. childProgram();
  19. }
  20.  
  21. //Liczba wystapien litery 'a' w pliku
  22.  
  23. void parentProgram()
  24. {
  25. int i, counter;
  26. int tidmaster, *tid, ilhost, ilarch;
  27. struct pvmhostinfo *info;
  28.  
  29. FILE *handle = fopen("data.txt", "r");
  30.  
  31. char chr;
  32. size_t len = 0;
  33. ssize_t read;
  34.  
  35.  
  36. counter = 0;
  37. /* zgłoszenie procesu do systemu PVM */
  38. tidmaster = pvm_mytid();
  39. /* sprawdzenie konfiguracji maszyny wirtualnej */
  40. pvm_config (&ilhost, &ilarch, &info);
  41. /* przygotowanie tablicy identyfikatorów zadań */
  42. tid = (int*) calloc (ilhost, sizeof(int));
  43. for (i = 0; i < 1; i++) {
  44.  
  45. pvm_spawn("/home/pvm/pvm3/lab9/plik", NULL, PvmTaskHost, info[i].hi_name, 1, &tid[i]);
  46. /* wyślij komunikat synchronizacyjny do procesu potomka */
  47. pvm_initsend (PvmDataDefault);
  48.  
  49. chr = fgetc(handle);
  50. printf("%d \n", chr);
  51. pvm_pkbyte(&chr, 1, 1);
  52. pvm_send(tid[i], 100);
  53. }
  54.  
  55. int response, child_tid, our_info;
  56. int var1, var2;
  57.  
  58. chr = fgetc(handle);
  59.  
  60. while(chr != EOF)
  61. {
  62. our_info = pvm_recv(-1, 200);
  63. pvm_bufinfo(our_info, &var1, &var2, &child_tid);
  64.  
  65. pvm_upkint(&response, 1, 1);
  66. counter += response;
  67. pvm_initsend(PvmDataDefault);
  68. pvm_pkbyte(&chr, 1, 1);
  69. pvm_send(tid[0], 100);
  70.  
  71.  
  72. // printf("%d %d", child_tid, tid[0]);
  73. chr = fgetc(handle);
  74. }
  75.  
  76.  
  77. for (i = 0; i < 1; i++) {
  78. printf("%c\n", chr);
  79. int dane = pvm_recv(-1, 200);
  80.  
  81. pvm_upkint(&response, 1 ,1);
  82. counter += response;
  83.  
  84. int *bytes, *mstag, *tid_local;
  85. pvm_kill(*tid_local);
  86. }
  87. printf ("Odpowiedz z komputera %d\n", counter);
  88. pvm_exit();
  89. exit(0);
  90. }
  91.  
  92.  
  93. void childProgram()
  94. {
  95. int masterid;
  96. char sign;
  97. char name;
  98. int counter;
  99. int i;
  100. /* kto mnie stworzył */
  101. masterid = pvm_parent();
  102.  
  103. while(1)
  104. {
  105. counter = 0;
  106. pvm_recv(masterid, 100);
  107. pvm_upkbyte(&name, 1, 1);
  108.  
  109. i = 0;
  110. sign = name;
  111. if(sign == 'a')
  112. {
  113. counter++;
  114. }
  115.  
  116. /* zwróć wyniki do procesu nadrzędnego */
  117. pvm_initsend(PvmDataDefault);
  118.  
  119. pvm_pkint(&counter, 1, 1);
  120. pvm_send(masterid, 200);
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement