home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / CTASK11.ZIP / TPRT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-07-01  |  2.7 KB  |  146 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. #include <ctype.h>
  10.  
  11. #include "tsk.h"
  12. #include "prt.h"
  13.  
  14. #define PORT 0    /* LPT1 (mono card) */
  15. #define POLL 0    /* Set to 1 to use polling */
  16.  
  17. #if (TSK_NAMED)
  18. extern void snapshot (FILE *f);
  19. #endif
  20.  
  21. #define STACKSIZE 2048
  22.  
  23. word _stklen = 3 * STACKSIZE;  /* For Turbo C: Two tasks + main Task Stack */
  24.  
  25. tcb tcb1, tcb2;
  26. flag halt, ready;
  27.  
  28. byte xmtbuf [1024];
  29. int endrun;
  30.  
  31. FILE *pfile;
  32.  
  33.  
  34. void far task1 (void)
  35. {
  36.    int i;
  37.  
  38.    printf ("Task 1 started\n");
  39.  
  40.    /* Copy file into printer pipe. Using a larger file buffer would be more
  41.       efficient, but this is only a demo.
  42.    */
  43.  
  44.    while (!endrun && ((i = getc (pfile)) != EOF))
  45.       prt_write (PORT, (byte)i, 0L);
  46.  
  47.    if (endrun)
  48.       return;
  49.  
  50.    /* Wait for printer to complete output. */
  51.  
  52.    if (!prt_complete (PORT))
  53.       puts ("Waiting for Printer");
  54.    prt_wait_complete (PORT, 0L);
  55.  
  56.    /* Stop program */
  57.  
  58.    set_flag (&halt);
  59.    set_flag (&ready);
  60. }
  61.  
  62.  
  63. /*
  64.    Second task just for fun. Echoes keyboard characters, aborts printing
  65.    when 'e' is entered. Also outputs snapshot dump on 'd'.
  66. */
  67.  
  68. void far task2 (void)
  69. {
  70.    int ch;
  71.  
  72.    printf ("Task 2 started\n");
  73.    while (!endrun)
  74.       {
  75.       ch = t_read_key () & 0xff;
  76.       putch (ch);
  77.       ch = tolower (ch);
  78.       endrun = (ch == 'e');
  79. #if (TSK_NAMED)
  80.       if (ch == 'd')
  81.          snapshot (stdout);
  82. #endif
  83.       }
  84.  
  85.    set_flag (&halt);
  86. }
  87.  
  88.  
  89.  
  90. void main (int argc, char **argv)
  91. {
  92.    char stack1 [STACKSIZE];
  93.    char stack2 [STACKSIZE];
  94.  
  95.    if ((pfile = fopen (argv [1], "rb")) == NULL)
  96.       {
  97.       printf ("Error opening file '%s'\n", argv [1]);
  98.       exit (1);
  99.       }
  100.  
  101.    endrun = 0;
  102.    install_tasker (0, 0);
  103.  
  104.    prt_install (PORT, POLL, PRI_STD, xmtbuf, sizeof (xmtbuf));
  105.  
  106.    create_task (&tcb1, task1, stack1, STACKSIZE, PRI_STD, NULL
  107. #if (TSK_NAMEPAR)
  108.                 ,"TASK1"
  109. #endif
  110.                 );
  111.    create_task (&tcb2, task2, stack2, STACKSIZE, PRI_STD, NULL
  112. #if (TSK_NAMEPAR)
  113.                 ,"TASK2"
  114. #endif
  115.                 );
  116.    create_flag (&halt
  117. #if (TSK_NAMEPAR)
  118.                 ,"Halt"
  119. #endif
  120.                 );
  121.    create_flag (&ready
  122. #if (TSK_NAMEPAR)
  123.                 ,"Ready"
  124. #endif
  125.                 );
  126.  
  127.    start_task (&tcb1);
  128.    start_task (&tcb2);
  129.  
  130.    preempt_on ();
  131.    wait_flag_set (&halt, 0L);
  132.  
  133.    endrun = 1;
  134.    puts ("******** Main Task *********");
  135.    wait_flag_set (&ready, 0L);
  136.  
  137.    puts ("******** End Run *********");
  138.    preempt_off ();
  139.    prt_remove (PORT);
  140.    remove_tasker ();
  141.  
  142.    fclose (pfile);
  143. }
  144.  
  145.  
  146.