Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #define BUFFER_SIZE 10
- struct shared_data
- {
- unsigned a;
- unsigned b;
- unsigned n;
- volatile unsigned* numbers;
- volatile unsigned current;
- pthread_mutex_t* mutex;
- pthread_cond_t* condConsumer;
- };
- void* generate_numbers(void* tdata)
- {
- struct shared_data* data = (struct shared_data*) tdata;
- printf("START GENERATE NUMBERS THREAD\n");
- int i = 0;
- while (i < data->n)
- {
- int modulo = data->b - data->a + 1;
- int number = rand() % modulo + 1;
- pthread_mutex_lock(data->mutex);
- if (data->current < BUFFER_SIZE)
- {
- //printf("INSERTING %d INTO THE BUFFER\n", number);
- data->numbers[data->current++] = number;
- i++;
- // printf(" CURRENT = %d\n", data->current);
- }
- if (data->current >= BUFFER_SIZE || i >= data->n)
- {
- // printf("SIGNAL TO THE CONDITION VARIABLE\n");
- pthread_cond_signal(data->condConsumer);
- }
- pthread_mutex_unlock(data->mutex);
- }
- printf("END OF GENERATE NUMBERS THREAD\n");
- return NULL;
- }
- char* get_prime_reduction(int number)
- {
- int num = number;
- char* str_reduction = (char*) malloc(50 * sizeof(char));
- sprintf(str_reduction, "%d =", number);
- while (num > 1)
- {
- int i = 2;
- while (i < num)
- {
- if (num % i == 0)
- {
- num /= i;
- char* temp = (char*) malloc(10 * sizeof(char));
- sprintf(temp, " %d *", i);
- str_reduction = strcat(str_reduction, temp);
- break;
- }
- i++;
- }
- if (i == num)
- {
- char* temp = (char*) malloc(10 * sizeof(char));
- sprintf(temp, " %d *", i);
- str_reduction = strcat(str_reduction, temp);
- break;
- }
- }
- str_reduction[strlen(str_reduction) - 2] = '\0';
- return str_reduction;
- }
- int main(int argc, char** argv)
- {
- if (argc < 4)
- {
- printf("Wrong number of parameters.\n");
- printf("Neccessary parameters:\n");
- printf(" a - low bound of interval\n");
- printf(" b - high bound of interval\n");
- printf(" n - number of coumpound numbers\n");
- return EXIT_FAILURE;
- }
- printf("INITIALIZING MUTEX\n");
- pthread_mutex_t mutex;
- pthread_mutex_init(&mutex, NULL);
- printf("INITIALIZING CONSUMER CONDITION VARIABLE\n");
- pthread_cond_t condConsumer;
- pthread_cond_init(&condConsumer, NULL);
- unsigned a = atoi(argv[1]);
- unsigned b = atoi(argv[2]);
- unsigned n = atoi(argv[3]);
- struct shared_data data = {
- a,
- b,
- n,
- malloc(BUFFER_SIZE * sizeof(unsigned)),
- 0,
- &mutex,
- &condConsumer
- };
- pthread_t generate_thread;
- pthread_create(&generate_thread, NULL, &generate_numbers, &data);
- int i = 0;
- while (i < data.n)
- {
- pthread_mutex_lock(&mutex);
- while (data.current == 0)
- {
- pthread_cond_wait(&condConsumer, &mutex);
- }
- pthread_mutex_unlock(&mutex);
- pthread_mutex_lock(&mutex);
- unsigned number = data.numbers[--(data.current)];
- pthread_mutex_unlock(&mutex);
- i++;
- char* number_reduction = get_prime_reduction(number);
- printf("%s\n", (number_reduction));
- // printf(" CURRENT = %d\n", data.current);
- }
- pthread_join(generate_thread, NULL);
- pthread_cond_destroy(&condConsumer);
- pthread_mutex_destroy(&mutex);
- free((void*) data.numbers);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement