---
name: "msgget"
section: "2"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["linux", "freebsd"]
url: "https://new.carta.tech/msgget/2"
---

# msgget(2)

get message queue

## NAME

**msgget** – get message queue

## LIBRARY

Standard C Library (libc, -lc)

## SYNOPSIS

**#include \<sys/msg.h\>**

*int*\

**msgget**(*key\_t key*, *int msgflg*);

## 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(2)](/msgctl/2.md)) is initialized as follows:

- *msg\_perm.cuid* and *msg\_perm.uid* are set to the effective uid of the calling process.

- *msg\_perm.gid* and *msg\_perm.cgid* are set to the effective gid of the calling process.

- *msg\_perm.mode* is set to the lower 9 bits of *msgflg* which are set by ORing these constants:

  - 0400

    Read access for user.

  - 0200

    Write access for user.

  - 0040

    Read access for group.

  - 0020

    Write access for group.

  - 0004

    Read access for other.

  - 0002

    Write access for other.

- *msg\_cbytes*, *msg\_qnum*, *msg\_lspid*, *msg\_lrpid*, *msg\_rtime*, and *msg\_stime* are set to 0.

- *msg\_qbytes* is set to the system wide maximum value for the number of bytes in a queue (MSGMNB).

- *msg\_ctime* is set to the current time.

## 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.

## 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(2)](/msgctl/2.md), [msgrcv(2)](/msgrcv/2.md), [msgsnd(2)](/msgsnd/2.md)

## HISTORY

Message queues appeared in the first release of AT&T System V UNIX.
