Advertisement
Guest User

Untitled

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