home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_09 / 9n09114a < prev    next >
Text File  |  1991-07-29  |  3KB  |  104 lines

  1. /**********************************************************
  2. ***
  3. ***                        LISTING 3
  4. ***
  5. ***                        sender.c
  6. ***
  7. ***        Program to illustrate message passing under QNX.
  8. ***        Written and tested under QNX version 2.15C atp
  9. ***        Compiler used:  Quantum's C compiler
  10. ***
  11. ***        This program is used to send a message to the
  12. ***        holder.c program (listing 2).  It can request that
  13. ***        holder.c provide one of the following services:
  14. ***
  15. ***        1)  Store a text string that is sent to it by this
  16. ***            task.
  17. ***        2)  Reply to this task with a previously stored 
  18. ***            string.
  19. ***        3)  Have holder.c commit suicide.
  20. ***
  21. **********************************************************/
  22.  
  23. #include <stdio.h>
  24. #include "message.h"
  25.  
  26. main(argc, argv)
  27. int argc;
  28. char **argv;
  29. {
  30.     unsigned rtid;                /* Variable to hold 
  31.                                    receiving task id    */
  32.     struct message buff;        /* Message buffers      */
  33.  
  34. /*
  35.     If an incorrect number of arguments have been passed to
  36.     this program print a command usage message and exit.
  37. */
  38.     if (argc != 2)
  39.     {
  40.         printf("\nUsage:   sender <arg>");
  41.         printf("\n\nWhere: <arg> = t=\"text string\"");
  42.         printf("\n               (stores string)");
  43.         printf("\n       <arg> = -query");
  44.         printf("\n               (get stored string)");
  45.         printf("\n       <arg> = -kill");
  46.         printf("\n               (cause holder to die)\n");
  47.         exit(-1);
  48.     }
  49.  
  50. /*
  51.     Find the task id of holder.c which will receive the 
  52.     messages from this task.
  53. */
  54.     if(!(rtid = name_locate(HOLDER_NAME, 0, sizeof(buff))))
  55.     {
  56.         printf("\nname_locate() failed\n");
  57.         exit(-1);
  58.     }
  59. /*
  60.     Build message to send to holder.
  61. */
  62.     switch ( (*argv[1] << 8) | *(argv[1] +1) )
  63.     {
  64.         case ('t' << 8) | '=' :
  65.             strcpy(buff.text, (argv[1] + 2) );
  66.             buff.mssg_type = STORE;
  67.             break;
  68.         case ('-' << 8) | 'q' :
  69.             buff.mssg_type = RETRIEVE;
  70.             break;
  71.         case ('-' << 8) | 'k' :
  72.             buff.mssg_type = KILL;
  73.             break;
  74.         default:
  75.             printf("\nUnidentified argument used.\n");
  76.             exit(-1);
  77.             
  78.     }
  79.  
  80. /*        
  81.     Send holder the message and print the reply if there
  82.     there is one.
  83. */
  84.     send(rtid, &buff, &buff, sizeof(buff) );
  85.  
  86.     switch (buff.mssg_type)
  87.     {
  88.         case STORED:
  89.             printf("\nMessage stored\n");
  90.             break;
  91.         case RETRIEVED:
  92.             printf("\nRetrieved message [%s]\n",buff.text);
  93.             break;
  94.         case KILL:
  95.             printf("\nSuicide request sent to holder\n");
  96.             break;
  97.         default:
  98.             printf("\nReceived unknown message [%d].\n", 
  99.                     buff.mssg_type);
  100.             break;
  101.     }
  102. }
  103.         
  104.