- #include "counter.h"
- #include <math.h>
- /* ============================================================================
- * File-global variables
- * ========================================================================== */
- static int ncounters = 0;
- static struct counter *counters = NULL;
- static int nthreads = 0;
- static int *ninstructions = NULL;
- static struct instruction **instructions = NULL;
- /* ============================================================================
- * Operations
- * ========================================================================== */
- static void
- decrement(long long *n) {
- *n = *n-1;
- }
- static void
- increment(long long *n) {
- *n = *n+1;
- }
- static void
- mult2(long long *n) {
- *n= 2*(*n);
- }
- /* ============================================================================
- * Helper functions
- * ========================================================================== */
- /* ============================================================================
- * Thread function
- * ========================================================================== */
- static void *
- worker_thread(void *arg) {
- int id = (int)arg;
- int instructionNo,i,repetition;
- for(i=0;i<ninstructions[id];i++){
- for(repetition=0;repetition<instructions[id][i].repetitions;repetition++){
- (*instructions[id][i].work_fn)(&instructions[id][i].counter->counter);
- }
- }
- return NULL;
- }
- /* ============================================================================
- * Main function
- * ========================================================================== */
- int
- main(void) {
- // TODO
- //read the number of counters
- scanf("%i\n",&ncounters);
- //read the number of threads
- scanf("%i\n",&nthreads);
- if(nthreads<=0 || ncounters<=0){
- printf("error\n");
- exit(0);
- }
- //allocate memory for the array of counters
- counters = malloc(sizeof(struct counter)*ncounters);
- //allocate memory for the array containing the number of instructions per thread
- ninstructions = (int*)malloc(sizeof(int)*nthreads);
- //allocate memory for the array of pointers **instructions
- instructions = malloc(sizeof(void *)*nthreads);
- //allocate memory for thread array
- pthread_t *threadArray = malloc(sizeof(pthread_t)*nthreads);
- int counterNo,numberOfInstructions,rep,i,j;
- char operation;
- struct counter* thisCounter;
- struct instruction *instructionArray;
- //make counters
- for(int i=0;i<ncounters;i++){
- counters[i].counter = 0;
- }
- //make instructions
- for(i=0;i<nthreads;i++){
- scanf(" %i",&numberOfInstructions);
- ninstructions[i] = numberOfInstructions;
- instructions[i] = malloc(sizeof(struct instruction)*numberOfInstructions);
- for(j=0;j<numberOfInstructions;j++){
- scanf(" &i &c &i",&counterNo,&operation,&rep);
- if(operation=='I')
- instructions[i][j].work_fn = &increment;
- else if(operation=='D')
- instructions[i][j].work_fn = &decrement;
- else if(operation=='2')
- instructions[i][j].work_fn = &mult2;
- instructions[i][j].counter = &counters[counterNo];
- instructions[i][j].repetitions = rep;
- }
- }
- //execute instructions
- for(i=0;i<nthreads;i++){
- pthread_create(&threadArray[i],NULL,worker_thread,(void *)i);
- }
- for(i=0;i<nthreads;i++){
- pthread_join(threadArray[i],NULL);
- }
- for(i=0;i<ncounters;i++){
- printf("%lld\n",counters[i].counter);
- }
- return 0;
- }