Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Killian Mills 11368701
- //Mark McCluskey 12514857
- #include <pthread.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <signal.h>
- #define ONE_SECOND 1000000
- #define RANGE 10
- #define PERIOD 2
- typedef struct {
- int firstNum; //first sigHoldber to be added
- int secondNum; //second sigHoldber to be added
- char *fileName; //a string containing the filename
- pthread_t mainRef; //our reference to the main thread
- }shared;
- //reader thread reads from file and prints what is has read
- static void * reader(void *shared_in){
- shared *structOb = (shared *)shared_in;
- sigset_t readBlock;
- sigemptyset(&readBlock);
- sigaddset(&readBlock, SIGUSR1);
- pthread_sigmask(SIG_BLOCK, &readBlock, NULL);
- int sigHold;
- FILE *fr;
- fr = fopen(structOb->fileName, "r");
- fscanf(fr, "%d", &structOb->firstNum);
- fscanf(fr, "%d", &structOb->secondNum);
- sigwait(&readBlock,&sigHold);
- while(!feof(fr)){
- printf("Read in first number %d\n", structOb->firstNum);
- printf("Read in second number %d\n", structOb->secondNum);
- int randomSleep = rand() % 10000;
- usleep( randomSleep );
- pthread_kill(structOb->mainRef, SIGUSR1); // thread version
- sigwait(&readBlock,&sigHold);
- fscanf(fr, "%d", &structOb->firstNum);
- fscanf(fr, "%d", &structOb->secondNum);
- }
- fclose(fr); // close the file
- pthread_kill(structOb->mainRef, SIGINT); // signal to main that reader has finished
- return ((void *)NULL);
- }
- //calculator thread adds two sigHoldbers together and prints result
- static void * calculator(void *shared_in){
- shared *structOb = (shared *)shared_in;
- sigset_t calcBlock;
- sigemptyset(&calcBlock);
- sigaddset(&calcBlock, SIGUSR2);
- pthread_sigmask(SIG_BLOCK, &calcBlock, NULL);
- int sigHold;
- while(1){
- sigwait(&calcBlock,&sigHold); // wait for main to tell me to work
- int result = (structOb->firstNum + structOb->secondNum);
- printf("Result: %d + %d = %d\n\n", structOb->firstNum, structOb->secondNum, result);
- pthread_kill(structOb->mainRef,SIGUSR2); // thread version
- }
- printf("Goodbye from calculator\n");
- return ((void *)NULL);
- }
- int main(int argc, char *argv[]){
- pthread_t readThread, calcThread; // make reader and calculator threads
- shared structOb; // declares the shared struct
- structOb.mainRef = pthread_self(); // makes t the reference to the main
- structOb.fileName = argv[1]; //
- // adds the three signals we are using to the main thread's signal blocker
- sigset_t mainBlock;
- sigemptyset(&mainBlock);
- sigaddset(&mainBlock, SIGUSR1);
- sigaddset(&mainBlock, SIGUSR2);
- sigaddset(&mainBlock, SIGINT);
- pthread_sigmask(SIG_BLOCK, &mainBlock, NULL);
- //Create our threads
- pthread_create(&readThread, NULL, reader, (void *)&structOb);
- pthread_create(&calcThread, NULL, calculator, (void *)&structOb);
- int sigHold; // hold the value of the current signal
- while(1){ // loop forver for the moment
- pthread_kill(readThread, SIGUSR1); // send reader to work
- sigwait(&mainBlock,&sigHold); // wait for reader to be done
- // if a sigint is sent, close the threads
- if (sigHold == SIGINT){
- pthread_cancel(readThread);
- pthread_cancel(calcThread);
- printf("goodbye from reader\n");
- printf("goodbye from calculator\n");
- break;
- }
- pthread_kill(calcThread, SIGUSR2); // send calculator to work
- sigwait(&mainBlock,&sigHold); // wait for calculator to be done
- }
- //Wait for our threads
- pthread_join(readThread, NULL);
- pthread_join(calcThread, NULL);
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement