Advertisement
Kallikanzarid

Untitled

Oct 22nd, 2013
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <mqueue.h>
  6. #include <errno.h>
  7. #include <fcntl.h>
  8.  
  9. int main() {
  10.     struct mq_attr attrs;
  11.     attrs.mq_maxmsg = 10;
  12.     attrs.mq_msgsize = sizeof(int);
  13.  
  14.     const char name[] = "/test-queue";
  15.  
  16.     mqd_t q = mq_open(name, O_CREAT | O_RDWR, 0600, &attrs);
  17.     if (q == (mqd_t)-1) {
  18.         perror("mq_open");
  19.         exit(EXIT_FAILURE);
  20.     }
  21.  
  22.     if (fork()) {
  23.         int msg = 666;
  24.         if (mq_send(q, (const char *)&msg, sizeof(msg), 1)) {
  25.             perror("mq_send");
  26.             exit(EXIT_FAILURE);
  27.         }
  28.     } else {
  29.         int msg;
  30.         unsigned priority;
  31.         if (mq_receive(q, (char *)&msg, sizeof(msg), &priority) == -1) {
  32.             perror("mq_receive");
  33.             exit(EXIT_FAILURE);
  34.         }
  35.         printf("%d\n", msg);
  36.     }
  37.  
  38.     mq_unlink(name);
  39.     mq_close(q);
  40.  
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement