home *** CD-ROM | disk | FTP | other *** search
/ C/C++ User's Journal & Wi…eveloper's Journal Tools / C-C__Users_Journal_and_Windows_Developers_Journal_Tools_1997.iso / smxdemo / smxpp / ddemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-24  |  6.4 KB  |  259 lines

  1. /*
  2. * DDEMO.C                                                   Version 2.21
  3. *
  4. * DOSdemo module. Contains code to demonstrate use of smx with MS-DOS,
  5. *                 or equivalents.     
  6. *
  7. * Copyright (c) 1990-1992 Micro Digital Associates, Inc. 
  8. *               All Rights Reserved.
  9. *
  10. * Author: Ralph Moore
  11. *
  12. ***********************************************************************/
  13.  
  14. #include    "core.h"
  15. #include    <stdio.h>
  16. #include    <stdlib.h>
  17.  
  18.  
  19. /* function prototypes */
  20.  
  21. #if __cplusplus
  22.  extern "C" {
  23. #endif
  24.  
  25. void        disk_demo_main(void);
  26. void        errgen_main(void);
  27. void        opcon_main(void);
  28. void        preempter_main(void);
  29. void        sleeper_main(void);
  30. void        swap_screen(void);
  31.  
  32. #if __cplusplus
  33. }
  34. #endif
  35.  
  36. /* variables */
  37.  
  38. TCB_PTR     disk_demo;
  39. long        disk_reads;    /* disk read counter */
  40. TCB_PTR     errgen;
  41. byte        fbuf[10000];   /* file buffer */
  42. XCB_PTR     free_msgs;
  43. TCB_PTR     opcon;         /* operation control task (for kbd) */
  44. TCB_PTR     preempter;
  45. long        preempts;      /* preempt task run counter */
  46. TCB_PTR     sleeper;
  47. FILE        *test_file;
  48.  
  49. /* externals */
  50.  
  51. extern PXCB_PTR        kbd_pipe;      /* pipe used by keyin() */
  52. extern PXCB_PTR        op_pipe;       /* operation input pipe */
  53.  
  54.  
  55. /***** APPLICATION INITIALIZATION
  56. * This is a typcial application initialization function. Create
  57. * something equivalent.
  58. **********************************************************************/
  59.  
  60. void _cdecl appl_init()
  61. {
  62.    #ifdef CODEVIEW
  63.     opcon = create_task(opcon_main, 0, 0);
  64.    #else
  65.     opcon = create_task(opcon_main, MAX, 0);
  66.    #endif
  67.  
  68.    start(opcon);
  69.  
  70.    disk_demo = create_task(disk_demo_main, MIN, 0);
  71.    start(disk_demo);
  72.    sleeper = create_task(sleeper_main, NORM, 0);
  73.    start(sleeper);
  74.    preempter = create_task(preempter_main, HI, 0);
  75.    start(preempter);
  76.    errgen = create_task(errgen_main, HI, 0);
  77.  
  78.    BUILD_HT(opcon, "opcon");
  79.    BUILD_HT(disk_demo, "disk_demo");
  80.    BUILD_HT(sleeper, "sleeper");
  81.    BUILD_HT(preempter, "preempter");
  82.    BUILD_HT(errgen, "errgen");
  83.  
  84.    wr_string(0,21,YELLOW,BLACK,!BLINK,"Hit Esc to quit.");
  85.    wr_string(0,7,LIGHTMAGENTA,BLACK,!BLINK,"DISK_DEMO TASK  reads = ");
  86.    wr_string(0,9,LIGHTBLUE,BLACK,!BLINK,"PREEMPTER TASK   runs = ");
  87.    wr_string(0,11,LIGHTGREEN,BLACK,!BLINK,"TIMEOUT TASK    ticks = ");
  88.  
  89.    wr_string(0,15,YELLOW,BLACK,!BLINK,"% IDLE = ");
  90.    wr_string(0,16,YELLOW,BLACK,!BLINK,"% BUSY = ");
  91.    wr_string(0,17,YELLOW,BLACK,!BLINK,"% OVH  = ");
  92.  
  93.    #ifndef  DEBUG
  94.     if((test_file = fopen("..\\test.dat","r+")) == NULL)
  95.       {
  96.       wr_string(0,21,LIGHTRED,BLACK,!BLINK,"Cannot find test.dat");
  97.       start(exitx);
  98.       }
  99.    #endif
  100. }
  101.  
  102.  
  103. /***** OPERATION CONTROL
  104. * This task can be enlarged to interpret more keys and to interpret
  105. * user commands.
  106. **********************************************************************/
  107.  
  108. void _cdecl opcon_main(void)
  109. {
  110.    char  key;
  111.  
  112.    while(key = (char)pget_char(op_pipe, INF))
  113.       {
  114.       if(key == Esc)
  115.          start(exitx);
  116.       else if(key == Ctrl_S)
  117.          {
  118.          if(sp_scrn_sel)
  119.             swap_screen();
  120.          kbd_pipe = op_pipe;
  121.          }
  122.       }
  123. }
  124.  
  125.  
  126. /***** DISK DEMO
  127. * This task performs continual disk reads. The amount read can be
  128. * changed by changing the third fread parameter. DO NOT EXCEED THE SIZE
  129. * OF fbuf.
  130. **********************************************************************/
  131.  
  132. void _cdecl disk_demo_main(void)
  133. {
  134.    static char buffer[10];   /* output buffer for this task */ 
  135.  
  136.    unlock();            /* permit this task to be preempted */
  137.  
  138.    while(TRUE)
  139.       {
  140.      #ifndef  DEBUG    /* file reads */
  141.       rewind(test_file);
  142.       fread(fbuf, 1, 1100, test_file);
  143.      #endif
  144.  
  145.       test(in_clib, INF);
  146.       sprintf(buffer, "%.6ld", ++disk_reads);
  147.       signal(in_clib);
  148.  
  149.       wr_string(24,7,LIGHTMAGENTA,BLACK,!BLINK,buffer);
  150.  
  151.       #ifdef DEBUG
  152.        if(pcticks)
  153.           {
  154.           pcticks--;  /* decrement before task switch if timeslicing */
  155.           #ifdef __ZTC__
  156.            tick_int();
  157.           #else
  158.            (*tick)();
  159.           #endif
  160.           }
  161.       #endif
  162.       }
  163. }
  164.  
  165.  
  166. /***** ERROR GENERATOR
  167. * The purpose of this task is to simulate errors to test the error
  168. * system.
  169. **********************************************************************/
  170.  
  171. void _cdecl errgen_main(void)
  172. {
  173.    while(1)
  174.       {
  175.       start(0);
  176.       send(0,0);
  177.       receive(0,0);
  178.       create_task(0,0,0);
  179.       pput_char(0,0,0);
  180.       count(10,ticks,INF);
  181.       }
  182. /* Must make sure returns from catastrophic errors are ok before
  183.    putting the following improvement in 
  184.  
  185.    word  errno;
  186.    errno = 0;
  187.    while(count(5,ticks,INF))
  188.       {
  189.       errno++;
  190.       if(errno == NUM_XERRORS) errno = 1;
  191.       (*xesrt[errno])(errno);
  192.       }
  193. */
  194. }
  195.  
  196.  
  197. /***** PREEMPTER
  198. * The purpose of this task is to test that disk i/o operations can
  199. * be preempted by tasks of various lengths. To alter the length of
  200. * operation, change the dctr initialization value.
  201. **********************************************************************/
  202.  
  203. void _cdecl preempter_main()
  204. {
  205.    static char buffer[10];    /* output buffer for this task */ 
  206.    long  dctr;
  207.  
  208.    unlock();
  209.    preempts = 0;
  210.    while(count(2, ticks, INF))
  211.       {
  212.       for(dctr = 100L; dctr; dctr--);   /* delay */
  213.       test(in_clib, INF);
  214.       sprintf(buffer, "%.6ld", ++preempts);
  215.       signal(in_clib);
  216.       wr_string(24,9,LIGHTBLUE,BLACK,!BLINK,buffer);
  217.       }
  218. }
  219.  
  220.  
  221. /***** SLEEPER
  222. * This task demonstrates how to make a task periodically wake up.
  223. **********************************************************************/
  224.  
  225. void _cdecl sleeper_main()
  226. {
  227.    unlock();
  228.    while(sleepx(get_stime() + 5))
  229.       wr_string(24,11,LIGHTCYAN,BLACK,!BLINK,"WAKEUP");
  230. }
  231.  
  232.  
  233. /***** SUBROUTINES
  234. **********************************************************************/
  235.  
  236. void _cdecl quit_appl()
  237. {
  238.    if(test_file) fclose(test_file);
  239. }
  240.  
  241.  
  242. void _cdecl show_etime()
  243. {
  244.    static char buffer[10];    /* output buffer for this task */ 
  245.    int   i, j;
  246.  
  247.    ltoa(get_etime(), buffer, 10);
  248.  
  249.    for(i = 0; buffer[i] && i < 6; i++);   /* find end of string */
  250.    for(; i < 6; i++)                      /* right align */
  251.       {
  252.       for(j = i+1; j > 0; j--)
  253.          buffer[j] = buffer[j-1];
  254.       buffer[j] = '0';
  255.       }
  256.    wr_string(24,11,LIGHTGREEN,BLACK,!BLINK,buffer);
  257. }
  258.