Advertisement
heavenriver

messages.c

Nov 26th, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.32 KB | None | 0 0
  1. /* basic operations with messages, UNIX-native */
  2.  
  3.  /* pointer arithmetics:
  4.   *
  5.   * push(pointer, v) INDICATES *(pointer++) = v
  6.   * pop(pointer) INDICATES *(--pointer)
  7.   * (*++v)[0] INDICATES (**++v)
  8.   *           INDICATES first char in v
  9.   *           INDICATES name of string/vector v
  10.   * likewise, *v[0] INDICATES **v
  11.   *       and *v[n] INDICATES **(v + n)
  12.   * returntype (*funct)(args) INDICATES a function funct with arguments args which returns...
  13.   * char **argv INDICATES pointer to char pointer
  14.   * int(*v)[len] INDICATES pointer "v" to a vector of "len" int elements
  15.   * int *v[len] INDICATES vector "v" of "len" pointers to int elements
  16.   * void *funct() INDICATES function "funct" that returns a pointer-to-void
  17.   * void (*funct)() INDICATES pointer to a function "funct" that returns void
  18.   *
  19.   */
  20.  
  21.  /* useful characters: [] # */
  22.  
  23.  # include <stdio.h>
  24.  # include <sys/ipc.h> // system utilities
  25.  # include <sys/msg.h> // system utilities
  26.  # include <string.h>
  27.  
  28.  # define SIZE 1024; // message size
  29.  
  30.  typedef struct { // struct that represents a message
  31.    long type;
  32.    char text[]; // original: char text[SIZE], generates compilation error
  33.  } msg;
  34.  
  35.  int main(int argc, char * argv[])
  36.     {
  37.      /* message queue creation */
  38.      key_t key = 30; // univocally identifies the queue
  39.      int descriptor = msgget(key, IPC_CREAT|0666); // creates the queue and returns its ID
  40.      if(descriptor == -1) printf("Call failure in msgget (0)\n");
  41.      
  42.      
  43.      /* queue operations */
  44.      // DESCRIPTOR and KEY will be used without being re-instantiated
  45.      key = 40;
  46.      descriptor = msgget(key, IPC_CREAT|IPC_EXCL|0666);
  47.      if(descriptor == -1)
  48.     {
  49.      printf("Call failure in msgget (1)\n");
  50.      exit(1);
  51.     }
  52.      sleep(1);
  53.      printf("End sleep\n");
  54.      msgctl(descriptor, IPC_RMID, NULL); // does something with the queue; in this case, removes the ID (RMID)
  55.      
  56.      
  57.      /* messages: create and send */
  58.      // DESCRIPTOR and KEY will be used without being re-instantiated
  59.      key = 50;
  60.      msg message; // struct!
  61.      int sendsuccess; // if > 0 indicates success with send operation
  62.      descriptor = msgget(key, IPC_CREAT|0666);
  63.      if(descriptor == -1)
  64.     {
  65.      printf("Call failure in msgget (2)\n");
  66.      exit(1);
  67.     }
  68.      message.type = 1;                // message initialisation
  69.      * message.text = "Hello world!"; // message initialisation
  70.      sendsuccess = msgsnd(descriptor, &message, strlen(message.text) + 1, IPC_NOWAIT); // sends the message, non-blocking
  71.      /* receive and remove */
  72.      sendsuccess = msgrcv(descriptor, &message, strlen(message.text) + 1, 1, 0); // type: 1; blocking flag: 0 (nonblock)
  73.      if(sendsuccess != -1) printf("%s\n", message.text); // NOTE: return unexpected; why?
  74.      sendsuccess = msgctl(descriptor, IPC_RMID, NULL); // removes the descriptor
  75.    
  76.      
  77.      /* Sample at: pg. 38
  78.       * One function for the message producer, one for the consumer, both in a do-while(!quit)
  79.       * main() contains:
  80.       *     if(fork() != 0)
  81.       *         {
  82.       *         if(fork() != 0)
  83.       *             {
  84.       *             wait(&status);
  85.       *             wait(&status);
  86.       *             }
  87.       *         else producer(queue_descriptor);
  88.       *         }
  89.       *     else consumer(queue_descriptor);
  90.       * ...Which allows the reading of all messages first, and then the send.
  91.       */
  92.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement