The msgget() system call returns the System message queue identifier associated with the value of the key argument. A new message queue is created if key has the value IPC_PRIVATE or key isn’t IPC_PRIVATE, no message queue with the given key exists, and IPC_CREAT is specified in msgflg. If msgflg specifies both IPC_CREAT and IPC_EXCL and a message queue already exists for key, then msgget() fails with errno set to EEXIST. This is analogous to the effect of the combination O_CREAT | O_EXCL for open.
These header files are used:
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
Syntax: int msgget(key_t key, int msgflg);
RETURN VALUE: If successful, the return value will be the message queue identifier (a nonnegative integer), otherwise -1 with errno indicating the error.
ERRORS: On failure, errno is set to one of the following values which are given below:
- EACCES: A message queue exists for key, but the calling process does not have permission to access the queue, and does not have the CAP_IPC_OWNER capability.
- EEXIST: A message queue exists for key and msgflg specified both IPC_CREAT and IPC_EXCL.
- ENOENT: No message queue exists for key and msgflg did not specify IPC_CREAT.
- ENOMEM: A message queue has to be created but the system does not have enough memory for the new data structure.
- ENOSPC: A message queue has to be created but the system limit for the maximum number of message queues (MSGMNI) would be exceeded.