Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<pthread.h>
  4. #include<stdlib.h>
  5. #include<unistd.h>
  6. /*
  7. *
  8. */
  9. int B[100][100];
  10. int A[100][100];
  11. pthread_mutex_t lock;
  12. pthread_t tid[100];
  13. pthread_barrier_t barrier;
  14. static int numBarriers=100;
  15. static int x=0;
  16. static int p=0;
  17.  
  18.  
  19. void* populate(void *arg);
  20. int check_neighbours(int i, int j);
  21. void printArray();
  22.  
  23. int main(int argc, char** argv) {
  24. FILE* file=NULL;
  25.  
  26. file = fopen( argv[1], "r" );
  27.  
  28. int i=0;
  29. int y=0;
  30.  
  31. for (i=0;i<100;i++){
  32. for(y=0;y<100;y++){
  33.  
  34. fscanf(file,"%d", &A[i][y]);
  35.  
  36. }
  37. }
  38. fclose(file);
  39. while(1){
  40. for (i=0;i<100;i++){
  41. for(y=0;y<100;y++){
  42. B[i][y]=A[i][y];
  43. }
  44. }
  45.  
  46.  
  47.  
  48.  
  49. if (pthread_mutex_init(&lock, NULL) != 0)
  50. {
  51. printf("n mutex init failedn");
  52. return 1;
  53. }
  54. int k=0,j,err,s;
  55. int c[5];
  56. s = pthread_barrier_init(&barrier, NULL, 100);
  57.  
  58. for(i=0;i<100;i=i+10){
  59. for(j=0;j<100;j=j+10){
  60. c[0]=i;
  61. c[1]=i+9;
  62. c[2]=j;
  63. c[3]=j+9;
  64. c[4]=k;
  65. err = pthread_create(&(tid[k++]),NULL, &populate, (void *)c);
  66. if (err != 0)
  67. printf("ncan't create thread :[%s]", strerror(err));
  68. //printf("k= %d",k);
  69.  
  70. }
  71. }
  72.  
  73.  
  74.  
  75.  
  76. for(i=0;i<100;i++){
  77. for(j=0;j<100;j++){
  78. A[i][j]=B[i][j];
  79. printf("%d", B[i][j] );
  80. if(j==99) printf("|n");
  81. }
  82. }
  83.  
  84. printf("n");
  85. pthread_mutex_destroy(&lock);
  86.  
  87. pthread_barrier_destroy(&barrier);
  88. }
  89.  
  90. return (EXIT_SUCCESS);
  91. }
  92.  
  93.  
  94. int check_neighbours(int i, int j){
  95. int y=0;
  96. int k=0;
  97. int count=0;
  98.  
  99. if(i==0 && j==0){
  100. if(A[i+1][j]==1) count++;
  101. if(A[i+1][j+1]==1) count++;
  102. if(A[i][j+1]==1) count++;
  103. return count;
  104. }
  105. else if(i==0 && j==99){
  106. if(A[i][j-1]==1) count++;
  107. if(A[i+1][j-1]==1) count++;
  108. if(A[i+1][j]==1) count++;
  109. return count;
  110. }
  111. else if(i==99 && j==0){
  112. if(A[i-1][j]==1) count++;
  113. if(A[i-1][j+1]==1) count++;
  114. if(A[i][j+1]==1) count++;
  115. return count;
  116. }
  117. else if(i==99 && j==99){
  118. if(A[i-1][j]==1) count++;
  119. if(A[i-1][j-1]==1) count++;
  120. if(A[i][j-1]==1) count++;
  121. return count;
  122. }
  123. else if(i==0){
  124. if(A[i][j-1]==1) count++;
  125. if(A[i][j+1]==1) count++;
  126. if(A[i+1][j-1]==1) count++;
  127. if(A[i+1][j]==1) count++;
  128. if(A[i+1][j+1]==1) count++;
  129. return count;
  130. }
  131. else if(i==99){
  132. if(A[i-1][j]==1) count++;
  133. if(A[i-1][j+1]==1) count++;
  134. if(A[i][j+1]==1) count++;
  135. if(A[i-1][j-1]==1) count++;
  136. if(A[i][j-1]==1) count++;
  137. return count;
  138. }
  139. else if(j==0){
  140. if(A[i-1][j]==1) count++;
  141. if(A[i-1][j-1]==1) count++;
  142. if(A[i][j+1]==1) count++;
  143. if(A[i+1][j+1]==1) count++;
  144. if(A[i+1][j]==1) count++;
  145. return count;
  146. }
  147. else if(j==99){
  148. if(A[i-1][j]==1) count++;
  149. if(A[i-1][j-1]==1) count++;
  150. if(A[i][j-1]==1) count++;
  151. if(A[i+1][j-1]==1) count++;
  152. if(A[i+1][j]==1) count++;
  153. return count;
  154. }
  155. else{
  156. if(A[i-1][j-1]==1) count++;
  157. if(A[i-1][j]==1) count++;
  158. if(A[i-1][j+1]==1) count++;
  159. if(A[i][j-1]==1) count++;
  160. if(A[i][j+1]==1) count++;
  161. if(A[i+1][j-1]==1) count++;
  162. if(A[i+1][j]==1) count++;
  163. if(A[i+1][j+1]==1) count++;
  164. return count;
  165. }
  166.  
  167. }
  168. void* populate(void *arg){
  169. pthread_mutex_lock(&lock);
  170. int s,k;
  171. int *val=(int *)arg;
  172. int i_low=val[0];
  173. int i_high=val[1];
  174. int j_low=val[2];
  175. int j_high=val[3];
  176. int g=val[4];
  177. int i=0,j=0,sum=0;
  178.  
  179. for(i=i_low;i<=i_high;i++){
  180. for(j=j_low;j<=j_high;j++){
  181. sum=check_neighbours(i,j);
  182. if((sum<=1) && (A[i][j]==1)) {
  183. B[i][j]=0;
  184. //printf("thread %d changed value of %d %d to : %dn",g,i,j, B[i][j] );
  185. }
  186. else if(((sum==2) || (sum==3)) && (A[i][j]==1)){
  187. B[i][j]=1;
  188. //printf("thread %d changed value of %d %d to : %dn",g,i,j, B[i][j] );
  189. }
  190. else if((sum>3) && (A[i][j]==1)){
  191. B[i][j]=0;
  192. //printf("thread %d changed value of %d %d to : %dn",g,i,j, B[i][j] );
  193. }
  194. else if((sum==3) && (A[i][j]==0)){
  195. B[i][j]=1;
  196. //printf("thread %d changed value of %d %d to : %dn",g,i,j, B[i][j] );
  197. }
  198. }
  199. }
  200.  
  201. pthread_mutex_unlock(&lock);
  202.  
  203.  
  204.  
  205.  
  206. s = pthread_barrier_wait(&barrier);
  207. //printf("Thread num. %d left the barriern",g );
  208.  
  209.  
  210.  
  211.  
  212. return NULL;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement