title: Simple IPC via linux messages
body: <p>I'm trying to write some example of client-server application which uses linux messages. Here is my code:</p>
  
  <pre><code>#include &lt;mqueue.h&gt;
  #include &lt;ctype.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;sys/types.h&gt;
  
  #define MSG_SIZE 4096
  #define MSG_MAX_COUNT 1024
  #define MSG_TYPE_TO_UPPER 0
  #define MSG_TYPE_EXIT 1
  #define MQ_NAME "msg_queue"
  
  namespace {
      int str_toupper(char *str)
      {
          int len = 0;
          for(; str[len]; ++len) {
              str[len] = toupper(str[len]);
          }
          return len;
      }
  }
  
  int main(int argc, char** argv)
  {
      if(argc != 2) {
          fprintf(stderr, "Usage: msg_queue (client|server)\n");
          exit(EXIT_FAILURE);
      }
  
      struct mq_attr attr;    // MQueue attributes
      mqd_t mqd;      // MQueue descriptor
      char buf[MSG_SIZE]; // Msg buffer
      unsigned int type;  // Msg type(priority)
  
      // Set up MQueue attributes
      attr.mq_maxmsg = MSG_MAX_COUNT;
      attr.mq_msgsize = MSG_SIZE;
      attr.mq_flags = 0;
      attr.mq_curmsgs = 0;
  
      mqd = mq_open(MQ_NAME, O_RDWR | O_CREAT, 0664, &amp;attr);
      if(mqd == -1) {
          fprintf(stderr, "mq_open() failed for \""MQ_NAME"\": %s\n", strerror(errno));
          exit(EXIT_FAILURE);
      }
  
      if(strcmp(argv[1], "server") == 0) {
          while(mq_receive (mqd, buf, MSG_SIZE, &amp;type) != -1) {
              if(type == MSG_TYPE_EXIT) {
                  mq_unlink(MQ_NAME);
                  mq_close(mqd);
                  break;
              } else if(type == MSG_TYPE_TO_UPPER) {
                  int len = str_toupper(buf);
                  if(mq_send (mqd, buf, len, MSG_TYPE_TO_UPPER) == -1) {
                      fprintf(stderr, "Server: mq_send() failed: %s", strerror(errno));
                  }
              }
          }
      } else if(strcmp(argv[1], "client") == 0) {
          while(1) {
              printf("Input a message: &lt;type&gt;(0 - TO_UPPER, 1 - EXIT) &lt;message&gt;\n");
              scanf("%u %s", &amp;type, buf);
  
              if(mq_send (mqd, buf, strlen(buf), type) == -1) {
                  fprintf(stderr, "Client: mq_send() failed: %s", strerror(errno));
              }
  
              if(type == MSG_TYPE_TO_UPPER) {
                  if(mq_receive (mqd, buf, MSG_SIZE, &amp;type) == -1) {
                      fprintf(stderr, "Client: mq_receive() failed: %s", strerror(errno));
                  }
                  printf("\"%s\" received\n", buf);
              } else if(type == MSG_TYPE_EXIT) {
                  mq_unlink(MQ_NAME);
                  mq_close(mqd);
                  break;
              }
          }
      } else {
          fprintf(stderr, "Usage: msg_queue (client|server)\n");
          exit(EXIT_FAILURE);
      }
      return 0;
  }
  </code></pre>
  
  <p>What is my mistake? It always prints error from the line 47 - <code>fprintf(stderr, "mq_open() failed for \""MQ_NAME"\": %s\n", strerror(errno));</code> with <code>errno = EINVAL</code>.</p>