Advertisement
peepraeza

Untitled

Sep 21st, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.22 KB | None | 0 0
  1. #include <stdio.h> 
  2.     #include <stdlib.h>
  3.     #include <pthread.h>
  4.    
  5.     void *avg_func(void *str);
  6.     void *min_func(void *ptr);
  7.     void *max_func(void *ptr);
  8.    
  9.  
  10.     double avg;      
  11.     int min;
  12.     int max;
  13.    
  14.  
  15.     typedef struct datastruct
  16.     {
  17.        int size;
  18.        int * values;
  19.     }datastruct;
  20.    
  21.  
  22.     main(int argc, char *argv[])
  23.     {
  24.         printf("\n\nWelcome to paheeThredz, by Sean Staz\n\n");
  25.        while(argc <=1)
  26.        {
  27.            printf("Incorrect input. No arguments entered.\n");
  28.            printf("Please enter one or more inputs.\n");
  29.            exit(0);
  30.         }
  31.      
  32.        int i = 0;
  33.        int copy[argc-1];
  34.        for(i; i < (argc -1); i++)
  35.        {
  36.            copy[i] = atoi(argv[i+1]);
  37.        }
  38.          
  39.        pthread_t thread1, thread2, thread3;
  40.        const char *message1 = "This is Thread 1";
  41.        const char *message2 = "This is Thread 2";
  42.        const char *message3 = "This is Thread 3";
  43.        int  t1, t2, t3;
  44.    
  45.        printf("Running: %s\n\n", argv[0]);
  46.      
  47.        datastruct ds = {argc - 1, copy};
  48.    
  49.        /* Create independent threads each of which will execute appropriate function*/
  50.        t1 = pthread_create(&thread1, NULL, (void *) avg_func, (void *) &ds);
  51.        if(t1)
  52.        {
  53.            fprintf(stderr,"Error - pthread_create() return code: %d\n", t1);
  54.            exit(EXIT_FAILURE);
  55.        }
  56.    
  57.        t2 = pthread_create(&thread2, NULL, (void *) min_func, (void *) &ds);
  58.        if(t2)
  59.        {
  60.            fprintf(stderr,"Error - pthread_create() return code: %d\n",t2);
  61.            exit(EXIT_FAILURE);
  62.        }
  63.        
  64.        t3 = pthread_create(&thread3, NULL, (void *) max_func, (void *) &ds);
  65.        if(t3)
  66.        {
  67.            fprintf(stderr,"Error - pthread_create() return code: %d\n", t3);
  68.            exit(EXIT_FAILURE);
  69.        }
  70.    
  71.        printf("pthread_create() for Thread 1 returns: %d\n",t1);
  72.        printf("pthread_create() for Thread 2 returns: %d\n",t2);
  73.        printf("pthread_create() for Thread 3 returns: %d\n\n",t3);
  74.    
  75.        /* Wait till threads are complete before main continues. */
  76.    
  77.        pthread_join(thread1, NULL);
  78.        pthread_join(thread2, NULL);
  79.        pthread_join(thread3, NULL);
  80.    
  81.        printf("The average:  %g\n", avg);
  82.        printf("The minimum:  %d\n", min);
  83.        printf("The maximum:  %d\n", max);
  84.    
  85.        exit(EXIT_SUCCESS);
  86.     }
  87.    
  88.     void *avg_func(void *ptr)
  89.     {
  90.        datastruct * copy;
  91.        copy = (datastruct *) ptr;
  92.      
  93.        int sz = copy->size;
  94.        int i;
  95.      
  96.        for(i = 0; i < sz; i++)
  97.        {
  98.            avg += (copy->values[i]);  
  99.        }                               //If I used double for avg it would have given 82.8571 which doesn't match the example output
  100.        avg = (int)(avg / sz);          //Used cast to match example output given on instructions.
  101.     }
  102.    
  103.  
  104.     void *min_func(void *ptr)
  105.     {
  106.        datastruct * copy;
  107.        copy = (datastruct *) ptr;
  108.      
  109.        int sz = copy->size;
  110.        int i;
  111.      
  112.        min = (copy->values[0]);
  113.        for(i = 1; i < sz; i++)
  114.        {
  115.            if(min > (copy->values[i]))
  116.            {
  117.                min = (copy->values[i]);
  118.            }
  119.        }
  120.     }
  121.    
  122.  
  123.     void *max_func(void *ptr)
  124.     {
  125.        datastruct * copy;
  126.        copy = (datastruct *) ptr;
  127.      
  128.        int sz = copy->size;
  129.        int i;
  130.      
  131.        max = copy->values[0];
  132.      
  133.        for(i = 1; i < sz; i++)
  134.        {
  135.            if(max < copy->values[i])
  136.            {
  137.                max = copy->values[i];
  138.            }
  139.        }
  140.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement