Advertisement
KillianMills

sigCalc.c

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