Go to main content

man pages section 2: System Calls

Exit Print View

Updated: Friday, August 13, 2021



msgget - get message queue


#include <sys/msg.h>

int msgget(key_t key, int msgflg);


The msgget() argument returns the message queue identifier associated with key.

A message queue identifier and associated message queue and data structure (see Intro(2)) are created for key if one of the following are true:

  • key is IPC_PRIVATE.

  • key does not already have a message queue identifier associated with it, and (msgflg&IPC_CREAT) is true.

On creation, the data structure associated with the new message queue identifier is initialized as follows:

  • msg_perm.cuid, msg_perm.uid, msg_perm.cgid, and msg_perm.gid are set to the effective user ID and effective group ID, respectively, of the calling process.

  • The low-order 9 bits of msg_perm.mode are set to the low-order 9 bits of msgflg.

  • msg_qnum, msg_lspid, msg_lrpid, msg_stime, and msg_rtime are set to 0.

  • msg_ctime is set to the current time.

  • msg_qbytes is set to the system limit. See NOTES.

Return Values

Upon successful completion, a non-negative integer representing a message queue identifier is returned. Otherwise, −1 is returned and errno is set to indicate the error.


The msgget() function will fail if:


A message queue identifier exists for key, but operation permission (see Intro(2)) as specified by the low-order 9 bits of msgflg would not be granted.


A message queue identifier exists for key but (msgflg&IPC_CREAT) and (msgflg&IPC_EXCL) are both true.


A message queue identifier does not exist for key and (msgflg&IPC_CREAT) is false.


A message queue identifier is to be created but the system-imposed limit on the maximum number of allowed message queue identifiers system wide would be exceeded. See NOTES.


See attributes(7) for descriptions of the following attributes:

Interface Stability

See Also

Intro(2), msgctl(2), msgrcv(2), msgsnd(2), setrctl(2), ftok(3C), attributes(7), standards(7), rctladm(8)


The system-defined limit used to initialize msg_qbytes is the minimum enforced value of the calling process's process.max-msg-qbytes resource control.

The system-imposed limit on the number of message queue identifiers is maintained on a per-project basis using the project.max-msg-ids resource control. The zone.max-msg-ids resource control restricts the total amount of message queue identifiers that can be allocated by a zone.

See rctladm(8) and setrctl(2) for information about using resource controls.