In order to create a new message queue, or access an existing queue, the msgget() system call is used.
SYSTEM CALL: msgget(); PROTOTYPE: int msgget ( key_t key, int msgflg ); RETURNS: message queue identifier on success -1 on error: errno = EACCESS (permission denied) EEXIST (Queue exists, cannot create) EIDRM (Queue is marked for deletion) ENOENT (Queue does not exist) ENOMEM (Not enough memory to create queue) ENOSPC (Maximum queue limit exceeded) NOTES:
Create the queue if it doesn't already exist in the kernel.
When used with IPC_CREAT, fail if queue already exists.
If IPC_CREAT is used alone, msgget() either returns the message queue identifier for a newly created message queue, or returns the identifier for a queue which exists with the same key value. If IPC_EXCL is used along with IPC_CREAT, then either a new queue is created, or if the queue exists, the call fails with -1. IPC_EXCL is useless by itself, but when combined with IPC_CREAT, it can be used as a facility to guarantee that no existing queue is opened for access.
An optional octal mode may be OR'd into the mask, since each IPC object has permissions that are similar in functionality to file permissions on a UNIX file system!
Let's create a quick wrapper function for opening or creating message queue:
int open_queue( key_t keyval ) { int qid; if((qid = msgget( keyval, IPC_CREAT | 0660 )) == -1) { return(-1); } return(qid); }