README.md
1# msg_queue
2
3This is the Cuttlefish message queue wrapper library, as one of the IPC options available. Below are the example usages running in two separate processes.
4
5```
6#define MAX_MSG_SIZE 200
7#define NUM_MESSAGES 100
8
9typedef struct msg_buffer {
10 long mesg_type;
11 char mesg_text[MAX_MSG_SIZE];
12} msg_buffer;
13
14int example_send()
15{
16 // create message queue with the key 'a'
17 int queue_id = msg_queue_create('a');
18 struct msg_buffer msg;
19 for (int i=1; i <= NUM_MESSAGES; i++) {
20 // create message types 1-3
21 msg.mesg_type = (i % 3) + 1;
22 sprintf(msg.mesg_text, "test %d", i);
23 int rc = msg_queue_send(queue_id, &msg, strlen(msg.mesg_text)+1, false);
24 if (rc == -1) {
25 perror("main: msgsnd");
26 exit(1);
27 }
28 }
29 printf("generated %d messages, exiting.\n", NUM_MESSAGES);
30 return 0;
31}
32```
33
34```
35#define MAX_MSG_SIZE 200
36
37typedef struct msg_buffer {
38 long mesg_type;
39 char mesg_text[MAX_MSG_SIZE];
40} msg_buffer;
41
42int example_receive()
43{
44 // create message queue with the key 'a'
45 int queue_id = msg_queue_create('a');
46 struct msg_buffer msg;
47 while (1) {
48 int rc = msg_queue_receive(queue_id, &msg, MAX_MSG_SIZE, 1, false);
49 if (rc == -1) {
50 perror("main: msgrcv");
51 exit(1);
52 }
53 printf("Reader '%d' read message: '%s'\n", 1, msg.mesg_text);
54 }
55 return 0;
56}
57```
58