#include "csim.h"
#include <bios.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void      generator(void);
void      tellor(void);
void      ender(void);

int       customers = 0;
int       tellors = 1;

int       main(void)
   {
      clrscr();
      start_process(generator,0x0040);

      randomize();

      start_process(tellor,0x0040);
      start_process(tellor,0x0040);
      start_process(tellor,0x0040);

      if(start_kb_process(ender,0x0040))
         exit_processing(3);
      set_time_ratio(1000.0);
      sim_start();
      return(0);
   }

void  generator(void)
   {
      int      post_id;
      post_id = init_post("Customers");
      while(1)
         {
            customers++;
            set_post(post_id,&customers);
            gotoxy(1,1);
            printf("customers = %d  time %d",customers,CurrentTime);
            wait_for_time((rand()%(int)(4.5 _MINUTES_)));
         }
   }
void   tellor(void)
   {
      int   number;
      int   post_id;
      number = tellors++;
      post_id = init_post("Customers");
      while(1)
         {
            gotoxy(2,1+2*number);
            printf("0");
            wait_post(post_id);
            if(customers)
               {
                  gotoxy(2,1+2*number);
                  printf("1");
                  if(customers>1)
                     set_post(post_id,&customers);
                  customers--;
                  gotoxy(1,1);
                  printf("customers = %d  time %ld",customers,CurrentTime);
                  wait_for_time(4_MINUTES_ +rand()%(int)(6_MINUTES_));
                  gotoxy(1,1);
                  printf("customers = %d  time %ld",customers,CurrentTime);
               }
         }
   }
void   ender(void)
   {
      wait_for_time(8 _HOURS_);
      exit_processing(0);
   }
