Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. #include "mpi.h"
  2. #include <cstdio>
  3. #include <unistd.h>
  4. #include <ctime>
  5. #include <pthread.h>
  6. #include <cstdlib>
  7. #define QUESTION 100
  8. #define ANSWER 101
  9.  
  10. int rank, size;
  11. MPI_Status status;
  12. int zegar=0;
  13. int ans[1] = {1};//zezwolenie na zajecie widelca przez konkurenta - tresc wiadomosci
  14. //widelce numerowane od 0 do 4
  15. int widelce[5];//0 - nie mam; 1 - nie mam, ale chce; 2 - mam;
  16. int pierwszyWidelec;
  17. int drugiWidelec;
  18.  
  19. int konkurentPierwszegoWidelca;
  20. int konkurentDrugiegoWidelca;
  21.  
  22. int prosbaKonkurentaOPierwszy;
  23. int prosbaKonkurentaODrugi;
  24.  
  25. pthread_mutex_t mutex_widelce = PTHREAD_MUTEX_INITIALIZER;
  26. pthread_t thread;
  27.  
  28. void mysl(){
  29. printf("Filozof nr %d mysli\n", rank );
  30. sleep(rand()%3+1);
  31. }
  32.  
  33. void jedz(){
  34. printf("Filozof nr %d spozywa\n", rank );
  35. sleep(rand()%3+1);
  36. }
  37.  
  38. void prosOWidelecNr(int nrWidelca, int konkurent){
  39. int msg[3];
  40. msg[0]=rank;
  41. msg[1]=zegar;
  42. msg[2]=nrWidelca;
  43. pthread_mutex_lock( &mutex_widelce );
  44. widelce[nrWidelca] = 1;
  45. //printf("rank = %d, miejsce 1\n", rank);
  46. MPI_Send( &msg, 3, MPI_INT, konkurent, QUESTION, MPI_COMM_WORLD );
  47. //printf("rank = %d, miejsce 1.1\n", rank);
  48. pthread_mutex_unlock( &mutex_widelce );
  49.  
  50. //gdy otrzyma zgode to blokuje widelec
  51. //printf("rank = %d, miejsce 2\n", rank);
  52. MPI_Recv(ans, 1, MPI_INT, MPI_ANY_SOURCE, ANSWER, MPI_COMM_WORLD, &status);
  53. //printf("rank = %d, miejsce 2.2\n", rank);
  54. pthread_mutex_lock( &mutex_widelce );
  55. widelce[nrWidelca] = 2;
  56. pthread_mutex_unlock( &mutex_widelce );
  57. printf("Filozof nr %d zabral o widelec nr %d\n", rank, nrWidelca );
  58. }
  59.  
  60. void prosOWidelce(){
  61. pthread_mutex_lock( &mutex_widelce );
  62. zegar++;
  63. pthread_mutex_unlock( &mutex_widelce );
  64. printf("Filozof nr %d prosi o widelec nr %d\n", rank, pierwszyWidelec );
  65. prosOWidelecNr(pierwszyWidelec, konkurentPierwszegoWidelca);
  66. printf("Filozof nr %d prosi o widelec nr %d\n", rank, drugiWidelec );
  67. prosOWidelecNr(drugiWidelec, konkurentDrugiegoWidelca);
  68. }
  69.  
  70. void oddajWidelce(){
  71. //printf("Filozof nr %d oddaje widelce nr %d oraz %d\n", rank, pierwszyWidelec, drugiWidelec );
  72. //najpierw oddaje drugiWidelec
  73. pthread_mutex_lock( &mutex_widelce );
  74. if(prosbaKonkurentaODrugi){
  75. prosbaKonkurentaODrugi=0;
  76. //printf("rank = %d, miejsce 3\n", rank);
  77. MPI_Send( &ans, 1, MPI_INT, konkurentDrugiegoWidelca, ANSWER, MPI_COMM_WORLD );
  78. //printf("rank = %d, miejsce 3.3\n", rank);
  79. }
  80. widelce[drugiWidelec]=0;
  81. pthread_mutex_unlock( &mutex_widelce );
  82.  
  83. //potem oddaje pierwszyWidelec
  84. pthread_mutex_lock( &mutex_widelce );
  85. if(prosbaKonkurentaOPierwszy){
  86. prosbaKonkurentaOPierwszy=0;
  87. //printf("rank = %d, miejsce 4\n", rank);
  88. MPI_Send( &ans, 1, MPI_INT, konkurentPierwszegoWidelca, ANSWER, MPI_COMM_WORLD );
  89. //printf("rank = %d, miejsce 4.4\n", rank);
  90. }
  91. widelce[pierwszyWidelec]=0;
  92. pthread_mutex_unlock( &mutex_widelce );
  93. }
  94.  
  95. bool czyPozwolic(int *msg){
  96. if(widelce[msg[2]] == 0)
  97. return true;
  98. if(widelce[msg[2]] == 1)
  99. if(msg[1] < zegar || (msg[1]==zegar && msg[0] < rank))
  100. return true;
  101. return false;
  102. }
  103.  
  104. //jak sie nie kompiluje to zmienic na: void* odbierz()
  105. void* odbierz(void *){
  106. int msg[3];
  107. while(1){
  108. //printf("rank = %d, miejsce 5\n", rank);
  109. MPI_Recv(&msg, 3, MPI_INT, MPI_ANY_SOURCE, QUESTION, MPI_COMM_WORLD, &status);
  110. //printf("rank = %d, miejsce 5.5\n", rank);
  111. pthread_mutex_lock( &mutex_widelce );
  112. //zezwolenie na wziece widelca przez inny proces
  113. if(czyPozwolic(msg)){
  114. printf("Filozof nr %d pozwala filozofowi %d na widelec %d\n", rank, msg[0], msg[2] );
  115. //printf("rank = %d, miejsce 6\n", rank);
  116. MPI_Send( &ans, 1, MPI_INT, msg[0], ANSWER, MPI_COMM_WORLD );
  117. //printf("rank = %d, miejsce 6.6\n", rank);
  118. }
  119. //w przypadku odmowy zapamietuje pytanie o dany widelec;
  120. else{
  121. //printf("Filozof nr %d NIE pozwala filozofowi %d na widelec %d\n", rank, msg[0], msg[2] );
  122. if(msg[2]==pierwszyWidelec)
  123. prosbaKonkurentaOPierwszy=1;
  124. else
  125. prosbaKonkurentaODrugi=1;
  126. }
  127. pthread_mutex_unlock( &mutex_widelce );
  128.  
  129. }
  130. }
  131.  
  132. void dopasujWidelce(){
  133. if(rank==4){
  134. pierwszyWidelec = 0;
  135. konkurentPierwszegoWidelca = 0;
  136. drugiWidelec = 4;
  137. konkurentDrugiegoWidelca = 3;
  138. }
  139. else{
  140. pierwszyWidelec = rank;
  141. konkurentPierwszegoWidelca = (rank + 4 )%5;
  142. drugiWidelec = rank+1;
  143. konkurentDrugiegoWidelca = rank + 1;
  144. }
  145. }
  146.  
  147. int main(int argc, char** argv){
  148. MPI_Init( &argc, &argv );
  149.  
  150. MPI_Comm_rank( MPI_COMM_WORLD, &rank );
  151. MPI_Comm_size( MPI_COMM_WORLD, &size );
  152.  
  153. pthread_create(&thread, NULL, odbierz, NULL);
  154. srand(time(0));
  155. dopasujWidelce();
  156.  
  157. while(1){
  158. mysl();
  159. prosOWidelce();
  160. jedz();
  161. oddajWidelce();
  162.  
  163. }
  164.  
  165. return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement