Advertisement
Guest User

Untitled

a guest
May 28th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <time.h>
  6.  
  7.  
  8. #define nt 5
  9. #define N 4
  10.  
  11. void* master_fun (void *threadarg);
  12. void* slave_fun (void *threadarg);
  13. int dot_prod(int x[], int y[]);
  14.  
  15. pthread_mutex_t lock_s;
  16.  
  17. int V1[N];
  18. int V2[N];
  19. int master_flag=0;
  20.  
  21.  
  22. int main(int argc, char* argv[])
  23. {
  24. srand(time(NULL));
  25. pthread_t threads[nt];
  26. unsigned int id[nt];
  27.  
  28. int rc,t;
  29.  
  30. //semaphores declaration out of main
  31. if(pthread_mutex_init(&lock_s, NULL) != 0)
  32. {
  33. return 1;
  34. }
  35. //CREATION THREADS
  36. for(t=0;t<nt;t++)
  37. {
  38. printf("In main thread#%d are being created : id--> ",t);
  39. if(t==4)
  40. {
  41. rc=pthread_create(&threads[t], NULL, master_fun,(void*) &t);
  42. printf("(%u)",t);
  43. }
  44. else
  45. {
  46. rc=pthread_create(&threads[t], NULL, slave_fun, (void*) &t);
  47. printf("(%u)",t);
  48.  
  49. }
  50. printf("\n");
  51.  
  52. if(rc)
  53. {
  54. printf("Error; return %d as value of error\n", rc);
  55. return(-1);
  56. }
  57. }
  58.  
  59.  
  60. //WAIT THREADS TO EXECUTE
  61. for(t=0;t<nt;t++)
  62. {
  63. rc=pthread_join(threads[t], NULL);
  64.  
  65. if(rc)
  66. {
  67. printf("Error; return %d as value of error\n", rc);
  68. return(-1);
  69. }
  70. }
  71.  
  72.  
  73. pthread_mutex_destroy(&lock_s);
  74.  
  75. }
  76.  
  77. void* master_fun(void *threadarg)
  78. {
  79. int i;
  80. for(i=0;i<N;i++)
  81. {
  82. V1[i]=rand()%20-10;
  83. V2[i]=rand()%20-10;
  84. printf("\n--/%d/--\n",V1[i]);
  85. printf("\n--/%d/--\n",V2[i]);
  86. }
  87. master_flag=1;
  88. pthread_exit(NULL);
  89.  
  90. }
  91.  
  92. void* slave_fun (void *threadarg)
  93. {
  94. int PS,i;
  95. int a1[N/4],a2[N/4];
  96.  
  97. pthread_mutex_lock(&lock_s);
  98.  
  99. while(master_flag==0)
  100. ;
  101.  
  102. //dot product
  103. if((*(int*)threadarg)==0)
  104. {
  105. for(i=0;i<N/4;i++)
  106. a1[i]=V1[i];
  107. a2[i]=V2[i];
  108.  
  109. PS=dot_prod(a1,a2);
  110. printf("\nprod_scal di mamt #1: %d\n",PS);
  111. }
  112. if((*(int*)threadarg)==1)
  113. {
  114. for(i=0;i<N/4;i++)
  115. a1[i]=V1[i];
  116. a2[i]=V2[i];
  117.  
  118. PS=dot_prod(a1,a2);
  119. printf("\nprod_scal di mamt #2: %d\n",PS);
  120. }
  121. if((*(int*)threadarg)==2)
  122. {
  123. for(i=0;i<N/4;i++)
  124. a1[i]=V1[i];
  125. a2[i]=V2[i];
  126.  
  127. PS=dot_prod(a1,a2);
  128. printf("\nprod_scal di mamt #3: %d\n",PS);
  129. }
  130. if((*(int*)threadarg)==3)
  131. {
  132. for(i=0;i<N/4;i++)
  133. a1[i]=V1[i];
  134. a2[i]=V2[i];
  135.  
  136. PS=dot_prod(a1,a2);
  137. printf("\nprod_scal di mamt #4: %d\n",PS);
  138. }
  139.  
  140. pthread_mutex_unlock(&lock_s);
  141. pthread_exit(NULL);
  142.  
  143.  
  144. }
  145.  
  146. int dot_prod(int x[], int y[])
  147. {
  148. int P,i;
  149.  
  150. P=0;
  151. for(i=0; i<N/4;i++)
  152. {
  153.  
  154. P=P+x[i]*y[i];
  155. }
  156.  
  157. return P;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement