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

  1.  
  2. /*
  3.    Test program for checking CTask DOS spawn compatibility.
  4.  
  5.    NOTE: Task 1 does a "screensnap" every second. This is only useful
  6.          in a dual-monitor system.
  7.          The monitor to use is determined by the program parameters:
  8.             SPAWNxx   - Don't do screensnap.
  9.             SPAWNxx M - Use mono monitor
  10.             SPAWNxx C - Use colour monitor
  11.  
  12.    NOTE: Task 3 repeatedly opens and closes the file 'C:\STATE.RST'
  13.          to test background DOS operations. If this file does not
  14.          exist, or can't be opened, the program will beep (Turbo C)
  15.          or output an error message (MS C). You should have a file
  16.          with that name in the C: root directory, or change Task 3.
  17.  
  18.    Keyboard input while SPAWN is in control:
  19.  
  20.       e  Terminates program
  21.       d  Snapshot dump to STDOUT
  22.       h  Stop screensnap output
  23.       c  Resume screensnap output
  24.       s  Spawn COMMAND.COM. Enter EXIT to return to SPAWN.
  25.       b  Beep (Turbo C only)
  26. */
  27.  
  28. #include <stdio.h>
  29. #include <conio.h>
  30. #include <string.h>
  31. #include <ctype.h>
  32. #include <process.h>
  33. #include <stdlib.h>
  34. #include <fcntl.h>
  35. #include <io.h>
  36.  
  37. #include "tsk.h"
  38. #include "tsksup.h"
  39. #include "tskprf.h"
  40.  
  41. #if (TSK_MSC)
  42. #define sound(x)
  43. #define nosound()
  44. #endif
  45.  
  46. #define SEROUT    1     /* If non-0, tasks will signal activity on
  47.                            COM1-Port. */
  48.  
  49. #if (SEROUT)
  50. #define outser(p,x)    tsk_outp(p,x)
  51. #else
  52. #define outser(p,x)
  53. #endif
  54.  
  55. #define STACKSIZE 2048
  56.  
  57. unsigned int _stklen = 8 * STACKSIZE;  /* For Turbo C: Four tasks + main Task Stack */
  58.  
  59. typedef struct {
  60.                farptr xx;
  61.                char str [20];
  62.                } message;
  63.  
  64. tcb tcb1, tcb2, tcb3, tcb4;
  65. mailbox box;
  66. message msg;
  67. flag halt;
  68. pipe pip;
  69. buffer buf;
  70.  
  71. byte pipbuf [10];
  72. word bufbuf [40];
  73. int scrnl;
  74. int endrun, xendrun;
  75. char cmdname [64];
  76. char *cmd;
  77.  
  78. #if (TSK_TURBO)
  79.  
  80. int handler (int errval, int ax, int bp, int si)
  81. {
  82.    return 0;
  83. }
  84.  
  85. int cbreak (void)
  86. {
  87.    return 1;
  88. }
  89.  
  90. #endif
  91.  
  92. /*
  93.    Task1 will display a screen snap on the selected monitor every second.
  94. */
  95.  
  96. void far task1 (void)
  97. {
  98.    tprintf ("Task 1 started\n");
  99.    while (!endrun)
  100.       {
  101.         t_delay (18L);
  102.         wait_flag_clear (&halt, 0L);
  103.       if (scrnl)
  104.          screensnap (25);
  105.       }
  106. }
  107.  
  108. /*
  109.    Task2 reads the keyboard. If a character has been read, it is passed
  110.    to Task4 via a pipe. Entering 'h' will set the halt flag (stopping Task1),
  111.    entering 'c' will clear the halt flag.
  112.    'e' stops the program.
  113. */
  114.  
  115. void far task2 (void)
  116. {
  117.    int ch;
  118.  
  119.    tprintf ("Task 2 started\n");
  120.    while (!endrun)
  121.       {
  122.       ch = t_wait_key (36L);
  123.       if (ch == -1)
  124.          outser (0x3f8, '2');
  125.       else
  126.          {
  127.          ch &= 0xff;
  128.          switch (tolower (ch))
  129.             {
  130.             case 'h':   set_flag (&halt);
  131.                         break;
  132.             case 'c':   clear_flag (&halt);
  133.                         break;
  134.             case 'e':   wake_task (NULL);
  135.                         break;
  136.    #if (TSK_NAMED)
  137.             case 'd':   snapshot (stdout);
  138.                         break;
  139.    #endif
  140.    #if (TSK_TURBO)
  141.             case 'b':   sound (2000);
  142.                         t_delay (2);
  143.                         nosound ();
  144.                         break;
  145.    #endif
  146.             case 's':   spawnlp (P_WAIT, cmdname, cmdname, NULL);
  147.                         break;
  148.             }
  149.  
  150.          if (!endrun)
  151.             write_pipe (&pip, (char)ch, 0L);
  152.          outser (0x3f8, 'r');
  153.          }
  154.       }
  155. }
  156.  
  157.  
  158. /*
  159.    Task3 does something in the backgound.
  160. */
  161.  
  162. void far task3 (void)
  163. {
  164.    int fil;
  165.  
  166.    tprintf ("Task 3 started\n");
  167.    while (!endrun)
  168.       {
  169.       t_delay (18L);
  170.       if ((fil = open ("c:/state.rst", O_RDONLY)) == -1) 
  171.          {
  172. #if (TSK_MSC)
  173.          tprintf ("\nError opening file\n");
  174. #else
  175.          outser (0x3f8, '!');
  176.          sound (200);
  177.          t_delay (2L);
  178.          nosound ();
  179. #endif
  180.          }
  181.       else
  182.             outser (0x3f8, '+');
  183.       close (fil);
  184.       }
  185. }
  186.  
  187.  
  188. /*
  189.    Task4 waits for a character in the pipe and displays it.
  190. */
  191.  
  192. void far task4 (void)
  193. {
  194.    int ch;
  195.  
  196.    tprintf ("Task 4 started\n");
  197.    while (!endrun)
  198.       {
  199.       ch = read_pipe (&pip, 40L);
  200.       if (ch < 0)
  201.          outser (0x3f8, '4');
  202.       else
  203.          tprintf ("Task 4 got <%c>\n", ch);
  204.       }
  205. }
  206.  
  207.  
  208. int main (int argc, char *argv [])
  209. {
  210.    char stack1 [STACKSIZE];
  211.    char stack2 [STACKSIZE];
  212.    char stack3 [STACKSIZE];
  213.    char stack4 [STACKSIZE];
  214.    char *m;
  215.    int c;
  216.  
  217. #if(TSK_TURBO)
  218.    harderr (handler);
  219.    ctrlbrk (cbreak);
  220. #endif
  221.  
  222.    c = (argc < 2) ? 'x' : tolower (argv [1][0]);
  223.    cmd = getenv ("COMSPEC");
  224.    strcpy (cmdname, (cmd == NULL) ? "command.com" : cmd);
  225.  
  226.    endrun = xendrun = 0;
  227.    install_tasker (0, 0, IFL_INT8_DIR | IFL_DISK, "Spawn");
  228.    init_conout ();
  229.    create_task (&tcb1, task1, stack1, STACKSIZE, PRI_STD, NULL TN("TASK1"));
  230.    create_task (&tcb2, task2, stack2, STACKSIZE, PRI_STD, NULL TN("TASK2"));
  231.    create_task (&tcb3, task3, stack3, STACKSIZE, PRI_STD, NULL TN("TASK3"));
  232.    create_task (&tcb4, task4, stack4, STACKSIZE, PRI_STD, NULL TN("TASK4"));
  233.    create_mailbox (&box TN("Mailbox"));
  234.    create_flag (&halt TN("Halt"));
  235.    create_pipe (&pip, pipbuf, sizeof (pipbuf) TN("Pipe"));
  236.    create_buffer (&buf, bufbuf, sizeof (bufbuf) TN("Buffer"));
  237.  
  238.    m = malloc (10000);
  239.    malloc (1);
  240.    free (m);
  241.  
  242.    switch (c)
  243.       {
  244.       case 'c':   tsk_set_colour (25, 80);
  245.                   scrnl = 1;
  246.                   break;
  247.       case 'm':   tsk_set_mono (25, 80);
  248.                   scrnl = 1;
  249.                   break;
  250.       default:    scrnl = 0;
  251.                   break;
  252.       }
  253.  
  254.    start_task (&tcb1);
  255.    start_task (&tcb2);
  256.    start_task (&tcb3);
  257.    start_task (&tcb4);
  258.    preempt_on ();
  259.  
  260.    t_delay (0L);
  261.  
  262.    endrun = 1;
  263.    tputs ("******** Main Task *********");
  264.  
  265.    set_priority (NULL, PRI_STD);
  266.    delete_mailbox (&box);
  267.    delete_pipe (&pip);
  268.    delete_buffer (&buf);
  269.    delete_flag (&halt);
  270.  
  271.    schedule ();
  272.  
  273.    xendrun = 1;
  274.  
  275.    preempt_off ();
  276.    end_conout ();
  277.    remove_tasker ();
  278.    puts ("******** End Run *********");
  279.    return 0;
  280. }
  281.  
  282.