Name

msgget - gets a message queue identifier

Library

libc.lib

Synopsis

  #include <sys/types.h>
  #include <sys/ipc.h>
  #include <sys/msg.h>
  int msgget (key_t key, int msgflg);

Return values

Upon successful completion a positive message queue identifier is returned. Otherwise, -1 is returned and the global variable errno is set to indicate the error.

Detailed description

The msgget function returns the message queue identifier associated with key. A message queue identifier is a unique integer greater than zero.

A message queue is created if either key is equal to IPC_PRIVATE, or key does not have a message queue identifier associated with it, and the IPC_CREAT bit is set in msgflg.

If a new message queue is created, the data structure associated with it (the msqid_ds structure, see msgctl is initialized as follows:


Examples

Example code to create a message queue using msgget
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

         
#define MESSAGE_Q_KEY 1000

         
int main(void)
{
   int msq_id, len;
   struct {
       long mtype;
       char mtext[128];
   } msg_buf;
   /*
    * Create a message queue with a given key
    */
    if ((msq_id = msgget(MESSAGE_Q_KEY, IPC_CREAT | IPC_EXCL | 0666)) == -1) {
       printf("Message Q create failed with errno %d\n", errno);
       return -1;
    }
    msg_buf.mtype = 1; /* message identifier */
    strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
    len = strlen(msg_buf.mtext)+1;
    /*
     * Put the message in the queue
     */
    if (msgsnd(msq_id, (struct msgbuf *)&msg_buf, len, 0) == -1) {
        printf("Message Q send failed with errno %d\n", errno);
    }
    return 0;
}

         

Example code to return an existing message queue with msgget

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

         
#define MESSAGE_Q_KEY 1000

         
int main(void)
{
   int msq_id;
   int msg_len = 128;
   int msg_type = 0; /* Any type of message */
   struct {
       long mtype;
       char mtext[128];
   } msg_buf;
   /*
    * Get the message queue id for the given key
    */
    if ((msq_id = msgget(MESSAGE_Q_KEY, 0)) == -1) {
       printf("Message Q  get id failed with errno %d\n", errno);
       return -1;
    }
    /*
     * Get the message from the queue
     */
    if (msgrcv(msq_id, (struct msgbuf *)&msg_buf, msg_len, msg_type, 0) == -1) {
        printf("Message Q recv failed with errno %d\n", errno);
    }
    /*
     * Remove the message queue
     */
     if (msgctl(msq_id, IPC_RMID, NULL) == -1) {
       printf("Message Q  delete failed with errno %d\n", errno);
       return -1;
     }
     return 0;
}

         


Errors

[EACCES]
  A message queue is already associated with key and the caller has no permission to access it.
[EEXIST]
  Both IPC_CREAT and IPC_EXCL are set in msgflg, and a message queue is already associated with key.
[ENOSPC]
  A new message queue could not be created because the system limit for the number of message queues has been reached.
[ENOENT]
  IPC_CREAT was not set in msgflg and no message queue associated with key was found.

See also

msgctl, msgrcv, msgsnd

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top