Guest User

Untitled

a guest
Dec 11th, 2023
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.70 KB | None | 0 0
  1. #include <unistd.h>
  2. #include "queue.h"
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <stdbool.h>
  6. #include <pthread.h>
  7.  
  8.  
  9. #define STORAGE_CAPACITY 10
  10. #define THREAD_COUNT 6
  11. #define ASC 0
  12. #define DESC 1
  13. #define EQ 2
  14. #define SWAP 3
  15.  
  16. void *compare_length_thread(void *data)
  17. {
  18.     ThreadData *thread_data = (ThreadData *)data;
  19.     Storage *storage = thread_data->storage;
  20.     int type = thread_data->type;
  21.  
  22.     while (1) {
  23.         Node *curr1;
  24.  
  25.         if (pthread_mutex_lock(&storage->sync) != 0) {
  26.             perror("compare_length_thread: pthread_mutex_lock(0) failed");
  27.             break;
  28.         }
  29.  
  30.         if( (curr1 = storage->first) == NULL)
  31.         {
  32.             fprintf(stderr, "compare_length_thread: curr1 is NULL\n");
  33.             pthread_mutex_unlock(&storage->sync);
  34.             break;
  35.         }
  36.  
  37.         if (pthread_mutex_lock(&curr1->sync) != 0) {
  38.             perror("compare_length_thread: pthread_mutex_lock(1) failed");
  39.             pthread_mutex_unlock(&storage->sync);
  40.             break;
  41.         }
  42.  
  43.         pthread_mutex_unlock(&storage->sync);
  44.         Node *curr2 = curr1->next;
  45.         while (curr2 != NULL)
  46.         {
  47.  
  48.             if(pthread_mutex_lock(&curr2->sync) != 0)
  49.             {
  50.                 perror("compare_length_thread: pthread_mutex_lock(2) failed");
  51.                 break;
  52.             }
  53.  
  54.             const int pair_count = strlen(curr1->value) - strlen(curr2->value);
  55.  
  56.             switch (type) {
  57.             case EQ:
  58.                 if(pair_count == 0) ++curr1->counter_eq;
  59.                 break;
  60.             case ASC:
  61.                 if(pair_count < 0) ++curr1->counter_asc;
  62.                 break;
  63.             case DESC:
  64.                 if(pair_count > 0) ++curr1->counter_dsc;
  65.                 break;
  66.             }
  67.  
  68.  
  69.             pthread_mutex_unlock(&curr1->sync);
  70.             curr1 = curr2;
  71.             curr2 = curr1->next;
  72.         }
  73.  
  74.         pthread_mutex_unlock(&curr1->sync);
  75.        
  76.     }
  77.     return NULL;
  78. }
  79.  
  80. static void perform_swap(Node **curr1_next, Node *curr2, Node *curr3) {
  81.     ++curr2->counter_swap;
  82. //    printf("\t%s (swap: %d)\n", curr2->value, curr2->counter_swap);
  83.     *curr1_next = curr3;
  84.     curr2->next = curr3->next;
  85.     curr3->next = curr2;
  86. }
  87.  
  88. void *swap_thread(void *data)
  89. {
  90.     Storage *storage = (Storage *)data;
  91.  
  92.     while (1) {
  93.         Node *curr1, *curr2, *curr3;
  94.  
  95.         if (pthread_mutex_lock(&storage->sync) != 0) {
  96.             perror("swap_thread: pthread_mutex_lock(0) failed");
  97.             break;
  98.         }
  99.  
  100.         if( (curr1 = storage->first) == NULL)
  101.         {
  102.             fprintf(stderr, "swap_thread: curr1 is NULL\n");
  103.             pthread_mutex_unlock(&storage->sync);
  104.             break;
  105.         }
  106.         if (pthread_mutex_lock(&curr1->sync) != 0) {
  107.             perror("swap_thread: pthread_mutex_lock(1) failed");
  108.             pthread_mutex_unlock(&storage->sync);
  109.             break;
  110.         }
  111.  
  112.         if( (curr2 = curr1->next) == NULL)
  113.         {
  114.             fprintf(stderr, "swap_thread: curr2 is NULL\n");
  115.             pthread_mutex_unlock(&curr1->sync);
  116.             pthread_mutex_unlock(&storage->sync);
  117.             break;
  118.         }
  119.         if (pthread_mutex_lock(&curr2->sync) != 0) {
  120.             perror("swap_thread: pthread_mutex_lock(2) failed");
  121.             pthread_mutex_unlock(&curr1->sync);
  122.             pthread_mutex_unlock(&storage->sync);
  123.             break;
  124.         }
  125.  
  126.         if ((rand() % 2) == 0) {
  127.             perform_swap(& storage->first, curr1, curr2);
  128.             curr1 = storage->first;
  129.             curr2 = curr1->next;
  130.         }
  131.  
  132.         pthread_mutex_unlock(&storage->sync);
  133.  
  134.         curr3 = curr2->next;
  135.         while (curr3 != NULL) {
  136.  
  137.             if(pthread_mutex_lock(&curr3->sync) != 0)
  138.             {
  139.                 perror("swap_thread: pthread_mutex_lock(3) failed");
  140.                 break;
  141.             }
  142.  
  143.             if ((rand() % 2) == 0) {
  144.                 perform_swap(& curr1->next, curr2, curr3);
  145.             }
  146.  
  147.             curr3 = curr1->next;
  148.             pthread_mutex_unlock(&curr1->sync);
  149.             curr1 = curr3;
  150.             curr2 = curr1->next;
  151.             curr3 = curr2->next;
  152.         }
  153.  
  154.         pthread_mutex_unlock(&curr1->sync);
  155.         pthread_mutex_unlock(&curr2->sync);
  156.     }
  157.     return NULL;
  158. }
  159.  
  160. void *count_monitor(void *arg) {
  161.     Storage *storage = (Storage *)arg;
  162.  
  163.     while (1) {
  164.         Node *curr1;
  165.  
  166.         if (pthread_mutex_lock(&storage->sync) != 0) {
  167.             perror("count_monitor: pthread_mutex_lock(0) failed");
  168.             break;
  169.         }
  170.  
  171.         if( (curr1 = storage->first) == NULL)
  172.         {
  173.             fprintf(stderr, "count_monitor: curr1 is NULL\n");
  174.             pthread_mutex_unlock(&storage->sync);
  175.             break;
  176.         }
  177.  
  178.         if (pthread_mutex_lock(&curr1->sync) != 0) {
  179.             perror("count_monitor: pthread_mutex_lock(1) failed");
  180.             pthread_mutex_unlock(&storage->sync);
  181.             break;
  182.         }
  183.  
  184.         pthread_mutex_unlock(&storage->sync);
  185.  
  186.         printf("\t%s (swap: %d asc: %d dsc: %d eq: %d)\n",
  187.                curr1->value, curr1->counter_swap,
  188.                curr1->counter_asc, curr1->counter_dsc, curr1->counter_eq);
  189.  
  190.         int swap = curr1->counter_swap;
  191.         int asc = curr1->counter_asc;
  192.         int dsc = curr1->counter_dsc;
  193.         int eq = curr1->counter_eq;
  194.  
  195.         Node *curr2 = curr1->next;
  196.         while (curr2 != NULL)
  197.         {
  198.  
  199.             if(pthread_mutex_lock(&curr2->sync) != 0)
  200.             {
  201.                 perror("count_monitor: pthread_mutex_lock(2) failed");
  202.                 break;
  203.             }
  204.  
  205.             pthread_mutex_unlock(&curr1->sync);
  206.  
  207.             printf("\t%s (swap: %d asc: %d dsc: %d eq: %d)\n",
  208.                    curr2->value, curr2->counter_swap,
  209.                    curr2->counter_asc, curr2->counter_dsc, curr2->counter_eq);
  210.  
  211.             swap += curr2->counter_swap;
  212.             asc += curr2->counter_asc;
  213.             dsc += curr2->counter_dsc;
  214.             eq += curr2->counter_eq;
  215.  
  216.             curr1 = curr2;
  217.             curr2 = curr1->next;
  218.         }
  219.  
  220.         pthread_mutex_unlock(&curr1->sync);
  221.         printf("Total: %d asc: %d dsc: %d eq: %d\n", swap, asc, dsc, eq);
  222.        
  223.     }
  224.     return NULL;
  225. }
  226.  
  227. int main() {
  228.     srand(time(NULL));
  229.     Storage *storage = init_storage(STORAGE_CAPACITY);
  230.     fill_storage(storage);
  231.     print_storage(storage);
  232.  
  233.     pthread_t compare_asc_tid, compare_desc_tid, compare_eq_tid, swap_tid1, swap_tid2, swap_tid3, monitor;
  234.  
  235. //    int *counters = calloc(THREAD_COUNT, sizeof(int));
  236.  
  237.     ThreadData compare_asc_data = {storage, ASC};
  238.     ThreadData compare_desc_data = {storage, DESC};
  239.     ThreadData compare_eq_data = {storage, EQ};
  240. //    ThreadData swap_data1 = {storage, &counters[SWAP]};
  241. //    ThreadData swap_data2 = {storage, &counters[SWAP]};
  242. //    ThreadData swap_data3 = {storage, &counters[SWAP]};
  243.  
  244.     pthread_create(&monitor, NULL, count_monitor, storage);
  245.     pthread_create(&compare_asc_tid, NULL, compare_length_thread, &compare_asc_data);
  246.     pthread_create(&compare_desc_tid, NULL, compare_length_thread, &compare_desc_data);
  247.     pthread_create(&compare_eq_tid, NULL, compare_length_thread, &compare_eq_data);
  248.     pthread_create(&swap_tid1, NULL, swap_thread, storage);
  249.     pthread_create(&swap_tid2, NULL, swap_thread, storage);
  250.     pthread_create(&swap_tid3, NULL, swap_thread, storage);
  251.  
  252.     pthread_join(compare_asc_tid, NULL);
  253.     pthread_join(compare_desc_tid, NULL);
  254.     pthread_join(compare_eq_tid, NULL);
  255.     pthread_join(swap_tid1, NULL);
  256.     pthread_join(swap_tid2, NULL);
  257.     pthread_join(swap_tid3, NULL);
  258.     pthread_join(monitor, NULL);
  259.  
  260. //    free(counters); // Free allocated memory
  261.  
  262.     return 0;
  263. }
  264.  
Add Comment
Please, Sign In to add comment