title: Linux programming. How to send array with message queue?
body: <p>I have writer.c and reader.c and I am trying to send array from writer and then print the numbers in the reader.</p>
  
  <pre><code>        //writer.c
  
      #include &lt;stdio.h&gt; 
      #include &lt;sys/ipc.h&gt; 
      #include &lt;sys/msg.h&gt; 
  
      // structure for message queue 
      struct mesg_buffer { 
          int mesg_type; 
          int a[5];
  
      } message; 
  
      int main() 
      { 
      message.a [0] = 5;
      message.a [1] = 10;
      message.a [2] = 15;
      message.a [3] = 34;
      message.a [4] = 34;
  
          key_t key; 
          int msgid; 
  
          // ftok to generate unique key 
          key = ftok(".", 65); 
  
          // msgget creates a message queue 
          // and returns identifier 
          msgid = msgget(key, 0666 | IPC_CREAT); 
          message.mesg_type = 1; 
  
   // msgsnd to send message 
      for(int i=0;i&lt;5;i++)
      msgsnd(msgid, (int *) &amp;message.a[i], sizeof(message.a[i]), 0);
  
  
  
          // display the message 
      for(int i=0;i&lt;5;i++)
          printf("Data send is : %d \n", message.a[i]); 
  
          return 0; 
      } 
  </code></pre>
  
  <p>reader.c</p>
  
  <pre><code> // C Program for Message Queue (Reader Process) 
  #include &lt;stdio.h&gt; 
  #include &lt;sys/ipc.h&gt; 
  #include &lt;sys/msg.h&gt; 
  
  // structure for message queue 
  struct mesg_buffer { 
      int mesg_type; 
      int a[5];
  } message; 
  
  int main() 
  { 
      key_t key; 
      int msgid; 
  
      // ftok to generate unique key 
      key = ftok(".", 65); 
  
      // msgget creates a message queue 
      // and returns identifier 
      msgid = msgget(key, 0666 | IPC_CREAT); 
  
      // msgrcv to receive message
      for(int i=0; i&lt;5; i++)
     msgrcv(msgid, &amp;message, sizeof(message.a[i]), 1, 0); 
  
      for(int i=0; i&lt;5; i++)
      printf("Data Received is : %d \n",  
                      message.a[i]); 
  
      // to destroy the message queue 
      msgctl(msgid, IPC_RMID, NULL); 
  
      return 0; 
  } 
  </code></pre>
  
  <p>I run firstly the writer.c and that's my output:</p>
  
  <blockquote>
    <p>Data send is : 5</p>
    
    <p>Data send is : 10</p>
    
    <p>Data send is : 15</p>
    
    <p>Data send is :34</p>
    
    <p>Data send is : 34</p>
  </blockquote>
  
  <p>But when I run the reader.c the program just stucks and nothing happens. Any suggestion how to pass the array?</p>
  
  <p><strong>edit:</strong> It seems that passing only the message in  msgsnd and msgrcv fixed the problem.</p>
  
  <pre><code>for(int i=0;i&lt;5;i++)
      msgsnd(msgid, &amp;message, sizeof(message), 0); 
  </code></pre>