home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / adaptor.zip / adapt.zip / adaptor / dalib / pvm3 / mailbox.c < prev    next >
Text File  |  1993-11-29  |  4KB  |  105 lines

  1. /*******************************************************************
  2. *                                                                  *
  3. *  MODULE : mailbox.c                                              *
  4. *                                                                  *
  5. *  Realization of asynchronus message passing via                  *
  6. *  Mailboxes that can contain one message from every process       *
  7. *                                                                  *
  8. *******************************************************************/
  9.  
  10. #include <stdio.h>
  11. #include "system.h"
  12. #include "pvm3.h"
  13.  
  14. /*******************************************************************
  15. *                                                                  *
  16. *  Sending and receiving of messages                               *
  17. *                                                                  *
  18. *******************************************************************/
  19.  
  20. void asend (from, to, message, length)
  21. int from, to;
  22. unsigned char *message;
  23. int length;
  24. /* Process 'from' sends a message to process 'to' */
  25.  
  26. {  int i;
  27.  
  28.    if (from == to)
  29.    { if (own_mailbox.full == 1)
  30.        { printf ("send: there is already own mail\n");
  31.          exit (-1);
  32.        }
  33.      own_mailbox.msg_length = length;
  34.      own_mailbox.msg = (unsigned char *) malloc (length);
  35.      for (i=0; i<length; i++)
  36.        own_mailbox.msg[i] = message[i];
  37.      own_mailbox.full = 1;
  38.    }
  39.    else
  40.    { pvm_initsend (PvmDataRaw);
  41.      /* printf ("call of send : to = %d, from = %d , length = %d\n",
  42.                 to, from, length);        */
  43.      pvm_pkbyte (message, length, 1);
  44.      pvm_send (tids[to], from);
  45.    }
  46. }
  47.  
  48. void areceive (to, from, message, length)
  49. /* process 'to' gets a message from process 'from' */
  50. int to, from;
  51. unsigned char *message;
  52. int length;
  53. {  int i;
  54.    if (to == from)
  55.    { if (own_mailbox.full == 0)
  56.        { printf ("receive: no own mail\n");
  57.          exit (-1);
  58.        }
  59.      if (own_mailbox.msg_length != length)
  60.        { printf ("receive: own mail wrong length\n");
  61.          exit (-1);
  62.        }
  63.      for (i=0; i<length; i++)
  64.        message[i] = own_mailbox.msg[i];
  65.      free (own_mailbox.msg);
  66.      own_mailbox.full = 0;
  67.    }
  68.    else
  69.    { /* printf ("call of recv : to = %d, from = %d , length = %d\n",
  70.                 to, from, length);       */
  71.      pvm_recv (tids[from], from);
  72.      pvm_upkbyte (message, length, 1);
  73.      /* printf ("has recvd : to = %d, from = %d , length = %d\n",
  74.                 to, from, length);      */
  75.    }
  76. }
  77.  
  78. /*******************************************************************
  79. *                                                                  *
  80. *  DALIB : Fortran Interface                                       *
  81. *                                                                  *
  82. *******************************************************************/
  83.  
  84. dalib_receive__ (from, message, length)
  85. int *from;
  86. unsigned char * message;
  87. int *length;
  88. { int to;
  89.   to = dalib_pid_ ();
  90.   /* printf ("call of dalib_receive : to = %d, from = %d , length = %d\n",
  91.               to, *from, *length);     */
  92.   areceive (to, *from, message, *length);
  93. }
  94.  
  95. dalib_send__ (to, message, length)
  96. int *to;
  97. unsigned char * message;
  98. int *length;
  99. { int from;
  100.   from = dalib_pid_ ();
  101.   /* printf ("call of dalib_receive : from = %d, to = %d , length = %d\n",
  102.               *to, from, *length);     */
  103.   asend (from, *to, message, *length);
  104. }
  105.