-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpmq_rcv.c
More file actions
76 lines (67 loc) · 1.65 KB
/
pmq_rcv.c
File metadata and controls
76 lines (67 loc) · 1.65 KB
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
/*
* Receiving a message via a posix message queue - making use of the
* 'timed' variant.
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <mqueue.h>
#include <signal.h>
#define MQ_NAME "/pmq_rcv"
#define MLEN 100
#define TTIME 5
static mqd_t q;
static void exit_err(char *reason) {
perror(reason);
exit(EXIT_FAILURE);
}
static void sendmsg(int sig) {
if (mq_send(q, "hello!", 7, 1) == -1)
exit_err("mq_send");
}
int main(int argc, char **argv) {
struct timespec ts;
char msg[MLEN];
struct mq_attr attr;
sigset_t block;
struct sigaction sigact;
pid_t child;
// Create mq if not exists.
attr.mq_maxmsg = 10;
attr.mq_msgsize = MLEN;
if ((q = mq_open(MQ_NAME, O_CREAT | O_RDWR, S_IRWXU, &attr)) == (mqd_t)-1)
exit_err("mq_open");
// Set up signals and child proc to handle sending a message on sigint
switch (child = fork()) {
case -1:
exit_err("fork");
case 0:
sigact.sa_handler = sendmsg;
sigact.sa_flags = 0;
sigemptyset(&sigact.sa_mask);
if (sigaction(SIGINT, &sigact, NULL) == -1)
exit_err("sigaction");
pause();
_exit(EXIT_SUCCESS);
default:
break;
}
sigemptyset(&block);
sigaddset(&block, SIGINT);
if (sigprocmask(SIG_BLOCK, &block, NULL) == -1)
exit_err("sigprocmask");
// timed receive
ts.tv_sec = time(NULL) + TTIME;
printf("mq_timedreceive with timeout of %d seconds\n", TTIME);
printf("press ctrl+c to send message on queue\n");
if (mq_timedreceive(q, msg, MLEN, NULL, &ts) == -1)
exit_err("mq_timedreceive");
else
printf("Message received: %.*s\n", MLEN, msg);
kill(child, SIGQUIT);
exit(EXIT_SUCCESS);
}