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

  1. /**********************************************************
  2. ***
  3. ***                        LISTING 2
  4. ***
  5. ***                        holder.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. ***        holder.c is meant to be run in the background and
  12. ***        provides the following services to the sender.c 
  13. ***        (listing 3) program:
  14. ***
  15. ***        1)  Store a text string.
  16. ***        2)  Reply with a previously stored string.
  17. ***        3)  Commit suicide.
  18. ***
  19. ***        The action carried out depends on the value of the
  20. ***        header word of the message that sender.c sends to
  21. ***        holder.c.
  22. ***
  23. **********************************************************/
  24.  
  25. #include <stdio.h>
  26. #include <task_msgs.h>
  27. #include "message.h"
  28.  
  29.  
  30. #define INIT_MSG        "*** No stored messages ***"
  31.  
  32. main(argc, argv)
  33. int argc;
  34. char **argv;
  35. {
  36.     unsigned stid;                /* Variable to hold 
  37.                                    sender's task id.    */
  38.     unsigned rtid;                /* Variable to hold 
  39.                                    this task's task id. */
  40.     struct message buff, rbuff;    /* Message buffers      */
  41.     extern char Cmd_flags;        /* Magic variable        */
  42. /*
  43.     If any arguments have been passed to this program or if
  44.     the program has not been run in background mode, print
  45.     a command usage message and exit.
  46. */
  47.     if (argc != 1 || (Cmd_flags & RUN_BACKGROUND) == 0 )
  48.     {
  49.         printf("\nUsage:   holder &\n");
  50.         exit(-1);
  51.     }
  52. /*
  53.     Register a name with the operating system so that other
  54.     tasks can find out its task id.
  55. */
  56.     if((rtid = name_attach(HOLDER_NAME, 0)) == 0)
  57.     {
  58.         printf("\nholder task couldn't attach name\n");
  59.         exit(-1);
  60.     }
  61. /*
  62.     Initialize the reply buffer with an initial text 
  63.     string.
  64. */
  65.     strcpy(rbuff.text, INIT_MSG);
  66.  
  67. /*
  68.     Now wait for a message.  Upon receiving a message, 
  69.     carry out the appropriate action based on the header
  70.     word of the message.  Then reply with the result to
  71.     the sending task.
  72. */
  73.     for (;;)
  74.     {
  75.         stid = receive(0, &buff, sizeof(buff) );
  76.         switch (buff.mssg_type)
  77.         {
  78.             case STORE:
  79.                 strcpy(rbuff.text, buff.text);
  80.                 rbuff.mssg_type = STORED;
  81.                 break;
  82.             case RETRIEVE:
  83.                 rbuff.mssg_type = RETRIEVED;
  84.                 break;
  85.             case KILL:    /* de-register name & die. */
  86.                 name_detach(HOLDER_NAME, rtid);
  87.                 exit(0);
  88.                 break;
  89.             default:
  90.                 rbuff.mssg_type = ERROR;
  91.                 break;
  92.         }
  93.         reply(stid, &rbuff, sizeof(rbuff) );
  94.     }
  95.  
  96. }
  97.  
  98.