Advertisement
Guest User

Rozklad na prvocisla

a guest
Dec 7th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5.  
  6. #define BUFFER_SIZE 10
  7.  
  8. struct shared_data
  9. {
  10. unsigned a;
  11. unsigned b;
  12. unsigned n;
  13.  
  14. volatile unsigned* numbers;
  15. volatile unsigned current;
  16.  
  17. pthread_mutex_t* mutex;
  18. pthread_cond_t* condConsumer;
  19. };
  20.  
  21. void* generate_numbers(void* tdata)
  22. {
  23. struct shared_data* data = (struct shared_data*) tdata;
  24. printf("START GENERATE NUMBERS THREAD\n");
  25.  
  26. int i = 0;
  27. while (i < data->n)
  28. {
  29. int modulo = data->b - data->a + 1;
  30. int number = rand() % modulo + 1;
  31.  
  32. pthread_mutex_lock(data->mutex);
  33. if (data->current < BUFFER_SIZE)
  34. {
  35. //printf("INSERTING %d INTO THE BUFFER\n", number);
  36. data->numbers[data->current++] = number;
  37. i++;
  38. // printf(" CURRENT = %d\n", data->current);
  39. }
  40.  
  41. if (data->current >= BUFFER_SIZE || i >= data->n)
  42. {
  43. // printf("SIGNAL TO THE CONDITION VARIABLE\n");
  44. pthread_cond_signal(data->condConsumer);
  45. }
  46. pthread_mutex_unlock(data->mutex);
  47. }
  48.  
  49. printf("END OF GENERATE NUMBERS THREAD\n");
  50. return NULL;
  51. }
  52.  
  53. char* get_prime_reduction(int number)
  54. {
  55. int num = number;
  56.  
  57. char* str_reduction = (char*) malloc(50 * sizeof(char));
  58. sprintf(str_reduction, "%d =", number);
  59.  
  60. while (num > 1)
  61. {
  62. int i = 2;
  63.  
  64. while (i < num)
  65. {
  66. if (num % i == 0)
  67. {
  68. num /= i;
  69. char* temp = (char*) malloc(10 * sizeof(char));
  70. sprintf(temp, " %d *", i);
  71. str_reduction = strcat(str_reduction, temp);
  72. break;
  73. }
  74.  
  75. i++;
  76. }
  77.  
  78. if (i == num)
  79. {
  80. char* temp = (char*) malloc(10 * sizeof(char));
  81. sprintf(temp, " %d *", i);
  82. str_reduction = strcat(str_reduction, temp);
  83. break;
  84. }
  85. }
  86.  
  87. str_reduction[strlen(str_reduction) - 2] = '\0';
  88.  
  89. return str_reduction;
  90. }
  91.  
  92. int main(int argc, char** argv)
  93. {
  94. if (argc < 4)
  95. {
  96. printf("Wrong number of parameters.\n");
  97. printf("Neccessary parameters:\n");
  98. printf(" a - low bound of interval\n");
  99. printf(" b - high bound of interval\n");
  100. printf(" n - number of coumpound numbers\n");
  101.  
  102. return EXIT_FAILURE;
  103. }
  104.  
  105. printf("INITIALIZING MUTEX\n");
  106. pthread_mutex_t mutex;
  107. pthread_mutex_init(&mutex, NULL);
  108.  
  109. printf("INITIALIZING CONSUMER CONDITION VARIABLE\n");
  110. pthread_cond_t condConsumer;
  111. pthread_cond_init(&condConsumer, NULL);
  112.  
  113. unsigned a = atoi(argv[1]);
  114. unsigned b = atoi(argv[2]);
  115. unsigned n = atoi(argv[3]);
  116.  
  117.  
  118. struct shared_data data = {
  119. a,
  120. b,
  121. n,
  122. malloc(BUFFER_SIZE * sizeof(unsigned)),
  123. 0,
  124. &mutex,
  125. &condConsumer
  126. };
  127.  
  128. pthread_t generate_thread;
  129. pthread_create(&generate_thread, NULL, &generate_numbers, &data);
  130.  
  131. int i = 0;
  132. while (i < data.n)
  133. {
  134. pthread_mutex_lock(&mutex);
  135. while (data.current == 0)
  136. {
  137. pthread_cond_wait(&condConsumer, &mutex);
  138. }
  139. pthread_mutex_unlock(&mutex);
  140.  
  141. pthread_mutex_lock(&mutex);
  142. unsigned number = data.numbers[--(data.current)];
  143. pthread_mutex_unlock(&mutex);
  144.  
  145. i++;
  146.  
  147. char* number_reduction = get_prime_reduction(number);
  148. printf("%s\n", (number_reduction));
  149. // printf(" CURRENT = %d\n", data.current);
  150. }
  151.  
  152. pthread_join(generate_thread, NULL);
  153. pthread_cond_destroy(&condConsumer);
  154. pthread_mutex_destroy(&mutex);
  155. free((void*) data.numbers);
  156.  
  157. return EXIT_SUCCESS;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement