#include <stdio.h>
#include <stdlib.h>
#include <synch.h>
#include <thread.h>

sema_t prod_sem, con_sem;

char c;

void *producer( )
{
      FILE *fp;
      if ((fp=fopen("t.txt", "r")) == NULL)
      {
            puts("Can not open file.\n");
            exit(1);
      }
      while ((c = getc(fp)) != EOF)
      {
            sema_post(&con_sem);
            sema_wait(&prod_sem);
      }
      fclose(fp);
      c = 'Z';
      sema_post(&con_sem);
}

void *consumer()
{
      while (c!= 'Z')
      {
            sema_wait(&con_sem);
            printf(" % c ", c);
           
           sema_post(&prod_sem);
      }
}



int main()
{
      thread_t pro, con;

      sema_init(&prod_sem, 0, 0, NULL);
      sema_init(&con_sem, 0, 0, NULL);

      thr_create(0, 0, producer, 0, 1, 0);
      thr_create(0, 0, consumer, 0, 1, 0);

      while(thr_join(0, 0, 0) == 0);

      return 1;
}