Advertisement
Guest User

Untitled

a guest
May 21st, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <wait.h>
  6. #include <time.h>
  7. #include <string.h>
  8. #include <fcntl.h>
  9. #include <sys/stat.h>
  10. #include <sys/mman.h>
  11. #include <semaphore.h>
  12. #include <pthread.h>
  13. #define numberArray 300
  14.  
  15. void * Tgenerate(void *arg);
  16. void * TCalculate(void *arg);
  17. void * TInc(void *arg);
  18. void * TDnc(void *arg);
  19.  
  20. int novosElementos;
  21. int posLeitura;
  22. int numPos;
  23. int numNeg;
  24. int helpPOS;
  25. int helpNEG;
  26. struct Prova{
  27. int number;
  28. int notaG1;
  29. int notaG2;
  30. int notaG3;
  31. int notaFinal;
  32. };
  33.  
  34.  
  35. pthread_mutex_t LerArray;
  36. pthread_mutex_t CalculateControl;
  37. pthread_mutex_t Increment;
  38. pthread_mutex_t IncrementLOCK;
  39. pthread_mutex_t Decrement;
  40. pthread_mutex_t DecrementLOCK;
  41.  
  42. pthread_cond_t condInit;
  43. pthread_cond_t condINC;
  44. pthread_cond_t condDEC;
  45.  
  46. int flag;
  47. int flagTerm;
  48.  
  49. int main(){
  50. numNeg=0;
  51. numPos=0;
  52. flag=0;
  53. posLeitura=0;
  54. pthread_mutex_init(&LerArray,NULL);
  55. pthread_mutex_init(&CalculateControl,NULL);
  56. pthread_mutex_init(&Increment,0);
  57. pthread_mutex_init(&IncrementLOCK,0);
  58. pthread_mutex_init(&Decrement,0);
  59. pthread_mutex_init(&DecrementLOCK,0);
  60. pthread_cond_init(&condInit,NULL);
  61. pthread_cond_init(&condINC,NULL);
  62. pthread_cond_init(&condDEC,NULL);
  63.  
  64.  
  65. struct Prova array[ numberArray];
  66.  
  67. pthread_t threads[5];
  68. pthread_create(&threads[3],NULL,TInc,NULL);
  69. pthread_create(&threads[4],NULL,TDnc,NULL);
  70. sleep(0.1);
  71. pthread_create(&threads[0],NULL,Tgenerate,(void *)array);
  72.  
  73. pthread_create(&threads[1],NULL,TCalculate,(void *)array);
  74.  
  75. pthread_create(&threads[2],NULL,TCalculate,(void *)array);
  76.  
  77. int i;
  78.  
  79. sleep(1);
  80.  
  81. pthread_cancel(threads[3]);
  82. pthread_cancel(threads[4]);
  83.  
  84. for ( i = 0; i < 5; i++)
  85. {
  86. pthread_join(threads[i],NULL);
  87. }
  88.  
  89. printf("NUM POS : %d \nNUM NEG : %d\n",numPos,numNeg);
  90.  
  91. pthread_mutex_destroy(&LerArray);
  92. pthread_mutex_destroy(&Increment);
  93. pthread_mutex_destroy(&Decrement);
  94. pthread_mutex_destroy(&CalculateControl);
  95. pthread_cond_destroy(&condInit);
  96. return 0;
  97. }
  98.  
  99.  
  100.  
  101. void * Tgenerate(void *arg){
  102.  
  103.  
  104. time_t t;
  105. /* intializes RNG (srand():stdlib.h; time(): time.h) */
  106. srand ((unsigned) time (&t));
  107.  
  108. struct Prova * array=(struct Prova *) arg;
  109.  
  110. int i;
  111. for ( i = 0; i < numberArray; i++)
  112. {
  113.  
  114. pthread_mutex_lock(&LerArray);
  115.  
  116. struct Prova p;
  117. p.notaG1=rand() %101;
  118. p.notaG2=rand() %101;
  119. p.notaG3=rand() %101;
  120.  
  121. *(array+i)=p;
  122.  
  123. novosElementos++;
  124. pthread_cond_broadcast(&condInit);
  125.  
  126. pthread_mutex_unlock(&LerArray);
  127. }
  128.  
  129. while (posLeitura!=numberArray);
  130. pthread_mutex_lock(&CalculateControl);
  131. flag=1;
  132. pthread_mutex_unlock(&Increment);
  133. pthread_mutex_unlock(&Decrement);
  134. pthread_cond_broadcast(&condInit);
  135. pthread_mutex_unlock(&LerArray);
  136. pthread_exit(NULL);
  137. }
  138.  
  139. void * TCalculate(void * arg){
  140.  
  141. struct Prova * array=(struct Prova *) arg;
  142. while (posLeitura!= numberArray)
  143. {
  144.  
  145.  
  146. pthread_mutex_lock(&LerArray);
  147.  
  148. while (novosElementos==0&&flag==0)
  149. {
  150. pthread_cond_wait(&condInit,&LerArray);
  151. }
  152. if (flag==1)
  153. {
  154.  
  155. pthread_mutex_unlock(&LerArray);
  156. pthread_exit(NULL);
  157. }
  158.  
  159. ((array[posLeitura])).notaFinal=(array[posLeitura].notaG1+array[posLeitura].notaG2+array[posLeitura].notaG3)/3;
  160. // printf("TESTE2.3 : %d\n",((array[posLeitura])).notaFinal);
  161. if (array[posLeitura].notaFinal>50)
  162. {
  163.  
  164. pthread_mutex_lock(&Increment);
  165.  
  166. pthread_cond_signal(&condINC);
  167. printf("HELP2\n");
  168. helpPOS++;
  169. pthread_mutex_unlock(&Increment);
  170.  
  171. }else
  172. {
  173.  
  174. pthread_mutex_lock(&Decrement);
  175.  
  176. pthread_cond_signal(&condDEC);
  177. printf("HELP1\n");
  178. helpNEG++;
  179. pthread_mutex_unlock(&Decrement);
  180.  
  181.  
  182. }
  183. novosElementos--;
  184. posLeitura++;
  185. pthread_mutex_unlock(&LerArray);
  186. }
  187. //pthread_mutex_unlock(&Increment);
  188.  
  189. //pthread_mutex_unlock(&Decrement);
  190. pthread_mutex_lock(&LerArray);
  191. pthread_mutex_unlock(&CalculateControl);
  192. pthread_mutex_unlock(&LerArray);
  193.  
  194. pthread_exit(NULL);
  195. }
  196.  
  197. void * TInc(void *arg){
  198. while (posLeitura!= numberArray)
  199. {
  200. pthread_mutex_lock(&Increment);
  201.  
  202. while (helpPOS==0){
  203. pthread_cond_wait(&condINC,&Increment);
  204.  
  205. if (flag==1&&helpPOS==0)
  206. {
  207. pthread_exit(NULL);
  208. }
  209.  
  210. }
  211. int i;
  212. for ( i = 0; i < helpPOS; i++)
  213. {
  214. printf("MAIS\n");
  215. numPos++;
  216. }
  217. helpPOS=0;
  218. pthread_mutex_unlock(&Increment);
  219.  
  220. }
  221. pthread_exit(NULL);
  222.  
  223. }
  224. void * TDnc(void *arg){
  225. while (posLeitura!= numberArray)
  226. {
  227. pthread_mutex_lock(&Decrement);
  228.  
  229. while (helpNEG==0)
  230. {
  231.  
  232. pthread_cond_wait(&condDEC,&Decrement);
  233.  
  234. if (flag==1&&helpNEG==0)
  235. {
  236. pthread_exit(NULL);
  237. }
  238.  
  239.  
  240. }
  241. int i;
  242. for ( i = 0; i < helpNEG; i++)
  243. {
  244. printf("MENOS\n");
  245. numNeg++;
  246.  
  247. }
  248. helpNEG=0;
  249. pthread_mutex_unlock(&Decrement);
  250. }
  251.  
  252. pthread_exit(NULL);
  253.  
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement