Guest User

Untitled

a guest
Jun 25th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.65 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <pthread.h>
  5.  
  6. /*
  7.  * Default values for parameterized variables.
  8.  */
  9. static int num_accts = 1024;   // number of bank accounts
  10. static int num_trans = 10000;  // number of transactions
  11. static int think_time = 50;    // amount of "thinking time"
  12. static int num_threads = 1;
  13. /*
  14.  * Struct representing a single bank account
  15.  */
  16. struct acct_t {
  17.     int bal;
  18. };
  19. void *transact(void *dummy);
  20. /*
  21.  * Pointer to accounts
  22.  */
  23. struct acct_t *accts;
  24.  
  25. /*
  26.  * Method to perform a number of transactions.
  27.  * Parameter "dummy" is not used.
  28.  */
  29. void *transact(void *dummy)
  30. {
  31.     int i;
  32.     for (i = 0; i < num_trans; i++) {
  33.         // pick two random accounts
  34.         int acct_id_from = rand() % num_accts;
  35.         int acct_id_to = rand() % num_accts;
  36.         // pick a random amount
  37.         int amt = rand() % 100;
  38.        
  39.         // try to transfer the money
  40.         if (accts[acct_id_from].bal > amt) {
  41.             accts[acct_id_from].bal -= amt;
  42.             accts[acct_id_to].bal += amt;
  43.            
  44.            
  45.             amt *= think_time; while (amt--) ;
  46.         }
  47.        
  48.        
  49.     }
  50. }
  51.  
  52. int main(int argc, char **argv)
  53. {
  54.    
  55.     // make sure the number of arguments is odd (including the program name)
  56.     if (!(argc == 1 || argc == 3 || argc == 5 || argc == 7 || argc == 9)) {
  57.         fprintf(stderr, "usage: %s [-a <accts>] [-i <transactions>] [-t <think-time>][-n <thread-numbers>]\n", argv[0]);
  58.         exit(-1);
  59.     }
  60.    
  61.     // look at each runtime argument and see which value it's attempting to set
  62.     int i;
  63.     for (i = 1; i < argc; i++) {
  64.         if (!strcmp(argv[i], "-a")) {
  65.             num_accts = atoi(argv[i+1]);
  66.             i++;
  67.         } else if (!strcmp(argv[i], "-i")) {
  68.             num_trans = atoi(argv[i+1]);
  69.             i++;
  70.         } else if (!strcmp(argv[i], "-t")) {
  71.             think_time = atoi(argv[i+1]);
  72.             i++;
  73.         } else if (!strcmp(argv[i], "-n")) {
  74.             num_threads = atoi(argv[i+1]);
  75.             i++;
  76.         }else {
  77.             fprintf(stderr, "usage: %s [-a <accts>] [-i <transactions>] [-t <think-time>] [-n <thread-numbers>]\n", argv[0]);
  78.             exit(-1);
  79.         }
  80.     }
  81.    
  82.     // display the parameters that will be used for this test run
  83.     fprintf(stderr, "%s: -a %d -i %d -t %d -n %d\n", argv[0], num_accts, num_trans, think_time, num_threads);
  84.    
  85.     // initialize the random number generator
  86.     srand(1);
  87.    
  88.     // create the bank accounts
  89.     accts = (struct acct_t *)malloc(num_accts * sizeof(struct acct_t));
  90.    
  91.     // initialize the bank accounts' values and keep track of the total sum in all accounts
  92.     int original_sum = 0;
  93.     for (i = 0; i < num_accts; i++) {
  94.         accts[i].bal = rand() % 1000;
  95.         original_sum += accts[i].bal;
  96.     }
  97.     pthread_t mythreads[num_threads];
  98.     for (i = 0; i < num_threads; i++)
  99.     {
  100.         pthread_create(&mythreads[i], NULL, transact, NULL);
  101.     }
  102.    
  103.     for (i = 0; i < num_threads; i++)
  104.     {
  105.         pthread_join(mythreads[i], NULL);
  106.     }
  107.     // call the transact function to do the transfers
  108.     //transact(NULL);
  109.    
  110.     // find the total sum of all accounts after the transfers are done
  111.     int sum = 0;
  112.     for (i = 0; i < num_accts; i++) {
  113.         sum += accts[i].bal;
  114.     }
  115.    
  116.     // if the sum is not equal to the original sum, then we had a race condition!!
  117.     if (sum != original_sum) {
  118.         fprintf(stderr, "ERROR! original_sum = %d, sum = %d\n", original_sum, sum);
  119.     }
  120.     else {
  121.         fprintf(stderr, "Values are still consistent\n");
  122.     }
  123.     pthread_exit(NULL);
  124.     return 0;
  125. }
Add Comment
Please, Sign In to add comment