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