title: IPC mechanism using message queues in C
body: <p>I am trying to implement a IPC mechanism for telephone conversation using message queues in C. I have created two .c files, one for caller and one for receiver. In each .c file i have created two threads , one for sending messages and another for receiving messages. Each thread creates its message queue. The send message thread from caller and the receive message thread from receiver share the same queue and same for the other. </p>
  
  <p>The message queues are being created but whenever i enter some message to be sent , it fails. The msgsnd(-,-,-,-) always returns -1.</p>
  
  <p>The caller.c file is as follows :</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/ipc.h&gt;
  #include &lt;sys/msg.h&gt;
  #include &lt;pthread.h&gt;
  
  struct msgbuf
  {
    long mtype;
    char mtext[50];
  }SEND_BUFFER,RECEIVE_BUFFER;
  
  int send_msgQ_id, receive_msgQ_id;
  key_t send_key,receive_key;
  
  void * send_message(void * a)
  {
  send_key = ftok("Caller.c", 'B');
  if(send_key==-1)
  {
      printf("\n caller send key error");
      exit(1);
  }
  send_msgQ_id = msgget(send_key, 0666 | IPC_CREAT);
  if(send_msgQ_id==-1)
  {
      printf("\n caller send msgget error");
      exit(1);
  }
  
  printf("\n Enter lines of text, ^D to quit:\n");
  
      while(fgets(SEND_BUFFER.mtext, sizeof(SEND_BUFFER.mtext), stdin) != NULL)
      {
          SEND_BUFFER.mtype = 0;
  
          int len = strlen(SEND_BUFFER.mtext);
          if (SEND_BUFFER.mtext[len-1] == '\n')
              SEND_BUFFER.mtext[len-1] = '\0';
          printf("\n Attemping to send %s\n", SEND_BUFFER.mtext);
          if(msgsnd(send_msgQ_id, &amp;SEND_BUFFER, len+1, 0)==-1)
              printf("\n msg sendign error\n");
      }
  int i =0;
  while(i&lt;9999)
      i++;
  msgctl(send_msgQ_id, IPC_RMID, NULL);
  return;
  }
  
  void * receive_message(void * a)
  {
  receive_key = ftok("Receiver.c", 'B');
  if(receive_key==-1)
  {
      printf("\n caller receive key error");
      exit(1);
  }
  receive_msgQ_id = msgget(receive_key, 0777 | IPC_CREAT);
   if(receive_msgQ_id==-1)
   {
       printf("\n caller receive msgget error");
       exit(1);
   }
  
  printf("\n Ready to receive ");
  while(1)
  {
     if( msgrcv(receive_msgQ_id, &amp;RECEIVE_BUFFER, sizeof(RECEIVE_BUFFER.mtext), 0, 0)!=-1)
      printf("Received : %s\n", RECEIVE_BUFFER.mtext);
  }
  return;
  }
  
  void initialize()
  {
  
  pthread_t send_thread,receive_thread;
  pthread_create(&amp;send_thread,NULL,send_message,NULL);
  pthread_create(&amp;receive_thread,NULL,receive_message,NULL);
  pthread_join(send_thread,NULL);
  pthread_join(receive_thread,NULL);
  return;
  }
  
  int main()
  {
  printf("\n\n *** Caller Program ***\n");
  initialize();
  return 0;
  }
  </code></pre>
  
  <p>The receiver.c file is as follows (it is similar to the caller.c file) :</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/ipc.h&gt;
  #include &lt;sys/msg.h&gt;
  #include &lt;pthread.h&gt;
  
  struct msgbuf
  {
  long mtype;
  char mtext[50];
  }SEND_BUFFER,RECEIVE_BUFFER;
  
  int send_msgQ_id, receive_msgQ_id;
  key_t send_key,receive_key;
  
  void * send_message(void * a)
  {
  send_key = ftok("Receiver.c", 'B');
  if(send_key==-1)
  {
      printf("\n receiver send key error");
      exit(1);
  }
  send_msgQ_id = msgget(send_key, 0777);
  if(send_msgQ_id==-1)
  {
      printf("\n receiver msgget error ");
      exit(1);
  }
  printf("\n Enter lines of text, ^D to quit:\n");
  
      SEND_BUFFER.mtype = 0;
      while(fgets(SEND_BUFFER.mtext, sizeof(SEND_BUFFER.mtext), stdin) != NULL)
      {
          int len = strlen(SEND_BUFFER.mtext);
          if (SEND_BUFFER.mtext[len-1] == '\n')
              SEND_BUFFER.mtext[len-1] = '\0';
          msgsnd(send_msgQ_id, &amp;SEND_BUFFER, len+1, 0);
      }
      int i =0;
      while(i&lt;9999)
          i++;
  msgctl(send_msgQ_id, IPC_RMID, NULL);
  return;
  }
  
  void * receive_message(void * a)
  {
  receive_key = ftok("Caller.c", 'B');
  if(receive_key==-1)
  {
      printf("\n receiver receiver key error");
      exit(1);
  }
  receive_msgQ_id = msgget(receive_key, 0666);
  if(receive_msgQ_id==-1)
  {
      printf("\n msgget error");
      exit(1);
  }
  
  while(1)
  {
      if(msgrcv(receive_msgQ_id, &amp;RECEIVE_BUFFER, sizeof(RECEIVE_BUFFER.mtext), 0, 0)==-1)
          printf("\n msg receiving error");
      else
          printf("Received : %s\n", RECEIVE_BUFFER.mtext);
  }
  return;
  }
  
  void initialize()
  {
  pthread_t send_thread,receive_thread;
  pthread_create(&amp;receive_thread,NULL,receive_message,NULL);
  pthread_create(&amp;send_thread,NULL,send_message,NULL);
  pthread_join(send_thread,NULL);
  pthread_join(receive_thread,NULL);
  return;
  }
  
  int main()
  {
  printf("\n\n *** Receiver Program ***\n");
  initialize();
  return 0;
  }
  </code></pre>
  
  <p>I need that both the caller and sender can receive and send messages at their free will.</p>