-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgrcv.html
910 lines (681 loc) · 24.5 KB
/
msgrcv.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
<!-- Creator : groff version 1.22.4 -->
<!-- CreationDate: Wed Jan 29 11:26:48 2020 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="generator" content="groff -Thtml, see www.gnu.org">
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<meta name="Content-Style" content="text/css">
<style type="text/css">
p { margin-top: 0; margin-bottom: 0; vertical-align: top }
pre { margin-top: 0; margin-bottom: 0; vertical-align: top }
table { margin-top: 0; margin-bottom: 0; vertical-align: top }
h1 { text-align: center }
</style>
<title>MSGOP</title>
</head>
<body>
<h1 align="center">MSGOP</h1>
<a href="#NAME">NAME</a><br>
<a href="#SYNOPSIS">SYNOPSIS</a><br>
<a href="#DESCRIPTION">DESCRIPTION</a><br>
<a href="#RETURN VALUE">RETURN VALUE</a><br>
<a href="#ERRORS">ERRORS</a><br>
<a href="#CONFORMING TO">CONFORMING TO</a><br>
<a href="#NOTES">NOTES</a><br>
<a href="#BUGS">BUGS</a><br>
<a href="#EXAMPLE">EXAMPLE</a><br>
<a href="#SEE ALSO">SEE ALSO</a><br>
<a href="#COLOPHON">COLOPHON</a><br>
<hr>
<h2>NAME
<a name="NAME"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">msgrcv, msgsnd
- System V message queue operations</p>
<h2>SYNOPSIS
<a name="SYNOPSIS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>#include
<sys/types.h> <br>
#include <sys/ipc.h> <br>
#include <sys/msg.h></b></p>
<p style="margin-left:11%; margin-top: 1em"><b>int
msgsnd(int</b> <i>msqid</i><b>, const void
*</b><i>msgp</i><b>, size_t</b> <i>msgsz</i><b>, int</b>
<i>msgflg</i><b>);</b></p>
<p style="margin-left:11%; margin-top: 1em"><b>ssize_t
msgrcv(int</b> <i>msqid</i><b>, void *</b><i>msgp</i><b>,
size_t</b> <i>msgsz</i><b>, long</b> <i>msgtyp</i><b>, <br>
int</b> <i>msgflg</i><b>);</b></p>
<h2>DESCRIPTION
<a name="DESCRIPTION"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">The
<b>msgsnd</b>() and <b>msgrcv</b>() system calls are used to
send messages to, and receive messages from, a System V
message queue. The calling process must have write
permission on the message queue in order to send a message,
and read permission to receive a message.</p>
<p style="margin-left:11%; margin-top: 1em">The <i>msgp</i>
argument is a pointer to a caller-defined structure of the
following general form:</p>
<p style="margin-left:17%; margin-top: 1em">struct msgbuf {
<br>
long mtype; /* message type, must be > 0 */ <br>
char mtext[1]; /* message data */ <br>
};</p>
<p style="margin-left:11%; margin-top: 1em">The
<i>mtext</i> field is an array (or other structure) whose
size is specified by <i>msgsz</i>, a nonnegative integer
value. Messages of zero length (i.e., no <i>mtext</i> field)
are permitted. The <i>mtype</i> field must have a strictly
positive integer value. This value can be used by the
receiving process for message selection (see the description
of <b>msgrcv</b>() below).</p>
<p style="margin-left:11%; margin-top: 1em"><b>msgsnd()</b>
<br>
The <b>msgsnd</b>() system call appends a copy of the
message pointed to by <i>msgp</i> to the message queue whose
identifier is specified by <i>msqid</i>.</p>
<p style="margin-left:11%; margin-top: 1em">If sufficient
space is available in the queue, <b>msgsnd</b>() succeeds
immediately. The queue capacity is governed by the
<i>msg_qbytes</i> field in the associated data structure for
the message queue. During queue creation this field is
initialized to <b>MSGMNB</b> bytes, but this limit can be
modified using <b>msgctl</b>(2). A message queue is
considered to be full if either of the following conditions
is true:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="2%"></td>
<td width="86%">
<p>Adding a new message to the queue would cause the total
number of bytes in the queue to exceed the queue’s
maximum size (the <i>msg_qbytes</i> field).</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="2%"></td>
<td width="86%">
<p>Adding another message to the queue would cause the
total number of messages in the queue to exceed the
queue’s maximum size (the <i>msg_qbytes</i> field).
This check is necessary to prevent an unlimited number of
zero-length messages being placed on the queue. Although
such messages contain no data, they nevertheless consume
(locked) kernel memory.</p></td></tr>
</table>
<p style="margin-left:11%; margin-top: 1em">If insufficient
space is available in the queue, then the default behavior
of <b>msgsnd</b>() is to block until space becomes
available. If <b>IPC_NOWAIT</b> is specified in
<i>msgflg</i>, then the call instead fails with the error
<b>EAGAIN</b>.</p>
<p style="margin-left:11%; margin-top: 1em">A blocked
<b>msgsnd</b>() call may also fail if:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p style="margin-top: 1em">*</p></td>
<td width="2%"></td>
<td width="86%">
<p style="margin-top: 1em">the queue is removed, in which
case the system call fails with <i>errno</i> set to
<b>EIDRM</b>; or</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="2%"></td>
<td width="86%">
<p>a signal is caught, in which case the system call fails
with <i>errno</i> set to <b>EINTR</b>;<b>see signal</b>(7).
(<b>msgsnd</b>() is never automatically restarted after
being interrupted by a signal handler, regardless of the
setting of the <b>SA_RESTART</b> flag when establishing a
signal handler.)</p></td></tr>
</table>
<p style="margin-left:11%; margin-top: 1em">Upon successful
completion the message queue data structure is updated as
follows:</p>
<p style="margin-left:22%; margin-top: 1em"><i>msg_lspid</i>
is set to the process ID of the calling process.</p>
<p style="margin-left:22%; margin-top: 1em"><i>msg_qnum</i>
is incremented by 1.</p>
<p style="margin-left:22%; margin-top: 1em"><i>msg_stime</i>
is set to the current time.</p>
<p style="margin-left:11%; margin-top: 1em"><b>msgrcv()</b>
<br>
The <b>msgrcv</b>() system call removes a message from the
queue specified by <i>msqid</i> and places it in the buffer
pointed to by <i>msgp</i>.</p>
<p style="margin-left:11%; margin-top: 1em">The argument
<i>msgsz</i> specifies the maximum size in bytes for the
member <i>mtext</i> of the structure pointed to by the
<i>msgp</i> argument. If the message text has length greater
than <i>msgsz</i>, then the behavior depends on whether
<b>MSG_NOERROR</b> is specified in <i>msgflg</i>. If
<b>MSG_NOERROR</b> is specified, then the message text will
be truncated (and the truncated part will be lost); if
<b>MSG_NOERROR</b> is not specified, then the message
isn’t removed from the queue and the system call fails
returning -1 with <i>errno</i> set to <b>E2BIG</b>.</p>
<p style="margin-left:11%; margin-top: 1em">Unless
<b>MSG_COPY</b> is specified in <i>msgflg</i> (see below),
the <i>msgtyp</i> argument specifies the type of message
requested, as follows:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p style="margin-top: 1em">*</p></td>
<td width="2%"></td>
<td width="86%">
<p style="margin-top: 1em">If <i>msgtyp</i> is 0, then the
first message in the queue is read.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="2%"></td>
<td width="86%">
<p>If <i>msgtyp</i> is greater than 0, then the first
message in the queue of type <i>msgtyp</i> is read, unless
<b>MSG_EXCEPT</b> was specified in <i>msgflg</i>, in which
case the first message in the queue of type not equal to
<i>msgtyp</i> will be read.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="2%"></td>
<td width="86%">
<p>If <i>msgtyp</i> is less than 0, then the first message
in the queue with the lowest type less than or equal to the
absolute value of <i>msgtyp</i> will be read.</p></td></tr>
</table>
<p style="margin-left:11%; margin-top: 1em">The
<i>msgflg</i> argument is a bit mask constructed by ORing
together zero or more of the following flags: <b><br>
IPC_NOWAIT</b></p>
<p style="margin-left:22%;">Return immediately if no
message of the requested type is in the queue. The system
call fails with <i>errno</i> set to <b>ENOMSG</b>.</p>
<p style="margin-left:11%;"><b>MSG_COPY</b> (since Linux
3.8)</p>
<p style="margin-left:22%;">Nondestructively fetch a copy
of the message at the ordinal position in the queue
specified by <i>msgtyp</i> (messages are considered to be
numbered starting at 0).</p>
<p style="margin-left:22%; margin-top: 1em">This flag must
be specified in conjunction with <b>IPC_NOWAIT</b>, with the
result that, if there is no message available at the given
position, the call fails immediately with the error
<b>ENOMSG</b>. Because they alter the meaning of
<i>msgtyp</i> in orthogonal ways, <b>MSG_COPY</b> and
<b>MSG_EXCEPT</b> may not both be specified in
<i>msgflg</i>.</p>
<p style="margin-left:22%; margin-top: 1em">The
<b>MSG_COPY</b> flag was added for the implementation of the
kernel checkpoint-restore facility and is available only if
the kernel was built with the
<b>CONFIG_CHECKPOINT_RESTORE</b> option.</p>
<p style="margin-left:11%;"><b>MSG_EXCEPT</b></p>
<p style="margin-left:22%;">Used with <i>msgtyp</i> greater
than 0 to read the first message in the queue with message
type that differs from <i>msgtyp</i>.</p>
<p style="margin-left:11%;"><b>MSG_NOERROR</b></p>
<p style="margin-left:22%;">To truncate the message text if
longer than <i>msgsz</i> bytes.</p>
<p style="margin-left:11%; margin-top: 1em">If no message
of the requested type is available and <b>IPC_NOWAIT</b>
isn’t specified in <i>msgflg</i>, the calling process
is blocked until one of the following conditions occurs:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="2%"></td>
<td width="86%">
<p>A message of the desired type is placed in the
queue.</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="2%"></td>
<td width="86%">
<p>The message queue is removed from the system. In this
case, the system call fails with <i>errno</i> set to
<b>EIDRM</b>.</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="2%"></td>
<td width="86%">
<p>The calling process catches a signal. In this case, the
system call fails with <i>errno</i> set to <b>EINTR</b>.
(<b>msgrcv</b>() is never automatically restarted after
being interrupted by a signal handler, regardless of the
setting of the <b>SA_RESTART</b> flag when establishing a
signal handler.)</p></td></tr>
</table>
<p style="margin-left:11%; margin-top: 1em">Upon successful
completion the message queue data structure is updated as
follows:</p>
<p style="margin-left:22%; margin-top: 1em"><i>msg_lrpid</i>
is set to the process ID of the calling process.</p>
<p style="margin-left:22%; margin-top: 1em"><i>msg_qnum</i>
is decremented by 1.</p>
<p style="margin-left:22%; margin-top: 1em"><i>msg_rtime</i>
is set to the current time.</p>
<h2>RETURN VALUE
<a name="RETURN VALUE"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">On failure both
functions return -1 with <i>errno</i> indicating the error,
otherwise <b>msgsnd</b>() returns 0 and <b>msgrcv</b>()
returns the number of bytes actually copied into the
<i>mtext</i> array.</p>
<h2>ERRORS
<a name="ERRORS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">When
<b>msgsnd</b>() fails, <i>errno</i> will be set to one among
the following values:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p style="margin-top: 1em"><b>EACCES</b></p></td>
<td width="2%"></td>
<td width="78%">
<p style="margin-top: 1em">The calling process does not
have write permission on the message queue, and does not
have the <b>CAP_IPC_OWNER</b> capability in the user
namespace that governs its IPC namespace.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EAGAIN</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The message can’t be sent due to the
<i>msg_qbytes</i> limit for the queue and <b>IPC_NOWAIT</b>
was specified in <i>msgflg</i>.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EFAULT</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The address pointed to by <i>msgp</i> isn’t
accessible.</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EIDRM</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The message queue was removed.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EINTR</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>Sleeping on a full message queue condition, the process
caught a signal.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EINVAL</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>Invalid <i>msqid</i> value, or nonpositive <i>mtype</i>
value, or invalid <i>msgsz</i> value (less than 0 or greater
than the system value <b>MSGMAX</b>).</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ENOMEM</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The system does not have enough memory to make a copy of
the message pointed to by <i>msgp</i>.</p></td></tr>
</table>
<p style="margin-left:11%; margin-top: 1em">When
<b>msgrcv</b>() fails, <i>errno</i> will be set to one among
the following values:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p style="margin-top: 1em"><b>E2BIG</b></p></td>
<td width="2%"></td>
<td width="78%">
<p style="margin-top: 1em">The message text length is
greater than <i>msgsz</i> and <b>MSG_NOERROR</b> isn’t
specified in <i>msgflg</i>.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EACCES</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The calling process does not have read permission on the
message queue, and does not have the <b>CAP_IPC_OWNER</b>
capability in the user namespace that governs its IPC
namespace.</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EFAULT</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The address pointed to by <i>msgp</i> isn’t
accessible.</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EIDRM</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>While the process was sleeping to receive a message, the
message queue was removed.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EINTR</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>While the process was sleeping to receive a message, the
process caught a signal; see <b>signal</b>(7).</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EINVAL</b></p></td>
<td width="2%"></td>
<td width="78%">
<p><i>msqid</i> was invalid, or <i>msgsz</i> was less than
0.</p> </td></tr>
</table>
<p style="margin-left:11%;"><b>EINVAL</b> (since Linux
3.14)</p>
<p style="margin-left:22%;"><i>msgflg</i> specified
<b>MSG_COPY</b>, but not <b>IPC_NOWAIT</b>.</p>
<p style="margin-left:11%;"><b>EINVAL</b> (since Linux
3.14)</p>
<p style="margin-left:22%;"><i>msgflg</i> specified both
<b>MSG_COPY</b> and <b>MSG_EXCEPT</b>.</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ENOMSG</b></p></td>
<td width="2%"></td>
<td width="78%">
<p><b>IPC_NOWAIT</b> was specified in <i>msgflg</i> and no
message of the requested type existed on the message
queue.</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ENOMSG</b></p></td>
<td width="2%"></td>
<td width="78%">
<p><b>IPC_NOWAIT</b> and <b>MSG_COPY</b> were specified in
<i>msgflg</i> and the queue contains less than <i>msgtyp</i>
messages.</p> </td></tr>
</table>
<p style="margin-left:11%;"><b>ENOSYS</b> (since Linux
3.8)</p>
<p style="margin-left:22%;"><i>MSG_COPY</i> was specified
in <i>msgflg</i>, and this kernel was configured without
<b>CONFIG_CHECKPOINT_RESTORE</b>.</p>
<h2>CONFORMING TO
<a name="CONFORMING TO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">POSIX.1-2001,
POSIX.1-2008, SVr4.</p>
<p style="margin-left:11%; margin-top: 1em">The
<b>MSG_EXCEPT</b> and <b>MSG_COPY</b> flags are
Linux-specific; their definitions can be obtained by
defining the <b>_GNU_SOURCE</b> feature test macro.</p>
<h2>NOTES
<a name="NOTES"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">The inclusion
of <i><sys/types.h></i> and <i><sys/ipc.h></i>
isn’t required on Linux or by any version of POSIX.
However, some old implementations required the inclusion of
these header files, and the SVID also documented their
inclusion. Applications intended to be portable to such old
systems may need to include these header files.</p>
<p style="margin-left:11%; margin-top: 1em">The <i>msgp</i>
argument is declared as <i>struct msgbuf *</i> in glibc
2.0 and 2.1. It is declared as <i>void *</i> in glibc
2.2 and later, as required by SUSv2 and SUSv3.</p>
<p style="margin-left:11%; margin-top: 1em">The following
limits on message queue resources affect the <b>msgsnd</b>()
call:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>MSGMAX</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>Maximum size of a message text, in bytes (default value:
8192 bytes). On Linux, this limit can be read and modified
via <i>/proc/sys/kernel/msgmax</i>.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>MSGMNB</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>Maximum number of bytes that can be held in a message
queue (default value: 16384 bytes). On Linux, this limit can
be read and modified via <i>/proc/sys/kernel/msgmnb</i>. A
privileged process (Linux: a process with the
<b>CAP_SYS_RESOURCE</b> capability) can increase the size of
a message queue beyond <b>MSGMNB</b> using the
<b>msgctl</b>(2) <b>IPC_SET</b> operation.</p></td></tr>
</table>
<p style="margin-left:11%; margin-top: 1em">The
implementation has no intrinsic system-wide limits on the
number of message headers (<b>MSGTQL</b>) and the number of
bytes in the message pool (<b>MSGPOOL</b>).</p>
<h2>BUGS
<a name="BUGS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">In Linux 3.13
and earlier, if <b>msgrcv</b>() was called with the
<b>MSG_COPY</b> flag, but without <b>IPC_NOWAIT</b>, and the
message queue contained less than <i>msgtyp</i> messages,
then the call would block until the next message is written
to the queue. At that point, the call would return a copy of
the message, <i>regardless</i> of whether that message was
at the ordinal position <i>msgtyp</i>. This bug is fixed in
Linux 3.14.</p>
<p style="margin-left:11%; margin-top: 1em">Specifying both
<b>MSG_COPY</b> and <b>MSC_EXCEPT</b> in <i>msgflg</i> is a
logical error (since these flags impose different
interpretations on <i>msgtyp</i>). In Linux 3.13 and
earlier, this error was not diagnosed by <b>msgrcv</b>().
This bug is fixed in Linux 3.14.</p>
<h2>EXAMPLE
<a name="EXAMPLE"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">The program
below demonstrates the use of <b>msgsnd</b>() and
<b>msgrcv</b>().</p>
<p style="margin-left:11%; margin-top: 1em">The example
program is first run with the <b>-s</b> option to send a
message and then run again with the <b>-r</b> option to
receive a message.</p>
<p style="margin-left:11%; margin-top: 1em">The following
shell session shows a sample run of the program:</p>
<p style="margin-left:17%; margin-top: 1em">$ <b>./a.out
-s</b> <br>
sent: a message at Wed Mar 4 16:25:45 2015</p>
<p style="margin-left:17%; margin-top: 1em">$ <b>./a.out
-r</b> <br>
message received: a message at Wed Mar 4 16:25:45 2015</p>
<p style="margin-left:11%; margin-top: 1em"><b>Program
source</b> <br>
#include <stdio.h> <br>
#include <stdlib.h> <br>
#include <string.h> <br>
#include <time.h> <br>
#include <unistd.h> <br>
#include <errno.h> <br>
#include <sys/types.h> <br>
#include <sys/ipc.h> <br>
#include <sys/msg.h></p>
<p style="margin-left:11%; margin-top: 1em">struct msgbuf {
<br>
long mtype; <br>
char mtext[80]; <br>
};</p>
<p style="margin-left:11%; margin-top: 1em">static void
<br>
usage(char *prog_name, char *msg) <br>
{ <br>
if (msg != NULL) <br>
fputs(msg, stderr);</p>
<p style="margin-left:11%; margin-top: 1em">fprintf(stderr,
"Usage: %s [options]\n", prog_name); <br>
fprintf(stderr, "Options are:\n"); <br>
fprintf(stderr, "-s send message using
msgsnd()\n"); <br>
fprintf(stderr, "-r read message using
msgrcv()\n"); <br>
fprintf(stderr, "-t message type (default is
1)\n"); <br>
fprintf(stderr, "-k message queue key (default is
1234)\n"); <br>
exit(EXIT_FAILURE); <br>
}</p>
<p style="margin-left:11%; margin-top: 1em">static void
<br>
send_msg(int qid, int msgtype) <br>
{ <br>
struct msgbuf msg; <br>
time_t t;</p>
<p style="margin-left:11%; margin-top: 1em">msg.mtype =
msgtype;</p>
<p style="margin-left:11%; margin-top: 1em">time(&t);
<br>
snprintf(msg.mtext, sizeof(msg.mtext), "a message at
%s", <br>
ctime(&t));</p>
<p style="margin-left:11%; margin-top: 1em">if (msgsnd(qid,
(void *) &msg, sizeof(msg.mtext), <br>
IPC_NOWAIT) == -1) { <br>
perror("msgsnd error"); <br>
exit(EXIT_FAILURE); <br>
} <br>
printf("sent: %s\n", msg.mtext); <br>
}</p>
<p style="margin-left:11%; margin-top: 1em">static void
<br>
get_msg(int qid, int msgtype) <br>
{ <br>
struct msgbuf msg;</p>
<p style="margin-left:11%; margin-top: 1em">if (msgrcv(qid,
(void *) &msg, sizeof(msg.mtext), msgtype, <br>
MSG_NOERROR | IPC_NOWAIT) == -1) { <br>
if (errno != ENOMSG) { <br>
perror("msgrcv"); <br>
exit(EXIT_FAILURE); <br>
} <br>
printf("No message available for msgrcv()\n");
<br>
} else <br>
printf("message received: %s\n", msg.mtext); <br>
}</p>
<p style="margin-left:11%; margin-top: 1em">int <br>
main(int argc, char *argv[]) <br>
{ <br>
int qid, opt; <br>
int mode = 0; /* 1 = send, 2 = receive */ <br>
int msgtype = 1; <br>
int msgkey = 1234;</p>
<p style="margin-left:11%; margin-top: 1em">while ((opt =
getopt(argc, argv, "srt:k:")) != -1) { <br>
switch (opt) { <br>
case 's': <br>
mode = 1; <br>
break; <br>
case 'r': <br>
mode = 2; <br>
break; <br>
case 't': <br>
msgtype = atoi(optarg); <br>
if (msgtype <= 0) <br>
usage(argv[0], "-t option must be greater than
0\n"); <br>
break; <br>
case 'k': <br>
msgkey = atoi(optarg); <br>
break; <br>
default: <br>
usage(argv[0], "Unrecognized option\n"); <br>
} <br>
}</p>
<p style="margin-left:11%; margin-top: 1em">if (mode == 0)
<br>
usage(argv[0], "must use either -s or -r
option\n");</p>
<p style="margin-left:11%; margin-top: 1em">qid =
msgget(msgkey, IPC_CREAT | 0666);</p>
<p style="margin-left:11%; margin-top: 1em">if (qid == -1)
{ <br>
perror("msgget"); <br>
exit(EXIT_FAILURE); <br>
}</p>
<p style="margin-left:11%; margin-top: 1em">if (mode == 2)
<br>
get_msg(qid, msgtype); <br>
else <br>
send_msg(qid, msgtype);</p>
<p style="margin-left:11%; margin-top: 1em">exit(EXIT_SUCCESS);
<br>
}</p>
<h2>SEE ALSO
<a name="SEE ALSO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>msgctl</b>(2),
<b>msgget</b>(2), <b>capabilities</b>(7),
<b>mq_overview</b>(7), <b>sysvipc</b>(7)</p>
<h2>COLOPHON
<a name="COLOPHON"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">This page is
part of release 5.02 of the Linux <i>man-pages</i> project.
A description of the project, information about reporting
bugs, and the latest version of this page, can be found at
https://www.kernel.org/doc/man-pages/.</p>
<hr>
</body>
</html>