Advertisement
KillianMills

threadCalcDraft.c

Nov 6th, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.52 KB | None | 0 0
  1. //Killian Mills 11368701
  2. //Mark McCluskey 12514857
  3.  
  4. #include <pthread.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include <signal.h>
  9.  
  10. #define ONE_SECOND 1000000
  11. #define RANGE 10
  12. #define PERIOD 2
  13.  
  14. typedef struct {
  15.     int firstNum; //first sigHoldber to be added
  16.     int secondNum; //second sigHoldber to be added
  17.     char *fileName; //a string containing the filename
  18.  
  19.     pthread_t mainRef; //our reference to the main thread
  20.  
  21. }shared;
  22.  
  23.  
  24. //reader thread reads from file and prints what is has read
  25. static void * reader(void *shared_in){
  26.  
  27.     shared *structOb = (shared *)shared_in;
  28.  
  29.     sigset_t readBlock;
  30.     sigemptyset(&readBlock);
  31.     sigaddset(&readBlock, SIGUSR1);
  32.     pthread_sigmask(SIG_BLOCK, &readBlock, NULL);
  33.    
  34.     int sigHold;
  35.        
  36.     FILE *fr;
  37.     fr = fopen(structOb->fileName, "r");
  38.  
  39.     fscanf(fr, "%d", &structOb->firstNum);
  40.     fscanf(fr, "%d", &structOb->secondNum);
  41.     sigwait(&readBlock,&sigHold);
  42.  
  43.     while(!feof(fr)){
  44.  
  45.         printf("Read in first number %d\n", structOb->firstNum);
  46.         printf("Read in second number %d\n", structOb->secondNum);
  47.         int randomSleep = rand() % 10000;
  48.         usleep( randomSleep );
  49.  
  50.         pthread_kill(structOb->mainRef, SIGUSR1); // thread version
  51.  
  52.         sigwait(&readBlock,&sigHold);
  53.         fscanf(fr, "%d", &structOb->firstNum);
  54.         fscanf(fr, "%d", &structOb->secondNum);
  55.  
  56.     }
  57.        
  58.     fclose(fr); // close the file
  59.     pthread_kill(structOb->mainRef, SIGINT); // signal to main that reader has finished
  60.  
  61.     return ((void *)NULL);
  62. }
  63.  
  64.  
  65. //calculator thread adds two sigHoldbers together and prints result
  66. static void * calculator(void *shared_in){
  67.    
  68.     shared *structOb = (shared *)shared_in;
  69.  
  70.     sigset_t calcBlock;
  71.     sigemptyset(&calcBlock);
  72.     sigaddset(&calcBlock, SIGUSR2);
  73.     pthread_sigmask(SIG_BLOCK, &calcBlock, NULL);
  74.  
  75.     int sigHold;
  76.    
  77.     while(1){
  78.  
  79.         sigwait(&calcBlock,&sigHold); // wait for main to tell me to work
  80.  
  81.         int result = (structOb->firstNum + structOb->secondNum);
  82.         printf("Result: %d + %d = %d\n\n", structOb->firstNum, structOb->secondNum, result);
  83.  
  84.         pthread_kill(structOb->mainRef,SIGUSR2); // thread version
  85.    
  86.     }
  87.    
  88.     printf("Goodbye from calculator\n");
  89.     return ((void *)NULL);
  90. }
  91.  
  92.  
  93. int  main(int argc, char *argv[]){
  94.     pthread_t readThread, calcThread; // make reader and calculator threads
  95.  
  96.     shared structOb; // declares the shared struct
  97.     structOb.mainRef = pthread_self(); // makes t the reference to the main
  98.     structOb.fileName = argv[1]; //    
  99.  
  100.     // adds the three signals we are using to the main thread's signal blocker
  101.     sigset_t mainBlock;
  102.     sigemptyset(&mainBlock);
  103.     sigaddset(&mainBlock, SIGUSR1);
  104.     sigaddset(&mainBlock, SIGUSR2);
  105.     sigaddset(&mainBlock, SIGINT);
  106.     pthread_sigmask(SIG_BLOCK, &mainBlock, NULL);
  107.    
  108.      //Create our threads
  109.     pthread_create(&readThread, NULL, reader, (void *)&structOb);
  110.     pthread_create(&calcThread, NULL, calculator, (void *)&structOb);
  111.  
  112.     int sigHold; // hold the value of the current signal
  113.    
  114.     while(1){ // loop forver for the moment
  115.  
  116.         pthread_kill(readThread, SIGUSR1); // send reader to work
  117.         sigwait(&mainBlock,&sigHold); // wait for reader to be done
  118.  
  119.         // if a sigint is sent, close the threads
  120.         if (sigHold == SIGINT){
  121.             pthread_cancel(readThread);
  122.             pthread_cancel(calcThread);
  123.             printf("goodbye from reader\n");
  124.             printf("goodbye from calculator\n");
  125.             break;
  126.         }
  127.  
  128.         pthread_kill(calcThread, SIGUSR2); // send calculator to work
  129.         sigwait(&mainBlock,&sigHold); // wait for calculator to be done
  130.  
  131.     }
  132.  
  133.     //Wait for our threads
  134.     pthread_join(readThread, NULL);
  135.     pthread_join(calcThread, NULL);
  136.    
  137.     return (0);
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement