Chapter Contents

Previous

Next
Coprocessing Functions

Passing Data between Coprocesses

The cocall , coreturn , and coexit functions all provide an argument that can be used to pass data from one coprocess to another. Because the type of data that coprocesses may want to exchange cannot be predicted in advance, values are communicated by address. The arguments and returned values from these functions are all defined as the generic pointer type void * .

Except when a coprocess is beginning or ending execution, a call to either cocall or coreturn by one coprocess causes the calling coprocess to be suspended and a call to the other function in another coprocess to be resumed. In either case, the argument to the first function becomes the return value from the function that is resumed.

To illustrate, here is a version of the previous example that demonstrates how data can be passed from coprocess to coprocess:

   Coprocess A                            Coprocess B
A_init()                           char * B_init(arg)
{                                  char *arg;
   char *ret;                      {
   B = costart(&B_init,NULL);         puts(arg);
   ret = cocall(B, "B0");             arg = coreturn("A1");
   puts(ret);                         puts(arg);
   ret = cocall(B, "B1");             puts(bsub());
   puts(ret);                         return "A3"; /* or coexit("A3"); */
   ret = cocall(B, "B2");          }
   puts(ret);
}                                  char *bsub()
                                   {
                                      char *arg;
                                      arg = coreturn("A2");
                                      return arg;
                                   }

When the function A_init is called, the following sequence of lines is written to stdout :

   B0
   A1
   B1
   A2
   B2
   A3

Each line is written by the coprocess indicated by its first letter.


Special Cases

In the case where a cocall causes execution of a coprocess to begin or where a return from the initial function of a coprocess causes it to terminate, the simple rule that the argument to one function becomes the return value from another does not apply. In the first case, the argument to cocall becomes the argument to the initial function of the newly started coprocess. In the second case, the return value from the initial function becomes the value returned by the cocall in the resumed coprocess. Both of these situations are illustrated in the preceding example.


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.