Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 15th, 2012  |  syntax: None  |  size: 3.40 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include "counter.h"
  2. #include <math.h>
  3.  
  4. /* ============================================================================
  5.  * File-global variables
  6.  * ========================================================================== */
  7. static int ncounters = 0;
  8. static struct counter *counters = NULL;
  9.  
  10. static int nthreads = 0;
  11. static int *ninstructions = NULL;
  12. static struct instruction **instructions = NULL;
  13.  
  14.  
  15. /* ============================================================================
  16.  * Operations
  17.  * ========================================================================== */
  18. static void
  19. decrement(long long *n) {
  20.         *n = *n-1;
  21. }
  22.  
  23. static void
  24. increment(long long *n) {
  25.         *n = *n+1;
  26. }
  27.  
  28. static void
  29. mult2(long long *n) {
  30.         *n= 2*(*n);
  31. }
  32.  
  33.  
  34. /* ============================================================================
  35.  * Helper functions
  36.  * ========================================================================== */
  37.  
  38.  
  39.  
  40. /* ============================================================================
  41.  * Thread function
  42.  * ========================================================================== */
  43. static void *
  44. worker_thread(void *arg) {
  45.        
  46.         int id = (int)arg;
  47.        
  48.         int instructionNo,i,repetition;
  49.         for(i=0;i<ninstructions[id];i++){
  50.                 for(repetition=0;repetition<instructions[id][i].repetitions;repetition++){
  51.                         (*instructions[id][i].work_fn)(&instructions[id][i].counter->counter);
  52.                 }
  53.         }
  54.         return NULL;
  55. }
  56.  
  57.  
  58. /* ============================================================================
  59.  * Main function
  60.  * ========================================================================== */
  61. int
  62. main(void) {
  63.         // TODO
  64.         //read the number of counters
  65.         scanf("%i\n",&ncounters);
  66.        
  67.         //read the number of threads
  68.         scanf("%i\n",&nthreads);
  69.        
  70.         if(nthreads<=0 || ncounters<=0){
  71.                 printf("error\n");
  72.                 exit(0);
  73.         }
  74.        
  75.         //allocate memory for the array of counters
  76.         counters = malloc(sizeof(struct counter)*ncounters);
  77.         //allocate memory for the array containing the number of instructions per thread
  78.         ninstructions = (int*)malloc(sizeof(int)*nthreads);
  79.         //allocate memory for the array of pointers **instructions
  80.         instructions = malloc(sizeof(void *)*nthreads);
  81.         //allocate memory for thread array
  82.         pthread_t *threadArray = malloc(sizeof(pthread_t)*nthreads);
  83.         int counterNo,numberOfInstructions,rep,i,j;
  84.         char operation;
  85.         struct counter* thisCounter;
  86.         struct instruction *instructionArray;
  87.         //make counters
  88.         for(int i=0;i<ncounters;i++){
  89.                 counters[i].counter = 0;
  90.         }
  91.                
  92.         //make instructions
  93.         for(i=0;i<nthreads;i++){
  94.                 scanf(" %i",&numberOfInstructions);
  95.                
  96.                 ninstructions[i] = numberOfInstructions;
  97.                 instructions[i] = malloc(sizeof(struct instruction)*numberOfInstructions);
  98.                 for(j=0;j<numberOfInstructions;j++){
  99.                         scanf(" &i &c &i",&counterNo,&operation,&rep);
  100.                        
  101.                        
  102.                         if(operation=='I')
  103.                                 instructions[i][j].work_fn = &increment;
  104.                         else if(operation=='D')
  105.                                 instructions[i][j].work_fn = &decrement;
  106.                         else if(operation=='2')
  107.                                 instructions[i][j].work_fn = &mult2;
  108.                         instructions[i][j].counter = &counters[counterNo];
  109.                         instructions[i][j].repetitions = rep;
  110.                        
  111.                        
  112.                 }
  113.         }
  114.         //execute instructions
  115.         for(i=0;i<nthreads;i++){
  116.                
  117.                 pthread_create(&threadArray[i],NULL,worker_thread,(void *)i);
  118.         }
  119.         for(i=0;i<nthreads;i++){
  120.                 pthread_join(threadArray[i],NULL);
  121.         }
  122.         for(i=0;i<ncounters;i++){
  123.                 printf("%lld\n",counters[i].counter);
  124.         }
  125.        
  126.        
  127.         return 0;
  128. }