home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 September
/
Simtel20_Sept92.cdr
/
msdos
/
ddjmag
/
ddj8712.arc
/
HOLUB.ARC
/
HOLUB.EXP
< prev
next >
Wrap
Text File
|
1980-01-01
|
6KB
|
240 lines
idle()
{
int t, i ;
do {
for( i = 1000; --i>=0 ; )
;
t_cli(); /* Clear interrupts */
t = T_numtasks; /* t = # of tasks */
t_sti(); /* restore ints. */
}
while( t > 1 ) ;
t_delete( NULL ); /* delete self */
}
Example 1: A typical idle task
foo( a, b )
char *a, *b;
{
t_printf("%s %s\n", a, b );
t_delete( NULL );
}
main()
{
t_create( foo,"foo", 10, 512, "hello", "world", NULL);
t_start( 2 );
}
Example 2: An example of task creation
#include <stdio.h>
#include <tools/video.h>
#include "kernel.h"
#define TEST 3 /* 1 = nonpreemptive test,
* 2 = preemptive test: simple timer
* 3 = test round-robin scheduling
*/
T_QUEUE *Queue1;
T_QUEUE *Queue2;
main()
{
int status = 0;
long t_numint(), t_numblk();
int sam(), dave(), timer(), idle(), maintask;
if( !(Queue1 = (T_QUEUE *) t_makequeue( 2 ) ) )
printf("Can't make Queue1 queue\n"), exit(1);
if( !(Queue2 = (T_QUEUE *) t_makequeue( 2 ) ) )
printf("Can't make Queue2 queue\n"), exit(1);
#if (TEST == 1)
status = (int) t_create(sam, "sam", 100, 512, "SAM", NULL);
status = (int) t_create(dave, "dave", 50, 512, "DAVE", NULL);
status = t_start( 0 );
#endif
#if (TEST == 2)
status = (int) t_create(timer,"timer",10, 512,"timer",NULL);
status = (int) t_create(idle, "idle", 1, 100, NULL);
status = t_start( 2 );
#endif
#if (TEST == 3)
status = (int) t_create(maintask,"maintask",200, 512, NULL);
status = t_start( 2 );
#endif
t_perror( "\ndone: ", status );
t_sstats();
printf("%ld interrupts, %ld blocked\n", t_numint(), t_numblk();
}
Example 3: A program that creates queues, starts multitasking, and performs thrs
dave( arg )
char *arg;
{
char *s;
t_printf( "In dave(%s), about to wait for message\n", arg );
if( t_iserr(s = t_wait(Queue1, 100)) )
t_perror("dave: first wait call", (int) s );
else
t_printf( "dave: got <%s> from Queue1\n", s );
if( t_iserr(s = t_wait(Queue1, 100)) )
t_perror("dave: first wait call", (int) s );
else
t_printf( "dave: got %s from Queue1\n", s );
t_printf("dave: yielding\n");
t_yield();
t_printf("dave: deleting self\n");
t_delete( NULL );
}
Example 4: The task sam()
sam( arg )
char *arg;
{
int err;
t_printf( "In sam(%s), yielding: no messages\n", arg);
t_yield();
t_printf( "sam: back from yield, sending messages\n");
if( err = t_send( Queue1, "1st message") )
t_perror( "Foo: sending 1st message", err );
if( err = t_send( Queue1, "2nd message") )
t_perror( "Foo: sending 2nd message", err );
if( err = t_send( Queue1, "3rd message") )
t_perror( "Foo: sending 3rd message", err );
t_printf("Foo: yielding again\n");
t_yield();
t_printf("Foo: Returned from 2nd yield, deleting self\n");
t_delete( NULL );
}
Example 5: The task dave()
In sam(SAM), yielding: no messages
In dave(DAVE), about to wait for message
sam: back from yield, sending messages
dave: got <1st message> from Queue1
dave: got 2nd message from Queue1
dave: yielding
Foo: yielding again
dave: deleting self
Foo: Returned from 2nd yield, deleting self
done: No error
Scheduler called 0 times: 0 tasks timed-out, 0 context swaps
0 interrupts, 0 blocked
Example 6: Output from test 1 in Example 3
timer()
{
int i;
t_printf("Starting up timer\n");
for( i = 5; --i >= 0 ; )
{
t_printf( "!" );
t_wait( Queue2, t_second() );
}
t_printf("Deleting timer\n");
t_delete( NULL );
}
#endif
Example 7: The timer() task
timer( arg )
{
/* I'm assuming that pointers & ints are
* the same size here.
*/
int i;
while( 1 )
{
for( i = 10000; --i >= 0 ; )
;
t_cli();
dv_putchar( arg + '0' );
t_sti();
}
}
maintask()
{
TCB *t1, *t2;
t1 = t_create( timer, "1st timer", 100, 512, (void*)1, NULL);
t2 = t_create( timer, "2nd timer", 100, 512, (void*)2, NULL);
t_perror( "task1:", (int)t1 );
t_perror( "task2:", (int)t2 );
t_wait( Queue2, 5 * t_second() );
t_delete( t1 );
t_delete( t2 );
t_delete( NULL );
}
Example 8: Test 3, used in Example 3
double mean(reset, data)
int reset;
double data;
{
static double xhat, ki;
return reset ? (ki = xhat = 0)
: (xhat += (data \sc0\ xhat) / ++ki)
;
}
Example 9: A C function that implements the algorithm shown in Figure 1