home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 330_02 / test.c < prev    next >
C/C++ Source or Header  |  1990-10-12  |  6KB  |  250 lines

  1.  
  2. /*
  3.    Test program for checking basic CTask functions.
  4.  
  5.    Keyboard input:
  6.  
  7.       e  Terminates program
  8.       d  Snapshot dump to STDOUT
  9.       h  Stop screen output
  10.       c  Resume screen output
  11.       b  Beep (Turbo C only)
  12.       m  Snapshot dump to mono screen
  13.       s  Snapshot dump to colour screen
  14.  
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <conio.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21. #include <time.h>
  22.  
  23. #include "tsk.h"
  24. #include "tsksup.h"
  25. #include "tskprf.h"
  26.  
  27. #define STACKSIZE 2048
  28.  
  29. unsigned int _stklen = 6 * STACKSIZE;  /* For Turbo C: Five tasks + main Task Stack */
  30.  
  31. typedef struct {
  32.                farptr xx;
  33.                char str [20];
  34.                } message;
  35.  
  36. tcb tcb1, tcb2, tcb3, tcb4, tcb5;
  37. mailbox box;
  38. message msg;
  39. flag halt;
  40. pipe pip;
  41. buffer buf;
  42.  
  43. byte pipbuf [10];
  44. word bufbuf [40];
  45.  
  46. int endrun;
  47.  
  48.  
  49. /*
  50.    Task1 sends a mail to Task3, and waits for a response in the buffer.
  51.    The response is then displayed.
  52.    Task1 will stop while the halt flag is set.
  53. */
  54.  
  55. void far task1 (void)
  56. {
  57.    char str [20];
  58.  
  59.    tprintf ("Task 1 started\n");
  60.    while (!endrun)
  61.       {
  62.       wait_flag_clear (&halt, 0L);
  63.  
  64.       t_delay (5L);
  65.       tputch ('1');
  66.       strcpy (msg.str, "From T1");
  67.       send_mail (&box, &msg);
  68.  
  69.       read_buffer (&buf, str, 20, 0L);
  70.       tprintf ("Task 1 read buf: <%s>\n", str);
  71.       }
  72. }
  73.  
  74. /*
  75.    Task2 reads the keyboard. If a character has been read, it is passed
  76.    to Task4 via a pipe. Entering 'h' will set the halt flag (stopping Task1),
  77.    entering 'c' will clear the halt flag.
  78.    'e' stops the program.
  79. */
  80.  
  81. void far task2 (void)
  82. {
  83.    int ch;
  84.  
  85.    tprintf ("Task 2 started\n");
  86.    while (!endrun)
  87.       {
  88.       ch = t_wait_key (36L);
  89.       if (ch < 0)
  90.          tputch (0x04);
  91.       else
  92.          {
  93.          switch (tolower (ch & 0xff))
  94.             {
  95.             case 'h':   set_flag (&halt);
  96.                         break;
  97.             case 'c':   clear_flag (&halt);
  98.                         break;
  99.             case 'e':   wake_task (NULL);
  100.                         break;
  101.    #if (TSK_NAMED)
  102.             case 'd':   csnapshot ();
  103.                         break;
  104.             case 'm':   tsk_set_mono (25, 80);
  105.                         screensnap (25);
  106.                         break;
  107.             case 's':   tsk_set_colour (25, 80);
  108.                         screensnap (25);
  109.                         break;
  110.    #endif
  111.    #if (TSK_TURBO)
  112.             case 'b':   sound (2000);
  113.                         t_delay (2);
  114.                         nosound ();
  115.                         break;
  116.    #endif
  117.             }
  118.  
  119.          if (!endrun)
  120.             write_pipe (&pip, (char)ch, 0L);
  121.          tputch ('2');
  122.          }
  123.       }
  124. }
  125.  
  126.  
  127. /*
  128.    Task3 waits for mail, then sends it back through a buffer.
  129. */
  130.  
  131. void far task3 (void)
  132. {
  133.    message far *m;
  134.  
  135.    tprintf ("Task 3 started\n");
  136.    while (!endrun)
  137.       {
  138.       m = wait_mail (&box, 0L);
  139.       tprintf ("Task 3 received <%Fs>\n", m->str);
  140.  
  141.       m->str [6] = '3';
  142.       write_buffer (&buf, m->str, 7, 0L);
  143.       }
  144. }
  145.  
  146.  
  147. /*
  148.    Task4 waits for a character in the pipe and displays it. To make
  149.    things livelier, it uses a timeout while waiting, and will display
  150.    faces when the timeout occurred before the character.
  151. */
  152.  
  153. void far task4 (void)
  154. {
  155.    int ch;
  156.  
  157.    tprintf ("Task 4 started\n");
  158.    while (!endrun)
  159.       {
  160.       ch = read_pipe (&pip, 10L);
  161.       if (ch < 0)
  162.          tputch (0x02);
  163.       else
  164.          tprintf ("Task 4 got <%c>\n", ch);
  165.       }
  166. }
  167.  
  168. /*
  169.    Task 5 checks the memory watch capability by waiting for the
  170.    BIOS keyboard status byte at 40:17 to contain 1 in the lower two
  171.    bits, signalling that both the left and right shift keys are pressed.
  172. */
  173.  
  174. void far task5 (void)
  175. {
  176.    tprintf ("Task 5 started\n");
  177.    while (!endrun)
  178.       {
  179.          wait_memory ((farptr)0x417L, 0x03, 0x03, TCMP_EQ);
  180.          tprintf ("Task 5: Both Shift keys pressed\n");
  181.          wait_memory ((farptr)0x417L, 0x03, 0x03, TCMP_NE);
  182.          tprintf ("Task 5: Shift keys released\n");
  183.       }
  184. }
  185.  
  186.  
  187. int main (void)
  188. {
  189.    char stack1 [STACKSIZE];
  190.    char stack2 [STACKSIZE];
  191.    char stack3 [STACKSIZE];
  192.    char stack4 [STACKSIZE];
  193.    char stack5 [STACKSIZE];
  194.    int i;
  195.  
  196. #if(TSK_TURBO)
  197.    directvideo = 0;
  198. #endif
  199.    endrun = 0;
  200.    if ((i = install_tasker (0, 0, IFL_DISK | IFL_INT8_DIR | IFL_PRINTER, "Test")) < 0)
  201.       {
  202.       printf ("Can't install, returncode = %d\n", i);
  203.       return 1;
  204.       }
  205.  
  206.    init_conout ();
  207.  
  208.    create_mailbox (&box TN("Mailbox"));
  209.    create_flag (&halt TN("Halt"));
  210.    create_pipe (&pip, pipbuf, sizeof (pipbuf) TN("Pipe"));
  211.    create_buffer (&buf, bufbuf, sizeof (bufbuf) TN("Buffer"));
  212.  
  213.    create_task (&tcb1, task1, stack1, STACKSIZE, PRI_STD, NULL TN("TASK1"));
  214.    create_task (&tcb2, task2, stack2, STACKSIZE, PRI_STD, NULL TN("TASK2"));
  215.    create_task (&tcb3, task3, stack3, STACKSIZE, PRI_STD, NULL TN("TASK3"));
  216.    create_task (&tcb4, task4, stack4, STACKSIZE, PRI_STD, NULL TN("TASK4"));
  217.    create_task (&tcb5, task5, stack5, STACKSIZE, PRI_STD, NULL TN("TASK5"));
  218.    start_task (&tcb1);
  219.    start_task (&tcb2);
  220.    start_task (&tcb3);
  221.    start_task (&tcb4);
  222.    start_task (&tcb5);
  223.    preempt_on ();
  224.  
  225.    t_delay (0L);
  226.  
  227.    endrun = 1;
  228.    tputs ("******** Main Task *********");
  229.  
  230.    kill_task (&tcb1);
  231.    kill_task (&tcb2);
  232.    kill_task (&tcb3);
  233.    kill_task (&tcb4);
  234.    kill_task (&tcb5);
  235.  
  236.    delete_mailbox (&box);
  237.    delete_pipe (&pip);
  238.    delete_buffer (&buf);
  239.    delete_flag (&halt);
  240.  
  241.    schedule ();
  242.  
  243.    end_conout ();
  244.  
  245.    remove_tasker ();
  246.    puts ("******** End Run *********");
  247.    return 0;
  248. }
  249.  
  250.