home *** CD-ROM | disk | FTP | other *** search
- /*
- * DDEMO.C Version 2.21
- *
- * DOSdemo module. Contains code to demonstrate use of smx with MS-DOS,
- * or equivalents.
- *
- * Copyright (c) 1990-1992 Micro Digital Associates, Inc.
- * All Rights Reserved.
- *
- * Author: Ralph Moore
- *
- ***********************************************************************/
-
- #include "core.h"
- #include <stdio.h>
- #include <stdlib.h>
-
-
- /* function prototypes */
-
- #if __cplusplus
- extern "C" {
- #endif
-
- void disk_demo_main(void);
- void errgen_main(void);
- void opcon_main(void);
- void preempter_main(void);
- void sleeper_main(void);
- void swap_screen(void);
-
- #if __cplusplus
- }
- #endif
-
- /* variables */
-
- TCB_PTR disk_demo;
- long disk_reads; /* disk read counter */
- TCB_PTR errgen;
- byte fbuf[10000]; /* file buffer */
- XCB_PTR free_msgs;
- TCB_PTR opcon; /* operation control task (for kbd) */
- TCB_PTR preempter;
- long preempts; /* preempt task run counter */
- TCB_PTR sleeper;
- FILE *test_file;
-
- /* externals */
-
- extern PXCB_PTR kbd_pipe; /* pipe used by keyin() */
- extern PXCB_PTR op_pipe; /* operation input pipe */
-
-
- /***** APPLICATION INITIALIZATION
- * This is a typcial application initialization function. Create
- * something equivalent.
- **********************************************************************/
-
- void _cdecl appl_init()
- {
- #ifdef CODEVIEW
- opcon = create_task(opcon_main, 0, 0);
- #else
- opcon = create_task(opcon_main, MAX, 0);
- #endif
-
- start(opcon);
-
- disk_demo = create_task(disk_demo_main, MIN, 0);
- start(disk_demo);
- sleeper = create_task(sleeper_main, NORM, 0);
- start(sleeper);
- preempter = create_task(preempter_main, HI, 0);
- start(preempter);
- errgen = create_task(errgen_main, HI, 0);
-
- BUILD_HT(opcon, "opcon");
- BUILD_HT(disk_demo, "disk_demo");
- BUILD_HT(sleeper, "sleeper");
- BUILD_HT(preempter, "preempter");
- BUILD_HT(errgen, "errgen");
-
- wr_string(0,21,YELLOW,BLACK,!BLINK,"Hit Esc to quit.");
- wr_string(0,7,LIGHTMAGENTA,BLACK,!BLINK,"DISK_DEMO TASK reads = ");
- wr_string(0,9,LIGHTBLUE,BLACK,!BLINK,"PREEMPTER TASK runs = ");
- wr_string(0,11,LIGHTGREEN,BLACK,!BLINK,"TIMEOUT TASK ticks = ");
-
- wr_string(0,15,YELLOW,BLACK,!BLINK,"% IDLE = ");
- wr_string(0,16,YELLOW,BLACK,!BLINK,"% BUSY = ");
- wr_string(0,17,YELLOW,BLACK,!BLINK,"% OVH = ");
-
- #ifndef DEBUG
- if((test_file = fopen("..\\test.dat","r+")) == NULL)
- {
- wr_string(0,21,LIGHTRED,BLACK,!BLINK,"Cannot find test.dat");
- start(exitx);
- }
- #endif
- }
-
-
- /***** OPERATION CONTROL
- * This task can be enlarged to interpret more keys and to interpret
- * user commands.
- **********************************************************************/
-
- void _cdecl opcon_main(void)
- {
- char key;
-
- while(key = (char)pget_char(op_pipe, INF))
- {
- if(key == Esc)
- start(exitx);
- else if(key == Ctrl_S)
- {
- if(sp_scrn_sel)
- swap_screen();
- kbd_pipe = op_pipe;
- }
- }
- }
-
-
- /***** DISK DEMO
- * This task performs continual disk reads. The amount read can be
- * changed by changing the third fread parameter. DO NOT EXCEED THE SIZE
- * OF fbuf.
- **********************************************************************/
-
- void _cdecl disk_demo_main(void)
- {
- static char buffer[10]; /* output buffer for this task */
-
- unlock(); /* permit this task to be preempted */
-
- while(TRUE)
- {
- #ifndef DEBUG /* file reads */
- rewind(test_file);
- fread(fbuf, 1, 1100, test_file);
- #endif
-
- test(in_clib, INF);
- sprintf(buffer, "%.6ld", ++disk_reads);
- signal(in_clib);
-
- wr_string(24,7,LIGHTMAGENTA,BLACK,!BLINK,buffer);
-
- #ifdef DEBUG
- if(pcticks)
- {
- pcticks--; /* decrement before task switch if timeslicing */
- #ifdef __ZTC__
- tick_int();
- #else
- (*tick)();
- #endif
- }
- #endif
- }
- }
-
-
- /***** ERROR GENERATOR
- * The purpose of this task is to simulate errors to test the error
- * system.
- **********************************************************************/
-
- void _cdecl errgen_main(void)
- {
- while(1)
- {
- start(0);
- send(0,0);
- receive(0,0);
- create_task(0,0,0);
- pput_char(0,0,0);
- count(10,ticks,INF);
- }
- /* Must make sure returns from catastrophic errors are ok before
- putting the following improvement in
-
- word errno;
- errno = 0;
- while(count(5,ticks,INF))
- {
- errno++;
- if(errno == NUM_XERRORS) errno = 1;
- (*xesrt[errno])(errno);
- }
- */
- }
-
-
- /***** PREEMPTER
- * The purpose of this task is to test that disk i/o operations can
- * be preempted by tasks of various lengths. To alter the length of
- * operation, change the dctr initialization value.
- **********************************************************************/
-
- void _cdecl preempter_main()
- {
- static char buffer[10]; /* output buffer for this task */
- long dctr;
-
- unlock();
- preempts = 0;
- while(count(2, ticks, INF))
- {
- for(dctr = 100L; dctr; dctr--); /* delay */
- test(in_clib, INF);
- sprintf(buffer, "%.6ld", ++preempts);
- signal(in_clib);
- wr_string(24,9,LIGHTBLUE,BLACK,!BLINK,buffer);
- }
- }
-
-
- /***** SLEEPER
- * This task demonstrates how to make a task periodically wake up.
- **********************************************************************/
-
- void _cdecl sleeper_main()
- {
- unlock();
- while(sleepx(get_stime() + 5))
- wr_string(24,11,LIGHTCYAN,BLACK,!BLINK,"WAKEUP");
- }
-
-
- /***** SUBROUTINES
- *
- **********************************************************************/
-
- void _cdecl quit_appl()
- {
- if(test_file) fclose(test_file);
- }
-
-
- void _cdecl show_etime()
- {
- static char buffer[10]; /* output buffer for this task */
- int i, j;
-
- ltoa(get_etime(), buffer, 10);
-
- for(i = 0; buffer[i] && i < 6; i++); /* find end of string */
- for(; i < 6; i++) /* right align */
- {
- for(j = i+1; j > 0; j--)
- buffer[j] = buffer[j-1];
- buffer[j] = '0';
- }
- wr_string(24,11,LIGHTGREEN,BLACK,!BLINK,buffer);
- }