msgop(2) CLIX msgop(2)
NAME
msgop, msgrev, msgnd - Handles message operations
LIBRARY
Standard C Library (libc.a)
SYNOPSIS
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int msgsnd(
int msqid ,
struct msgbuf *msgp ,
int msgsz ,
int msgflg );
int msgrcv(
int msqid ,
struct msgbuf *msgp ,
int msgsz ,
long msgtyp ,
int msgflg );
PARAMETERS
msqid Represents the message queue identifier. The msgget() function
returns such an identifier after creating the necessary
resources.
msgp Points to the structure containing the message.
msgsz Represents the size of a message, ranging between 0 and a
system-imposed upper limit (configurable in the UNIXCFG product,
the default is 32768).
msgflg Specifies the action to be taken by the function.
msgtyp Specifies a positive integer used by the receiving process to
select an appropriate message.
DESCRIPTION
The msgsnd() function is used to send a message to the queue associated
with the message queue identifier specified by msqid. The value of msgp
is a pointer to a structure, msgbuf, containing the message. This
2/94 - Intergraph Corporation 1
msgop(2) CLIX msgop(2)
structure is composed of the following members:
long mtype; /* message type */
char mtext[]; /* message text */
The mtype member is a positive integer that can be used by the receiving
process for message selection. (See msgrcv() below). The mtext member is
any text of length msgsz bytes. The value of msgsz can range from 0 to a
system-imposed maximum.
The msgflg parameter specifies an action to be taken if one or more of the
following are true:
⊕ The number of bytes already on the queue is equal to msg_qbytes. (See
intro(2.)
⊕ The total number of messages on all queues system-wide is equal to the
system-imposed limit.
These actions are as follows:
⊕ If (msgflg & IPC_NOWAIT) is true, the message will not be sent and the
calling process will return immediately. (See the <sys/ipc.h> header
file.)
⊕ If (msgflg & IPC_NOWAIT) is false, the calling process will suspend
execution until one of the following occurs:
-- The condition responsible for the suspension no longer exists, in
which case the message is sent.
-- The msqid is removed from the system. (See the msgctl() function.)
When this occurs, errno is set equal to EIDRM and a value of -1 is
returned.
-- The calling process receives a signal that is to be caught. In this
case the message is not sent and the calling process resumes
execution in the manner prescribed in the signal() function.
Upon the successful completion of msgsnd(), the following actions are
taken with respect to the data structure associated with msqid. (See
intro().)
⊕ The value of msg_qnum is incremented by 1.
⊕ The value of msg_lspid is set equal to the process ID of the calling
process.
⊕ The value of msg_stime is set equal to the current time.
The msgrcv() function reads a message from the queue associated with the
2 Intergraph Corporation - 2/94
msgop(2) CLIX msgop(2)
message queue identifier specified by msqid, and places it in the
structure pointed to by msgp. This structure is composed of the following
members:
long mtype; /* message type */
char mtext[]; /* message text */
The mtype member is the type of the received message as specified by the
sending process. The mtext member is the text of the message. The msgsz
parameter specifies the size in bytes of mtext. The received message is
truncated to msgsz bytes if it is larger than msgsz and (msgflg &
MSG_NOERROR) is true. The truncated part of the message is lost and no
indication of the truncation is given to the calling process.
The msgtyp parameter specifies the type of message requested as follows:
⊕ If msgtyp is equal to 0, the first message on the queue is received.
⊕ If msgtyp is greater than 0, the first message of type msgtyp is
received.
⊕ If msgtyp is less than 0, the first message of the lowest type that is
less than or equal to the absolute value of msgtyp is received.
The value of msgflg specifies the action to be taken if a message of the
desired type is not on the queue. These actions are as follows:
⊕ If (msgflg & IPC_NOWAIT) is true, the calling process returns
immediately with a return value of -1 and errno set to ENOMSG.
⊕ If (msgflg & IPC_NOWAIT) is false, the calling process will suspend
execution until one of the following occurs:
-- A message of the desired type is placed on the queue.
-- The msqid parameter is removed from the system. When this occurs,
errno is set equal to EIDRM and a value of -1 is returned.
-- The calling process receives a signal that is to be caught. In this
case a message is not received and the calling process resumes
execution in the manner prescribed in signal().
Upon successful completion, the following actions are taken with respect
to the data structure associated with msqid. (See intro().)
⊕ The value of msg_qnum is decremented by 1.
⊕ The value of msg_lrpid is set equal to the process ID of the calling
process.
⊕ The value of msg_rtime is set equal to the current time.
2/94 - Intergraph Corporation 3
msgop(2) CLIX msgop(2)
EXAMPLES
1. To send a message of type 1:
struct msg {
int type;
char text[256];
} msg;
msg.type = 1;
strcpy(msg.text, "Message type 1");
if (msgsnd(msg_queue, &msg, strlen(msg.text), 0) < 0)
perror("Could not send message");
2. To receive a message of type 1:
if (msgrcv(msg_queue, &msg, sizeof(msg.text), 1, 0) < 0)
perror("Could not send message");
printf("Message = %s\n", msg.text);
RETURN VALUES
If msgsnd() or msgrcv() returns due to the receipt of a signal, a value of
-1 is returned to the calling process and errno is set to EINTR. If they
return due to removal of msqid from the system, a value of -1 is returned
and errno is set to EIDRM.
Upon successful completion, the return value is as follows:
⊕ The msgsnd() function returns a value of 0.
⊕ The msgrcv() function returns a value equal to the number of bytes
actually placed into mtext.
Otherwise, a value of -1 is returned and errno is set to indicate the
error.
ERRORS
The msgsnd() function fails and no message is sent if one or more of the
following are true:
[EINVAL] The msqid parameter is not a valid message queue identifier.
[EACCES] Operation permission is denied to the calling process. (See
intro().)
4 Intergraph Corporation - 2/94
msgop(2) CLIX msgop(2)
[EINVAL] The value of mtype is less than 1.
[EAGAIN] The message cannot be sent for one of the reasons cited above
and (msgflg & IPC_NOWAIT) is true.
[EIDRM] The specified msqid was removed while the process was suspended
waiting for resources to become available.
[EINTR] A signal was caught.
[EINVAL] The value of msgsz is less than 0 or greater than the system-
imposed limit.
[EFAULT] The msgp parameter points to an illegal address.
The msgrcv() function fails and no message is received if one or more of
the following are true:
[EINVAL] The value of msqid is not a valid message queue identifier.
[EACCES] Operation permission is denied to the calling process.
[EINVAL] The value of msgsz is less than 0.
[E2BIG] The value of mtext is greater than msgsz and (msgflg &
MSG_NOERROR) is false.
[EIDRM] The specified msqid was removed while the process was suspended
waiting for a message to be sent.
[EINTR] A signal was caught.
[ENOMSG] The queue does not contain a message of the desired type and
(msgtyp & IPC_NOWAIT) is true.
[EFAULT] The msgp parameter points to an illegal address.
RELATED INFORMATION
Functions: intro(2), msgctl(2), msgget(2), signal(2)
2/94 - Intergraph Corporation 5