title: Message queue: msgsnd failed : Invalid argument
body: <p>Can anyone please help me to point out as what is the error in my program?</p>
  
  <p>Thanks in advance,
  kingsmasher1</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/ipc.h&gt;
  #include &lt;sys/msg.h&gt;
  #include &lt;string.h&gt;
  #include &lt;errno.h&gt;
  
  typedef struct msgbuf {
      long mtype;     /* message type, must be &gt; 0 */
      char mtext[15];  /* message data */
  } msgbuf;
  
  int main() {
      key_t key;
      int msqid, pid, length;
      msgbuf buf;
  
      msqid=msgget(IPC_PRIVATE,IPC_CREAT);
  
      if(msqid==-1){
          perror("msgget failed");
          return;
      }
      else {
          printf("msgget succeeded. ID:%u",msqid);
      }
  
      pid=fork();
  
      if(pid==-1) {
          perror("fork failed\n");
      }
  
      buf.mtype=1;
      strcpy(buf.mtext, "This is a test message");
      length=sizeof(buf.mtext);
  
      if(msgsnd(msqid,&amp;buf,length,0)!=0) {
          perror("msgsnd failed:\n");
      }
      else {
         printf("msgsnd succeeded\n");
      }
  }
  </code></pre>
  
  <p>Output:
      msgsnd failed: Invalid argument</p>