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.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-21  |  5.7 KB  |  232 lines

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