1MSGSND(P) POSIX Programmer's Manual MSGSND(P)
2
3
4
6 msgsnd - XSI message send operation
7
9 #include <sys/msg.h>
10
11 int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
12
13
15 The msgsnd() function operates on XSI message queues (see the Base Def‐
16 initions volume of IEEE Std 1003.1-2001, Section 3.224, Message Queue).
17 It is unspecified whether this function interoperates with the realtime
18 interprocess communication facilities defined in Realtime .
19
20 The msgsnd() function shall send a message to the queue associated with
21 the message queue identifier specified by msqid.
22
23 The application shall ensure that the argument msgp points to a user-
24 defined buffer that contains first a field of type long specifying the
25 type of the message, and then a data portion that holds the data bytes
26 of the message. The structure below is an example of what this user-
27 defined buffer might look like:
28
29
30 struct mymsg {
31 long mtype; /* Message type. */
32 char mtext[1]; /* Message text. */
33 }
34
35 The structure member mtype is a non-zero positive type long that can be
36 used by the receiving process for message selection.
37
38 The structure member mtext is any text of length msgsz bytes. The argu‐
39 ment msgsz can range from 0 to a system-imposed maximum.
40
41 The argument msgflg specifies the action to be taken if one or more of
42 the following is true:
43
44 * The number of bytes already on the queue is equal to msg_qbytes; see
45 <sys/msg.h>.
46
47 * The total number of messages on all queues system-wide is equal to
48 the system-imposed limit.
49
50 These actions are as follows:
51
52 * If (msgflg & IPC_NOWAIT) is non-zero, the message shall not be sent
53 and the calling thread shall return immediately.
54
55 * If (msgflg & IPC_NOWAIT) is 0, the calling thread shall suspend exe‐
56 cution until one of the following occurs:
57
58 * The condition responsible for the suspension no longer exists, in
59 which case the message is sent.
60
61 * The message queue identifier msqid is removed from the system;
62 when this occurs, errno shall be set equal to [EIDRM] and -1
63 shall be returned.
64
65 * The calling thread receives a signal that is to be caught; in
66 this case the message is not sent and the calling thread resumes
67 execution in the manner prescribed in sigaction() .
68
69 Upon successful completion, the following actions are taken with
70 respect to the data structure associated with msqid; see <sys/msg.h>:
71
72 * msg_qnum shall be incremented by 1.
73
74 * msg_lspid shall be set equal to the process ID of the calling
75 process.
76
77 * msg_stime shall be set equal to the current time.
78
80 Upon successful completion, msgsnd() shall return 0; otherwise, no mes‐
81 sage shall be sent, msgsnd() shall return -1, and errno shall be set to
82 indicate the error.
83
85 The msgsnd() function shall fail if:
86
87 EACCES Operation permission is denied to the calling process; see XSI
88 Interprocess Communication .
89
90 EAGAIN The message cannot be sent for one of the reasons cited above
91 and (msgflg & IPC_NOWAIT) is non-zero.
92
93 EIDRM The message queue identifier msqid is removed from the system.
94
95 EINTR The msgsnd() function was interrupted by a signal.
96
97 EINVAL The value of msqid is not a valid message queue identifier, or
98 the value of mtype is less than 1; or the value of msgsz is less
99 than 0 or greater than the system-imposed limit.
100
101
102 The following sections are informative.
103
105 Sending a Message
106 The following example sends a message to the queue identified by the
107 msqid argument (assuming that value has previously been set). This call
108 specifies that an error should be reported if no message is available.
109 The message size is calculated directly using the sizeof operator.
110
111
112 #include <sys/msg.h>
113 ...
114 int result;
115 int msqid;
116 struct message {
117 long type;
118 char text[20];
119 } msg;
120
121
122 msg.type = 1;
123 strcpy(msg.text, "This is message 1");
124 ...
125 result = msgsnd(msqid, (void *) &msg, sizeof(msg.text), IPC_NOWAIT);
126
128 The POSIX Realtime Extension defines alternative interfaces for inter‐
129 process communication (IPC). Application developers who need to use IPC
130 should design their applications so that modules using the IPC routines
131 described in XSI Interprocess Communication can be easily modified to
132 use the alternative interfaces.
133
135 None.
136
138 None.
139
141 XSI Interprocess Communication , Realtime , mq_close() , mq_getattr() ,
142 mq_notify() , mq_open() , mq_receive() , mq_send() , mq_setattr() ,
143 mq_unlink() , msgctl() , msgget() , msgrcv() , sigaction() , the Base
144 Definitions volume of IEEE Std 1003.1-2001, <sys/msg.h>
145
147 Portions of this text are reprinted and reproduced in electronic form
148 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
149 -- Portable Operating System Interface (POSIX), The Open Group Base
150 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
151 Electrical and Electronics Engineers, Inc and The Open Group. In the
152 event of any discrepancy between this version and the original IEEE and
153 The Open Group Standard, the original IEEE and The Open Group Standard
154 is the referee document. The original Standard can be obtained online
155 at http://www.opengroup.org/unix/online.html .
156
157
158
159IEEE/The Open Group 2003 MSGSND(P)