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
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <mqueue.h>
#include <assert.h>
static mqd_t mq_test;
static mqd_t mq_recv;
static const size_t bufsiz = 256;
int main(int argc, char **argv)
{
int c_stat;
struct mq_attr m_attr;
char buf[bufsiz], recv[bufsiz];
unsigned int prio;
ssize_t sz_recv;
memset(buf, '\0', bufsiz);
memset(recv, '\0', bufsiz);
if (argc > 1)
strncpy(buf, argv[1], bufsiz-1);
m_attr.mq_flags = 0;
m_attr.mq_msgsize = bufsiz;
m_attr.mq_maxmsg = 10;
m_attr.mq_curmsgs = 0;
mq_unlink("/testmq");
assert( (mq_test = mq_open( "/testmq", O_NONBLOCK | O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG, &m_attr )) != (mqd_t)-1 );
assert( mq_getattr(mq_test, &m_attr) == 0 );
printf("flags..........: %ld\n"
"maxmsg.........: %ld\n"
"msgsize........: %ld\n"
"curmsg.........: %ld\n",
m_attr.mq_flags, m_attr.mq_maxmsg, m_attr.mq_msgsize, m_attr.mq_curmsgs);
strcpy(buf, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVQXYZ");
assert ( mq_send(mq_test, buf, bufsiz, 0) == 0 );
assert ( (sz_recv = mq_receive(mq_test, recv, bufsiz, &prio)) > 0 );
printf("SENT(%03lu bytes): %s\n", (long unsigned int) strlen(buf), buf);
printf("RECV(%03lu bytes): %s\n", (long unsigned int) sz_recv, recv);
memset(recv, '\0', bufsiz);
if (fork() > 0) {
assert( (mq_recv = mq_open( "/testmq", O_RDONLY, S_IRWXU | S_IRWXG, &m_attr )) != (mqd_t)-1 );
assert ( (sz_recv = mq_receive(mq_recv, recv, bufsiz, &prio)) > 0 );
printf("RECV(%03lu bytes): %s\n", (long unsigned int) sz_recv, recv);
return 0;
}
printf("SENT(%03lu bytes): %s\n", (long unsigned int) strlen(buf), buf);
assert ( mq_send(mq_test, buf, bufsiz, 0) == 0 );
wait(&c_stat);
return 0;
}
|