home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frostbyte's 1980s DOS Shareware Collection
/
floppyshareware.zip
/
floppyshareware
/
DOOG
/
CTASK.ZIP
/
SPAWN.C
< prev
next >
Wrap
C/C++ Source or Header
|
1989-12-13
|
7KB
|
315 lines
/*
Test program for checking CTask DOS spawn compatibility.
NOTE: Task 1 does a "screensnap" every second. This is only useful
in a dual-monitor system.
The monitor to use is determined by the program parameters:
SPAWNxx - Don't do screensnap.
SPAWNxx M - Use mono monitor
SPAWNxx C - Use colour monitor
NOTE: Task 3 repeatedly opens and closes the file 'C:\STATE.RST'
to test background DOS operations. If this file does not
exist, or can't be opened, the program will beep (Turbo C)
or output an error message (MS C). You should have a file
with that name in the C: root directory, or change Task 3.
Keyboard input while SPAWN is in control:
e Terminates program
d Snapshot dump to STDOUT
h Stop screensnap output
c Resume screensnap output
s Spawn COMMAND.COM. Enter EXIT to return to SPAWN.
b Beep (Turbo C only)
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include <process.h>
#include <stdlib.h>
#include <fcntl.h>
#include <io.h>
#include "tsk.h"
#include "tsksup.h"
#if (MSC)
#define sound(x)
#define nosound()
#endif
#define SEROUT 0 /* If non-0, tasks will signal activity on
COM1-Port. */
#if (SEROUT)
#define outser(p,x) tsk_outp(p,x)
#else
#define outser(p,x)
#endif
#define STACKSIZE 2048
unsigned int _stklen = 8 * STACKSIZE; /* For Turbo C: Four tasks + main Task Stack */
typedef struct {
farptr xx;
char str [20];
} message;
tcb tcb1, tcb2, tcb3, tcb4;
mailbox box;
message msg;
flag halt;
pipe pip;
buffer buf;
byte pipbuf [10];
word bufbuf [40];
char far *scrnp;
int scrnl;
int endrun, xendrun;
char cmdname [64];
char *cmd;
#if (TURBO)
int handler (int errval, int ax, int bp, int si)
{
return 0;
}
int cbreak (void)
{
return 1;
}
#endif
/*
Task1 will display a screen snap on the color monitor every second.
*/
void far task1 (void)
{
tprintf ("Task 1 started\n");
while (!endrun)
{
t_delay (18L);
wait_flag_clear (&halt, 0L);
if (scrnl)
screensnap (scrnp, scrnl);
}
}
/*
Task2 reads the keyboard. If a character has been read, it is passed
to Task4 via a pipe. Entering 'h' will set the halt flag (stopping Task1),
entering 'c' will clear the halt flag.
'e' stops the program.
*/
void far task2 (void)
{
int ch;
tprintf ("Task 2 started\n");
while (!endrun)
{
ch = t_wait_key (36L);
if (ch == -1)
outser (0x3f8, '2');
else
{
ch &= 0xff;
switch (tolower (ch))
{
case 'h': set_flag (&halt);
break;
case 'c': clear_flag (&halt);
break;
case 'e': wake_task (NULL);
break;
#if (TSK_NAMED)
case 'd': snapshot (stdout);
break;
#endif
#if (TURBO)
case 'b': sound (2000);
t_delay (2);
nosound ();
break;
#endif
case 's': spawnlp (P_WAIT, cmdname, cmdname, NULL);
break;
}
if (!endrun)
write_pipe (&pip, (char)ch, 0L);
outser (0x3f8, 'r');
}
}
}
/*
Task3 does something in the backgound.
*/
void far task3 (void)
{
int fil;
tprintf ("Task 3 started\n");
while (!endrun)
{
t_delay (18L);
if ((fil = open ("c:/state.rst", O_RDONLY)) == -1)
{
#if (MSC)
tprintf ("\nError opening file\n");
#else
outser (0x3f8, '!');
sound (200);
t_delay (2L);
nosound ();
#endif
}
else
outser (0x3f8, '+');
close (fil);
}
}
/*
Task4 waits for a character in the pipe and displays it.
*/
void far task4 (void)
{
int ch;
tprintf ("Task 4 started\n");
while (!endrun)
{
ch = read_pipe (&pip, 40L);
if (ch < 0)
outser (0x3f8, '4');
else
tprintf ("Task 4 got <%c>\n", ch);
}
}
int main (int argc, char *argv [])
{
char stack1 [STACKSIZE];
char stack2 [STACKSIZE];
char stack3 [STACKSIZE];
char stack4 [STACKSIZE];
char *m;
int c;
#if(TURBO)
harderr (handler);
ctrlbrk (cbreak);
#endif
c = (argc < 2) ? 'x' : tolower (argv [1][0]);
switch (c)
{
case 'c': scrnp = (char far *)0xb8000000L;
scrnl = 60;
break;
case 'm': scrnp = (char far *)0xb0000000L;
scrnl = 25;
break;
default: scrnp = NULL;
scrnl = 0;
break;
}
cmd = getenv ("COMSPEC");
strcpy (cmdname, (cmd == NULL) ? "command.com" : cmd);
endrun = xendrun = 0;
install_tasker (0, 0, IFL_STD, "Spawn");
init_conout ();
create_task (&tcb1, task1, stack1, STACKSIZE, PRI_STD, NULL
#if (TSK_NAMEPAR)
,"TASK1"
#endif
);
create_task (&tcb2, task2, stack2, STACKSIZE, PRI_STD, NULL
#if (TSK_NAMEPAR)
,"TASK2"
#endif
);
create_task (&tcb3, task3, stack3, STACKSIZE, PRI_STD, NULL
#if (TSK_NAMEPAR)
,"TASK3"
#endif
);
create_task (&tcb4, task4, stack4, STACKSIZE, PRI_STD, NULL
#if (TSK_NAMEPAR)
,"TASK4"
#endif
);
create_mailbox (&box
#if (TSK_NAMEPAR)
,"Mailbox"
#endif
);
create_flag (&halt
#if (TSK_NAMEPAR)
,"Halt"
#endif
);
create_pipe (&pip, pipbuf, sizeof (pipbuf)
#if (TSK_NAMEPAR)
,"Pipe"
#endif
);
create_buffer (&buf, bufbuf, sizeof (bufbuf)
#if (TSK_NAMEPAR)
,"Buffer"
#endif
);
m = malloc (10000);
malloc (1);
free (m);
start_task (&tcb1);
start_task (&tcb2);
start_task (&tcb3);
start_task (&tcb4);
preempt_on ();
t_delay (0L);
endrun = 1;
tputs ("******** Main Task *********");
set_priority (NULL, PRI_STD);
delete_mailbox (&box);
delete_pipe (&pip);
delete_buffer (&buf);
delete_flag (&halt);
schedule ();
xendrun = 1;
preempt_off ();
end_conout ();
remove_tasker ();
puts ("******** End Run *********");
return 0;
}