msgget()

NAME

msgget() - get message queue

SYNOPSIS

#include <sys/msg.h>

int msgget (key_t key, int msgflg)

DESCRIPTION

The msgget(2) function returns a message queue identifier associated with key.

The key can be an agreed-upon integer or it can have the value IPC_PRIVATE.

The msgflag controls the mode and permissions of the message queue. The following bits (defined in <sys/ipc.h>) can be OR'ed into msgflag:

IPC_CREAT
Create the entry if the key doesn't exist.
IPC_EXCL
Fail if the key already exists and its creation was requested.
IPC_NOWAIT
Return an error if the request must wait. This request controls the behavior of msgrcv(2) and msgsnd(2), while the IPC_CREAT and IPC_EXCL requests control how an IPC handle is obtained.

If the key already has a message queue identifier associated with it, then msgget(2) returns that value.

The msgget(2) function creates the message queue if:

When a message queue is created, the data structure associated with the identifier is initialized as follows:

Member Meaning Initialized
msg_perm.uid effective user ID of calling process
msg_perm.gid effective group ID of calling process
msg_perm.cuid effective user ID of calling process
msg_perm.cgid effective group ID of calling process
msg_perm.mode Permissions low-order 9 bits are set to low-order 9 bits of msgflg
msg_ctime time of last change current time
msg_lrpid process ID of last msgrcv() 0
msg_lspid process ID of last msgsnd() 0
msg_qbytes max num. of bytes allowed on queue system default
msg_qnum number of messages on queue 0
msg_rtime Time of last msgrcv() 0
msg_stime Time of last msgsnd() 0

Note that there is no distinction between user and effective user IDs. The limit for msg_qbytes is available memory.

When you create the queue, it has a size of 4K bytes. This size can be changed with the msgctl(IPC_SET) call. The maximum size of a queue is limited only be the ability of the system to allocate contiguous memory.

The header file <sys/ipc.h> is automatically included when you include the file <sys/msg.h>.

RETURN VALUE

On success, msgget(2) returns a message queue identifier, a non-negative integer . On failure, it returns -1 and sets errno to indicate the error.

ERRORS

The msgget(2) function can fail for the following reasons:

[EACCES]
A message queue identifier already exists for key, but permissions cannot be granted.
[EEXIST]
A message queue identifier already exists for key, but ((msgflg && (msgflg is non-zero.
[ENOENT]
No message queue identifier exists for key and creation of one was not requested (that is, msgflg&IPC_CREAT is zero).
[ENOSPC]
Creating the message queue identifier would exceed the system-wide limit on the number of message queue identifiers.

SEE ALSO

msgctl(2)

msgrcv(2)

msgsnd(2)