title: Full Duplex communication using message queue, continuously printing messeges
body: <p><strong>program1.c</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/ipc.h&gt;
  #include &lt;sys/msg.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;string.h&gt;
  #define SEND_KEY 2
  #define RCV_KEY 3
  
  typedef struct msgbuf 
  {
      long mtype;     /* message type, must be &gt; 0 */
      char mtext[100];  /* message data */
  } MSG_BUF;
  int main()
  {
      int msg_id;
      MSG_BUF mbuf_snd,mbuf_rcv;
  
      if((msg_id=msgget(5,IPC_CREAT |0644))&lt;0)
          perror("msgget");
  
      if(fork()==0)
      {
          // child is receiving
          while(1) {
              if((msgrcv(msg_id,&amp;mbuf_rcv,sizeof(mbuf_rcv.mtext),RCV_KEY,0))&lt;0)
                  perror("msgrcv");
              printf("Message : %s\n",mbuf_rcv.mtext);
          }
      }
      else
      {
          // Parent is writing
          while(1){
              mbuf_snd.mtype=SEND_KEY;
              printf("Enter the message\n");
              scanf(" %[^\n]",mbuf_snd.mtext);
              if((msgsnd(msg_id,&amp;mbuf_snd,strlen(mbuf_snd.mtext)+1,0))&lt;0)
                  perror("msgsnd");
          }
      }
      return 0;
  }
  </code></pre>
  
  <p><strong>program2.c</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/ipc.h&gt;
  #include &lt;sys/msg.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;string.h&gt;
  #define SEND_KEY 3
  #define RCV_KEY 2
  
  typedef struct msgbuf {
          long mtype;     /* message type, must be &gt; 0 */
          char mtext[100];  /* message data */
  }MSG_BUF;
  int main()
  {
      int msg_id;
      MSG_BUF mbuf_snd,mbuf_rcv;
      if((msg_id=msgget(5,IPC_CREAT |0644))&lt;0)
          perror("msgget");
      if(fork()==0)
      {
          //child is receiving 
          while(1){
              if((msgrcv(msg_id,&amp;mbuf_rcv,sizeof(mbuf_rcv.mtext),RCV_KEY,0))&lt;0)
                  perror("msgrcv");
              printf("Message : %s\n",mbuf_rcv.mtext);
          }
      }
      else
      {
          // Parent is writing
          while(1){
              mbuf_snd.mtype=SEND_KEY;
              printf("Enter the message\n");
              scanf(" %[^\n]",mbuf_snd.mtext);
              if((msgsnd(msg_id,&amp;mbuf_snd,strlen(mbuf_snd.mtext)+1,0))&lt;0)
                  perror("msgsnd");
              }
      }
      return 0;
  }
  </code></pre>
  
  <p>Program1 parent is writing message for mtype 2 and child receiving message from mtype 3. </p>
  
  <p>Program2 parent is writing message for mtype 3 and child receiving mesaage from mtype 2.</p>
  
  <p>But after compiling it is not giving any error. But when I execute it, it is continuously printing data on screen. My requirement is it should stop after printing data and wait for next data. </p>
  
  <p>Could you explain me what is happening?</p>