home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / ctask.zip / TPRT.C < prev    next >
C/C++ Source or Header  |  1988-03-01  |  2KB  |  119 lines

  1.  
  2. /*
  3.    Test program for checking CTask printer buffering.
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <conio.h>
  8. #include <process.h>
  9.  
  10. #include "tsk.h"
  11. #include "prt.h"
  12.  
  13. #define PORT 0    /* LPT1 (mono card) */
  14. #define POLL 0    /* Set to 1 to use polling */
  15.  
  16. #define STACKSIZE 2048
  17.  
  18. word _stklen = 3 * STACKSIZE;  /* For Turbo C: Two tasks + main Task Stack */
  19.  
  20. tcb tcb1, tcb2;
  21. flag halt, ready;
  22.  
  23. byte xmtbuf [1024];
  24. int endrun;
  25.  
  26. FILE *pfile;
  27.  
  28.  
  29. void far task1 (void)
  30. {
  31.    int i;
  32.  
  33.    printf ("Task 1 started\n");
  34.  
  35.    /* Copy file into printer pipe. Using a larger file buffer would be more
  36.       efficient, but this is only a demo.
  37.    */
  38.  
  39.    while (!endrun && ((i = getc (pfile)) != EOF))
  40.       prt_write (PORT, (byte)i, 0L);
  41.  
  42.    if (endrun)
  43.       return;
  44.  
  45.    /* Wait for printer to complete output. */
  46.  
  47.    if (!prt_complete (PORT))
  48.       puts ("Waiting for Printer");
  49.    prt_wait_complete (PORT, 0L);
  50.  
  51.    /* Stop program */
  52.  
  53.    set_flag (&halt);
  54.    set_flag (&ready);
  55. }
  56.  
  57.  
  58. /*
  59.    Second task just for fun. Echoes keyboard characters, aborts printing
  60.    when 'e' is entered.
  61. */
  62.  
  63. void far task2 (void)
  64. {
  65.    int ch;
  66.  
  67.    printf ("Task 2 started\n");
  68.    while (!endrun)
  69.       {
  70.       ch = t_read_key () & 0xff;
  71.       putch (ch);
  72.       endrun = (ch == 'e');
  73.       }
  74.  
  75.    set_flag (&halt);
  76. }
  77.  
  78.  
  79.  
  80. void main (int argc, char **argv)
  81. {
  82.    char stack1 [STACKSIZE];
  83.    char stack2 [STACKSIZE];
  84.  
  85.    if ((pfile = fopen (argv [1], "rb")) == NULL)
  86.       {
  87.       printf ("Error opening file '%s'\n", argv [1]);
  88.       exit (1);
  89.       }
  90.  
  91.    endrun = 0;
  92.    install_tasker (0, 0);
  93.  
  94.    prt_install (PORT, POLL, 100, xmtbuf, sizeof (xmtbuf));
  95.  
  96.    create_task (&tcb1, task1, stack1, STACKSIZE, 100, NULL);
  97.    create_task (&tcb2, task2, stack2, STACKSIZE, 100, NULL);
  98.    create_flag (&halt);
  99.    create_flag (&ready);
  100.    start_task (&tcb1);
  101.    start_task (&tcb2);
  102.  
  103.    preempt_on ();
  104.    wait_flag_set (&halt, 0L);
  105.  
  106.    endrun = 1;
  107.    puts ("******** Main Task *********");
  108.    wait_flag_set (&ready, 0L);
  109.  
  110.    puts ("******** End Run *********");
  111.    preempt_off ();
  112.    prt_remove (PORT);
  113.    remove_tasker ();
  114.  
  115.    fclose (pfile);
  116. }
  117.  
  118.  
  119.