msgget() - get message queue
#include <sys/msg.h>
int msgget (key_t key, int msgflg)
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:
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>.
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.
The msgget(2) function can fail for the following reasons:
msgctl(2)
msgrcv(2)
msgsnd(2)