home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 December
/
simtel1292_SIMTEL_1292_Walnut_Creek.iso
/
msdos
/
ddjmag
/
ddj8911.arc
/
KAR.LST
< prev
next >
Wrap
File List
|
1989-10-04
|
6KB
|
226 lines
_DATE-FLOW MULTITASKING_
by Rabindra Kar
[LISTING ONE]
/* quad_eq.c -
* Calculate roots of quadratic equation: ax + bx + c = 0
*/
#include "stdio.h"
#include "rmx.h"
#include "math.h"
#define NUM_VALS 100
unsigned status;
struct coeffs {double a, b, c;} y[NUM_VALS+1];
double det;
double num;
double res;
/* Calculate determinant */
determinant(i)
unsigned i;
{
det = y[i].b * y[i].b - 4 * y[i].a * y[i].c;
}
/* Calculate numerator */
numerator(i)
unsigned i;
{
num = pow(det,0.5) - y[i].b;
}
/* Calculate, print result */
result(i)
unsigned i;
{
res = num / (2 * y[i].a);
printf("Coeffs = %.1f,%.1f,%.1f Root = %7.2f\n",
y[i].a,y[i].b,y[i].c,res);
}
main()
{
unsigned i;
/* Create input data. Done for convenience. Could have read the data
* from any input device.
*/
for (i = 1; i < NUM_VALS; i++)
{y[i].a = (double)i; y[i].c = (double)i; y[i].b = 3*(double)i;}
/* Compute the roots */
for (i = 1; i < NUM_VALS; i++)
{
determinant(i);
numerator(i);
result(i);
}
printf("\n");
}
[LISTING TWO]
/****************************************************************************\
* dflow.c -
* Compute the roots of a quadratic equation using multitasking
* and data flow concepts.
* Operating System: iRMX II. Compiler: iC-286 V3.2
\****************************************************************************/
#include "stdio.h"
#include "rmx.h"
#include "math.h"
#define NUM_VALS 100
unsigned status, task_t;
FILE *fp;
/* "union" used to decompose a pointer into segment:offset */
typedef struct {unsigned offset; unsigned sel;} ptr_s;
union { unsigned *pointer; ptr_s ptr; } ptr_u;
/* Input data area */
struct coeffs {double a, b, c;} y[NUM_VALS+1];
/* "DET" mailbox -
* Output from main task; input to num_task
*/
double det = 0.0;
unsigned index1 = 0;
/* "NUM" mailbox -
* Output from num_task; input to res_task
*/
double num = 0.0;
unsigned index2 = 0;
/************************ Computational tasks *************************/
det_task()
{ /* Calculate determinant */
unsigned i;
for (i = 1; y[i].a != 0.0; i++)
{
while (det != 0.0) rq$sleep(0,&status);
index1++;
det = y[i].b * y[i].b - 4 * y[i].a * y[i].c;
}
}
num_task()
{ /* Calculate numerator */
while (y[index1].a != 0.0)
{
while ((det == 0.0) || (num != 0.0)) rq$sleep(0,&status);
index2++;
num = pow(det, 0.5) - y[index1].b;
det = 0.0;
}
rq$delete$task(0,&status); /* Delete self */
}
res_task()
{ /* Calculate result, print on console */
double res;
while (y[index2].a != 0.0)
{
while (num == 0.0) rq$sleep(0,&status);
res = num / (2 * y[index2].a);
num = 0.0;
printf("Coeffs = %.1f,%.1f,%.1f Root = %7.2f\n",
y[index2].a, y[index2].b, y[index2].c, res);
}
printf("\n");
rq$delete$task(0,&status); /* Delete self */
}
/****************************** Main Program ******************************/
main()
{
unsigned i;
unsigned short pri;
/* Create input data. Done for convenience. Could have read the data
* from any input device.
*/
for (i = 1; i < NUM_VALS; i++)
{y[i].a = (double)i; y[i].c = (double)i; y[i].b = 3*(double)i;}
y[NUM_VALS].a = 0.0; /* 0.0 indicates end of input data */
/* Place a pointer to any variable in union "ptr_u", so the data segment
of this program becomes known.
*/
ptr_u.pointer = &status;
/* Find the priority level that iRMX accords the main task */
pri = rq$get$priority (NULL, &status);
/*
* Spawn the operation tasks. All have the same priority as main.
*/
task_t = rq$create$task (pri, (long)det_task, ptr_u.ptr.sel,
0L, 1024, 1, &status);
if (status != 0) printf("det_task create error = %u\n",status);
task_t = rq$create$task (pri, (long)num_task, ptr_u.ptr.sel,
0L, 1024, 1, &status);
if (status != 0) printf("num_task create error = %u\n",status);
task_t = rq$create$task (pri, (long)res_task, ptr_u.ptr.sel,
0L, 1024, 1, &status);
if (status != 0) printf("res_task create error = %u\n",status);
/*
* Lower main task's priority so main() will be idled by OS while other
* tasks do computations. Will return here when input data is exhausted.
*/
rq$set$priority (NULL, (pri+1), &status);
/*********************************************************************/
rq$sleep(10, &status); /* Let other tasks complete */
printf("\n Done! \n");
}
[LISTING THREE]
/*
* This code is similar to "res_task()" in dflow.c, except
* that the computed result is output to a file instead of the (default)
* console screen.
* This code could be added to dflow.c along with two statements in
* main() to: 1. create a "res_task2()" task, and 2. open a file for
* output with fp as its file pointer. By so doing, the I/O delay involved
* in waiting for console output could be overlapped with file output delays,
* thus speeding up the program. Of course, the results would then be
* distributed between the console and the output file.
*/
res_task2()
{ /* Calculate result, print to file */
double res;
while (y[index2].a != 0.0)
{
while (num == 0.0) rq$sleep(0,&status);
res = num / (2 * y[index2].a);
num = 0.0;
fprintf(fp, "Coeffs = %.1f,%.1f,%.1f root = %7.2f\n",
y[index2].a, y[index2].b, y[index2].c, res);
}
rq$delete$task(0,&status); /* Delete self */
}