title: Pthread and Signal Assignment in C
body: <p>I have an assignment and i'm stuck and I have no idea where the problem is, but I have to use one thread to takes in two numbers from a file, prints the numbers on screen, then another thread adds them two numbers together and prints them on screen.
  Here's what I have so far but I can't seem to get it to work properly any help would be greatly appreciated.
  <strong>I should point out, I have to do this without the use of mutex's and semaphores.</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/wait.h&gt;
  
  typedef struct
  {
       int a;
       int b;
  } pair_t;
  
  FILE *f;
  
  static void * reader(void *data){
      pair_t *d = (pair_t *)data;
      sigset_t catch;
      int flag;
  
      sigemptyset(&amp;catch);
  
      sigaddset(&amp;catch, SIGUSR1);
  
      while(!feof(f)){
          sigwait(&amp;catch, &amp;flag);
          fscanf(f,"%d", &amp;d-&gt;a);
          fscanf(f,"%d", &amp;d-&gt;b);
          printf("Thread 1 submitting : %d %d", d-&gt;a, d-&gt;b);
          sleep(5);
      }
      return 0;
  }
  
  static void * calculator(void *data){
      pair_t *pa = (pair_t *)data;
      sigset_t catch;
      sigemptyset(&amp;catch);
      sigaddset(&amp;catch, SIGUSR1);
      int flag;
  
      while(1){
          sigwait(&amp;catch, &amp;flag);
          printf("Thread 2 calculated = %d\n",(pa-&gt;a + pa-&gt;b));
          sleep(5);
      }
      return 0;
  }
  
  int main(int argc, char *argv[]){
      f = fopen(argv[1],"r");
      if(f ==NULL){
          perror; 
      }
      if(f !=NULL){
          pair_t * p;
  
          sigset_t set;
          sigemptyset(&amp;set);
          sigaddset(&amp;set, SIGUSR1);
  
          pthread_t t1;
          pthread_t t2;
  
          pthread_sigmask(SIG_BLOCK, &amp;set, NULL);
          pthread_create(&amp;t1, NULL, reader, (void *)&amp;p);
          pthread_create(&amp;t2, NULL, calculator, (void *)&amp;p);
  
          pthread_join(t1, NULL);
          printf("Goodbye from Thread One");
          pthread_join(t2, NULL);
          printf("Goodbye from Thread Two");
  
          pthread_exit(NULL); 
      }
      return 0;
  }
  </code></pre>