-
Notifications
You must be signed in to change notification settings - Fork 1
/
prodcons2.c
175 lines (128 loc) · 3.13 KB
/
prodcons2.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
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
/*
Producer/Consumer sharing time through a system V message queue
Author: R. Koucha
Date: 28-Fev-2020
*/
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sched.h>
#include <unistd.h>
#include <stdlib.h>
#include <libgen.h>
#include <time.h>
#include <string.h>
#include <signal.h>
#include "util.h"
#define PRODUCER_NAME "producer2"
#define CONSUMER_NAME "consumer2"
static char *name;
#define MSG_KEY 0x12345
static int idq = -1;
static void sig_hdl(int s)
{
int rc;
printf("\n\n%s: Signal %d!\n\n", name, s);
// If I am the producer ==> Unlink the message queue
if (!strcmp(name, PRODUCER_NAME)) {
if (idq != -1) {
rc = msgctl(idq, IPC_RMID, 0);
if (rc < 0) {
ERR("msgctl(%d, IPC_RMID): '%m' (%d)\n", idq, errno);
exit(1);
}
}
}
exit(0);
} // sig_hdl
int main(int ac, char *av[])
{
int rc;
struct msgq {
long mtype;
char date[128];
} msg;
int c;
time_t t;
(void)ac;
name = basename(av[0]);
printf("%s#%d is starting...\n", name, getpid());
// Capture CTRL-C
(void)signal(SIGINT, sig_hdl);
// If I am the producer
if (!strcmp(name, PRODUCER_NAME)) {
int already_unshared = 0;
/*
* Producer
*/
// Destroy any previously created message queue
rc = msgget(MSG_KEY, 0);
if (rc >= 0) {
(void)msgctl(rc, IPC_RMID, 0);
}
// Get an identifier on a message queue
idq = msgget(MSG_KEY, IPC_CREAT|IPC_EXCL|0777);
if (idq < 0) {
ERR("msgget(0x%x): '%m' (%d)\n", MSG_KEY, errno);
return 1;
}
// Infinite loop
while (1) {
if (!already_unshared) {
prompt("Unshare ipc namespaces ([Y]/N) ? ");
c = getanswer();
if (c == 'Y' || c == 'y' || c == '\n') {
rc = unshare(CLONE_NEWIPC);
if (rc < 0) {
ERR("unshare(): '%m' (%d)\n", errno);
return 1;
}
already_unshared = 1;
}
} else {
prompt("Update time ([Y]/N) ? ");
c = getanswer();
if (c != 'Y' && c != 'y' && c != '\n') {
continue;
}
}
// Write current time in the message
msg.mtype = 1;
t = time(NULL);
ctime_r(&t, msg.date);
// Remove terminating '\n'
msg.date[strlen(msg.date) - 1] = '\0';
// Send the message
rc = msgsnd(idq, &msg, sizeof(msg) - sizeof(msg.mtype), 0);
if (rc != 0) {
ERR("msgsnd(): '%m' (%d)\n", errno);
return 1;
}
} // End while
} else {
/*
* Consumer
*/
// Get an identifier on the message queue
idq = msgget(MSG_KEY, 0);
if (idq < 0) {
ERR("msgget(0x%x): '%m' (%d)\n", MSG_KEY, errno);
return 1;
}
// Infinite loop
while (1) {
// Receive the message
rc = msgrcv(idq, &msg, sizeof(msg) - sizeof(msg.mtype), 1, 0);
if (rc < 0) {
ERR("msgrcv(%d): '%m' (%d)\n", idq, errno);
return 1;
}
// Print the date
printf(" %s\r", msg.date);
fflush(stdout);
} // End while
} // End if producer
return 0;
} // main