SHARE
TWEET

Untitled

a guest Apr 21st, 2019 115 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * SO, 2019
  3.  * Lab #8
  4.  *
  5.  * Task #3, lin
  6.  *
  7.  * Is malloc thread safe?
  8.  */
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <pthread.h>
  13. #include <unistd.h>
  14.  
  15. #include <utils.h>
  16. #include "critical.h"
  17.  
  18. #define NUM_THREADS 10
  19. #define NUM_ROUNDS  10000
  20. #define NUM_ROWS    100
  21. #define NUM_COLUMNS 1000
  22.  
  23. #define PRINT_HDR_SZ    32
  24. #define PRINT_BUF_SZ    256
  25. #define MAIN_PRINT      (-1)
  26.  
  27. int global_storage;
  28.  
  29. static void increase_numbers(int *a, int *b, int*c) {
  30.     (*a)++;
  31.     (*b)++;
  32.     (*c)++;
  33. }
  34.  
  35. static void print_stats(int origin,
  36.                         int *global_storage,
  37.                         int *function_global_storage,
  38.                         int *function_specific_storage)
  39. {
  40.     char hdr[PRINT_HDR_SZ] = { 0 };
  41.     char buf[PRINT_BUF_SZ] = { 0 };
  42.  
  43.     /* TODO1: Enter critical section using acquire_lock */
  44.     acquire_lock();
  45.  
  46.     if (origin == MAIN_PRINT)
  47.         sprintf(hdr, "Final report:\n");
  48.     else
  49.         sprintf(hdr, "Thread %d report:\n", origin);
  50.  
  51.     snprintf(buf, strlen(hdr) + 1, "%s\n", hdr);
  52.  
  53.     sprintf(buf + strlen(buf),
  54.             "sum_of(global_storage)=%d\n", *global_storage);
  55.     sprintf(buf + strlen(buf),
  56.             "sum_of(function_global_storage)=%d\n", *function_global_storage);
  57.     sprintf(buf + strlen(buf),
  58.             "sum_of(function_specific_storage)=%d\n", *function_specific_storage);
  59.  
  60.     printf("%s", buf);
  61.  
  62.     /* TODO1: Leave critical section using release_lock */
  63.     release_lock();
  64. }
  65.  
  66. void *thread_function(void *args)
  67. {
  68.     static int function_global_storage;
  69.     int function_specific_storage = 0;
  70.     size_t i, j;
  71.     char **a;
  72.  
  73.     for (int i = 0; i < NUM_ROUNDS; i++) {
  74.  
  75.         /* TODO1: Enter critical section using acquire_lock */
  76.     acquire_lock();
  77.        
  78.         increase_numbers(&global_storage,
  79.                          &function_global_storage,
  80.                          &function_specific_storage);
  81.  
  82.         /* TODO1: Leave critical section using release_lock */
  83.     release_lock();
  84.     }
  85.  
  86.     print_stats((long)args,
  87.                 &global_storage,
  88.                 &function_global_storage,
  89.                 &function_specific_storage);
  90.  
  91.     a = malloc(1000 * sizeof(char *));
  92.     for (i = 0; i < NUM_ROWS; i++) {
  93.         a[i] = malloc(NUM_COLUMNS);
  94.  
  95.         for (j = 0; j < NUM_COLUMNS; j++)
  96.             a[i][j] = rand() % (NUM_COLUMNS * NUM_ROWS);
  97.     }
  98.  
  99.     return a;
  100. }
  101.  
  102. void free_mat(void **mat)
  103. {
  104.     /* Free the memory allocated by each thread */
  105.  
  106.     size_t i;
  107.     char **a = (char **) mat;
  108.  
  109.     for (i = 0; i < NUM_ROUNDS/100; i++)
  110.         free(a[i]);
  111.     free(a);
  112. }
  113.  
  114. int main(void)
  115. {
  116.     static int function_global_storage;
  117.     int function_specific_storage = 0;
  118.    
  119.     pthread_t th[NUM_THREADS];
  120.     int rc;
  121.     size_t i;
  122.     void *mat[NUM_THREADS];
  123.    
  124.     for (i = 0; i < NUM_THREADS; i++) {
  125.         rc = pthread_create(&th[i], NULL, thread_function, (void *) i);
  126.         DIE(rc != 0, "pthread_create");
  127.     }
  128.  
  129.     for (i = 0; i < NUM_ROUNDS; i++) {
  130.    
  131.         /* TODO1: Enter critical section using acquire_lock */
  132.     acquire_lock();
  133.  
  134.         increase_numbers(&global_storage,
  135.                          &function_global_storage,
  136.                          &function_specific_storage);
  137.  
  138.         /* TODO1: Leave critical section using release_lock */
  139.     release_lock();
  140.     }
  141.  
  142.     for (i = 0; i < NUM_THREADS; i++) {
  143.         rc = pthread_join(th[i], &mat[i]);
  144.         DIE(rc != 0, "pthread_join");
  145.     }
  146.    
  147.     /* Freeing */
  148.     for (i = 0; i < NUM_THREADS; i++)
  149.         free_mat(mat[i]);
  150.    
  151.     /* Extract final report */
  152.     print_stats(MAIN_PRINT,
  153.                 &global_storage,
  154.                 &function_global_storage,
  155.                 &function_specific_storage);
  156.  
  157.     return 0;
  158. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top