Advertisement
Guest User

Untitled

a guest
May 21st, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6.  
  7. #define NUM_PROVAS 5
  8. #define MAX_GRADE 100
  9. #define NUM_THREADS 5
  10.  
  11. typedef struct {
  12. int number;
  13. int notaG1;
  14. int notaG2;
  15. int notaG3;
  16. int notaFinal;
  17. } Prova;
  18.  
  19. Prova arrayProva[NUM_PROVAS];
  20. int numProvas;
  21.  
  22. int numPos;
  23. int numNeg;
  24.  
  25. pthread_mutex_t mutex;
  26. pthread_cond_t condCalculate = PTHREAD_COND_INITIALIZER;
  27.  
  28. void *insertProvas(void *arg) {
  29.  
  30. time_t t;
  31. srand ((unsigned) time (&t));
  32.  
  33. int i;
  34. for (i = 0; i < NUM_PROVAS; i++) {
  35.  
  36. Prova prova;
  37. prova.number = rand() % 1000 + 1;
  38. prova.notaG1 = rand() % MAX_GRADE + 1;
  39. prova.notaG2 = rand() % MAX_GRADE + 1;
  40. prova.notaG3 = rand() % MAX_GRADE + 1;
  41.  
  42. arrayProva[i] = prova;
  43.  
  44. pthread_mutex_lock(&mutex);
  45.  
  46. pthread_cond_signal(&condCalculate);
  47. numProvas++;
  48.  
  49. printf("Cria: %d\n", numProvas - 1);
  50.  
  51. pthread_mutex_unlock(&mutex);
  52.  
  53. pthread_mutex_lock(&mutex);
  54.  
  55. if (numProvas == 5) {
  56. pthread_cond_broadcast(&condCalculate);
  57. } else {
  58. pthread_cond_wait(&condCalculate, &mutex);
  59. }
  60.  
  61. pthread_mutex_unlock(&mutex);
  62. }
  63.  
  64. pthread_exit(NULL);
  65.  
  66. }
  67.  
  68. void *calculateFinalNote(void *arg) {
  69.  
  70. while (1) {
  71.  
  72. pthread_mutex_lock(&mutex);
  73.  
  74. printf("Calculate Wait\n");
  75. pthread_cond_wait(&condCalculate, &mutex);
  76.  
  77. int index = numProvas - 1;
  78.  
  79. pthread_mutex_unlock(&mutex);
  80.  
  81. printf("Calcula: %d\n", index);
  82.  
  83. arrayProva[index].notaFinal = (arrayProva[index].notaG1 + arrayProva[index].notaG2 + arrayProva[index].notaG3) / 3;
  84.  
  85. if (index + 1 == NUM_PROVAS) {
  86. printf("Calculate finished!\n");
  87. pthread_exit(NULL);
  88. }
  89.  
  90. ///*
  91. pthread_mutex_lock(&mutex);
  92.  
  93. pthread_cond_signal(&condCalculate);
  94.  
  95. pthread_mutex_unlock(&mutex);
  96. //*/
  97.  
  98. }
  99.  
  100. pthread_exit(NULL);
  101.  
  102. }
  103.  
  104. int main() {
  105.  
  106. numProvas = 0;
  107.  
  108. numPos = 0;
  109. numNeg = 0;
  110.  
  111. pthread_t threads[NUM_THREADS];
  112.  
  113. int i;
  114. for (i = 1; i < 3; i++) {
  115. if (pthread_create(&threads[i], NULL, calculateFinalNote, NULL) != 0) {
  116. printf("Erro no pthread_create()!\n");
  117. exit(1);
  118. }
  119. }
  120.  
  121. if (pthread_create(&threads[0], NULL, insertProvas, NULL) != 0) {
  122. printf("Erro no pthread_create()!\n");
  123. exit(1);
  124. }
  125.  
  126. for (i = 0; i < 3; i++) {
  127. if(pthread_join(threads[i], NULL) != 0){
  128. perror("Erro a terminar thread...");
  129. exit(1);
  130. }
  131. }
  132.  
  133. for (i = 0; i < NUM_PROVAS; i++) {
  134. printf("Aluno: %d Nota G1: %d Nota G2: %d Nota G3: %d Nota Final: %d\n", arrayProva[i].number, arrayProva[i].notaG1, arrayProva[i].notaG2, arrayProva[i].notaG3, arrayProva[i].notaFinal);
  135. }
  136.  
  137. return 0;
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement