next up previous contents
Next: SYSTEM CALL: msgsnd() Up: 6.4.2 Message Queues Previous: Kernel ipc_perm structure

SYSTEM CALL: msgget()

In order to create a new message queue, or access an existing queue, the msgget() system call is used.

  SYSTEM CALL: msgget();                                                          

  PROTOTYPE: int msgget ( key_t key, int msgflg );                                             
    RETURNS: message queue identifier on success                                                       
             -1 on error: errno = EACCESS (permission denied)
                                  EEXIST (Queue exists, cannot create)
                                  EIDRM (Queue is marked for deletion)
                                  ENOENT (Queue does not exist)
                                  ENOMEM (Not enough memory to create queue)
                                  ENOSPC (Maximum queue limit exceeded)

The first argument to msgget() is the key value (in our case returned by a call to ftok()). This key value is then compared to existing key values that exist within the kernel for other message queues. At that point, the open or access operation is dependent upon the contents of the msgflg argument.


Create the queue if it doesn't already exist in the kernel.


When used with IPC_CREAT, fail if queue already exists.

If IPC_CREAT is used alone, msgget() either returns the message queue identifier for a newly created message queue, or returns the identifier for a queue which exists with the same key value. If IPC_EXCL is used along with IPC_CREAT, then either a new queue is created, or if the queue exists, the call fails with -1. IPC_EXCL is useless by itself, but when combined with IPC_CREAT, it can be used as a facility to guarantee that no existing queue is opened for access.

An optional octal mode may be OR'd into the mask, since each IPC object has permissions that are similar in functionality to file permissions on a UNIX file system!

Let's create a quick wrapper function for opening or creating message queue:

int open_queue( key_t keyval )
        int     qid;
        if((qid = msgget( keyval, IPC_CREAT | 0660 )) == -1)

Note the use of the explicit permissions of 0660. This small function either returns a message queue identifier (int), or -1 on error. The key value must be passed to it as its only argument.

Converted on:
Fri Mar 29 14:43:04 EST 1996