-
Notifications
You must be signed in to change notification settings - Fork 10
/
ipc.c
87 lines (77 loc) · 1.76 KB
/
ipc.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <mqueue.h>
#define Q_NAME "/ch6_ipc"
#define MAX_SIZE 1024
#define M_EXIT "done"
#define SRV_FLAG "-producer"
int main(int argc, char *argv[])
{
if (argc < 2)
{
producer();
}
else if (argc >= 2 && 0 == strncmp(argv[1], SRV_FLAG, strlen(SRV_FLAG)))
{
producer();
}
else
{
consumer();
}
}
int producer()
{
mqd_t mq;
struct mq_attr attr;
char buffer[MAX_SIZE];
int msg, i;
attr.mq_flags = 0;
attr.mq_maxmsg = 10;
attr.mq_msgsize = MAX_SIZE;
attr.mq_curmsgs = 0;
mq = mq_open(Q_NAME, O_CREAT | O_WRONLY, 0644, &attr);
/* seed random */
srand(time(NULL));
i = 0;
while (i < 500)
{
msg = rand() % 256;
memset(buffer, 0, MAX_SIZE);
sprintf(buffer, "%x", msg);
printf("Produced: %s\n", buffer);
fflush(stdout);
mq_send(mq, buffer, MAX_SIZE, 0);
i=i+1;
}
memset(buffer, 0, MAX_SIZE);
sprintf(buffer, M_EXIT);
mq_send(mq, buffer, MAX_SIZE, 0);
mq_close(mq);
mq_unlink(Q_NAME);
return 0;
}
int consumer()
{
struct mq_attr attr;
char buffer[MAX_SIZE + 1];
ssize_t bytes_read;
mqd_t mq = mq_open(Q_NAME, O_RDONLY);
if ((mqd_t)-1 == mq) {
printf("Either the producer has not been started or maybe I cannot access the same memory...\n");
exit(1);
}
do {
bytes_read = mq_receive(mq, buffer, MAX_SIZE, NULL);
buffer[bytes_read] = '\0';
printf("Consumed: %s\n", buffer);
} while (0 != strncmp(buffer, M_EXIT, strlen(M_EXIT)));
mq_close(mq);
return 0;
}