#include <sys/msg.h>
int msgsnd(int msqid, const void *msgp,
size_t msgsz, int msgflg);
|
The msgsnd() function is used to send a message to the queue associated with the message queue identifier specified by msqid.
Parameters
The parameter msgp points to a user-defined buffer that must contain the following:
The following structure is an example of what this user-defined buffer might look like:
struct mymsg {
long int mtype; /* message type */
char mtext[1]; /* message text */
}
The structure member mtype is a long int that is greater than zero. It can be used by the receiving thread for message selection. The structure member mtext is any text of length msgsz bytes. The parameter msgsz can range from zero to a system-imposed maximum.
The parameter msgsz should include any bytes inserted by the compiler for padding or alignment purposes. These bytes are part of the message data and affect the total number of bytes in the message queue.
The following example shows pad data and how it affects the size of a message:
struct mymsg {
long int mtype; /* 12 bytes padding inserted after */
char *pointer; /* the mtype field by the compiler.*/
char c; /* 15 bytes padding inserted after */
char *pointer2; /* the c field by the compiler. */
} msg; /* After the mtype field, there are*/
/* 33 bytes of user data, but 60 */
/* bytes of data including padding.*/
msgsz = sizeof(msg) - sizeof(long int); /* 60 bytes. */
Authorities
Figure 1-7. Authorization Required for msgsnd()
| Object Referred to | Authority Required | errno |
|---|---|---|
| Message queue on which message is placed | Write | EACCES |
Return Value
Error Conditions
If msgsnd() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.
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.
The calling thread does not have write permission.
The message cannot be sent for one of the reasons cited above and (msgflg & IPC_NOWAIT) is not zero. (& is a bitwise AND.)
A referenced object is damaged. The object cannot be used.
The message queue has been damaged by a previous message queue operation.
In attempting to use an argument in a call, the system detected an address that is not valid.
While attempting to access a parameter passed to this function, the system detected an address that is not valid.
The message queue identifier msqid was removed from the system.
The function msgsnd() was interrupted by a signal.
A parameter passed to this function is not valid.
One of the following has occurred:
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
Related Information
Example
The following example sends a message to a message queue:
#include <sys/msg.h>
main() {
int msqid = 0;
int msgflg = 0;
int rc;
size_t msgsz;
struct mymsg {
long int mtype;
char mtext[256];
};
msgsz = 256;
mymsg.mtype = 1;
rc = msgsnd(msqid, &mymsg, msgsz, msgflg | IPC_NOWAIT);
}
| Top | Interprocess Communication APIs | APIs by category |
| [Information Center Home Page | Feedback ] | [Legal | AS/400 Glossary] |