msgget()-Get Message Queue



Syntax

#include <sys/msg.h>
#include <sys/stat.h>

int msgget(key_t key, int msgflg);


Threadsafe: Yes

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

Parameters

key
(Input) Key associated with the message queue. Specifying a key of IPC_PRIVATE guarantees that a unique message queue is created. A key also can be generated by the caller or by calling the ftok() function.

msgflg
(Input) Operations and permissions flag.

The value of msgflg is either 0 or is obtained by performing an OR operation on one or more of the following constants:

'0x0100' or S_IRUSR
Permits the creator of the message queue to read it
'0x0080' or S_IWUSR
Permits the creator of the message queue to write it
'0x0020' or S_IRGRP
Permits the group associated with the message queue to read it
'0x0010' or S_IWGRP
Permits the group associated with the message queue to write it
'0x0004' or S_IROTH
Permits others to read the message queue
'0x0002' or S_IWOTH
Permits others to write the message queue
'0x0200' or IPC_CREAT
Creates the message queue if it does not exist already
'0x0400' or IPC_EXCL
Causes msgget() to fail if IPC_CREAT is set and the message queue already exists

Authorities

Figure 1-5. Authorization Required for msgget()
Object Referred to Authority Required errno
Message queue to be created None None
Existing message queue to be accessed See Note EACCES
Note: If the thread is accessing an existing message queue, the mode specified in the last 9 bits of msgflg must be a subset of the mode of the existing message queue.

Return Value

value
msgget() was successful. The value returned is the message queue ID associated with the key parameter.
-1
msgget() was not successful. The errno variable is set to indicate the error.

Error Conditions

If msgget() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.

[EACCES]
Permission denied.

An attempt was made to access an object in a way forbidden by its object access permissions.

The thread does not have access to the specified file, directory, component, or path.

If you are accessing a remote file through the Network File System, update operations to file permissions at the server are not reflected at the client until updates to data that is stored locally by the Network File System take place. (Several options on the Add Mounted File System (ADDMFS) command determine the time between refresh operations of local data.) Access to a remote file may also fail due to different mappings of user IDs (UID) or group IDs (GID) on the local and remote systems.

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

[EDAMAGE]
A damaged object was encountered.

A referenced object is damaged. The object cannot be used.

The message queue has been damaged by a previous message queue operation.

[EEXIST]
File exists.

The file specified already exists and the specified operation requires that it not exist.

The named file, directory, or path already exists.

A message queue identifier exists for the parameter key, but ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) is not zero. (& is a bitwise AND; && is a logical AND.)

[ENOENT]
No such path or directory.

The directory or a component of the path name specified does not exist.

A named file or directory does not exist or is an empty string.

A message queue identifier does not exist for the parameter key, and (msgflg & IPC_CREAT) is zero. (& is a bitwise AND.)

[ENOSPC]
No space available.

The requested operations required additional space on the device and there is no space left. This could also be caused by exceeding the user profile storage limit when creating or transferring ownership of an object.

Insufficient space remains to hold the intended file, directory, or link.

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.

[EUNKNOWN]
Unknown system state.

The operation failed because of an unknown system state. See any messages in the job log and correct any errors that are indicated, then retry the operation.

Error Messages

None.

Usage Notes

  1. A message queue identifier, associated message queue, and data structure (see the <sys/msg.h> header file) are created for the parameter key if one of the following is true:

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

Related Information

Example

The following example creates a message queue:



#include <sys/msg.h>
#include <sys/stat.h>

main() {
  int msgflg = 0;
  int msqid;

  msqid = msgget(IPC_PRIVATE, msgflg | IPC_CREAT | S_IRUSR | S_IWUSR);
}


Top | Interprocess Communication APIs
APIs by category


[Information Center Home Page | Feedback ] [Legal | AS/400 Glossary]