-
Notifications
You must be signed in to change notification settings - Fork 1
/
prodcons.c
257 lines (191 loc) · 4.67 KB
/
prodcons.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
Producer/Consumer sharing time through a shared memory segment
Author: R. Koucha
Date: 28-Fev-2020
*/
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sched.h>
#include <unistd.h>
#include <stdlib.h>
#include <libgen.h>
#include <time.h>
#include <string.h>
#include <semaphore.h>
#include <signal.h>
#include "util.h"
#define PRODUCER_NAME "producer"
#define CONSUMER_NAME "consumer"
static char *name;
#define SHM_KEY 0x12345
#define SHM_SIZE 4096
static int idm = -1;
static char *pShm;
static void sig_hdl(int s)
{
int rc;
printf("\n\n%s: Signal %d!\n\n", name, s);
if (pShm) {
rc = shmdt(pShm);
if (rc != 0) {
ERR("shmdt(%p): '%m' (%d)\n", pShm, errno);
exit(1);
}
}
// If I am the producer ==> Unlink the message queue
if (!strcmp(name, PRODUCER_NAME)) {
if (idm != -1) {
rc = shmctl(idm, IPC_RMID, 0);
if (rc < 0) {
ERR("shmctl(%d, IPC_RMID): '%m' (%d)\n", idm, errno);
exit(1);
}
}
}
exit(0);
} // sig_hdl
static int P(sem_t *sem)
{
int rc;
rc = sem_wait(sem);
if (rc != 0) {
ERR("sem_wait(): '%m' (%d)\n", errno);
return -1;
}
return 0;
} // P
static int V(sem_t *sem)
{
int rc;
rc = sem_post(sem);
if (rc != 0) {
ERR("sem_post(): '%m' (%d)\n", errno);
return -1;
}
return 0;
} // V
int main(int ac, char *av[])
{
int rc;
char *pData;
size_t data_sz;
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 memory segment
rc = shmget(SHM_KEY, 0, 0);
if (rc >= 0) {
(void)shmctl(rc, IPC_RMID, 0);
}
// Get an identifier on a shared memory segment
idm = shmget(SHM_KEY, SHM_SIZE, IPC_CREAT|IPC_EXCL|0777);
if (idm < 0) {
ERR("shmget(0x%x): '%m' (%d)\n", SHM_KEY, errno);
return 1;
}
// Attach the memory segment to the virtual address space
pShm = shmat(idm, 0, 0777);
if (pShm == (char *)-1) {
ERR("shmat(%d): '%m' (%d)\n", idm, errno);
return 1;
}
printf("%s: Shared memory segment attached at: %p\n", name, pShm);
// Semaphore creation and initialization to 1
rc = sem_init((sem_t *)pShm, 1, 1);
if (rc < 0) {
return 1;
}
// Data zone (right after the semaphore)
pData = (char *)(((sem_t *)pShm) + 1);
data_sz = (pShm + SHM_SIZE) - pData;
// Reset the zone
memset(pData, 0, data_sz);
// 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;
}
}
rc = P((sem_t *)pShm);
if (rc != 0) {
return 1;
}
// Write current time in the shared memory segment
t = time(NULL);
ctime_r(&t, pData);
// Remove terminating '\n'
*(pData + strlen(pData) - 1) = '\0';
rc = V((sem_t *)pShm);
if (rc != 0) {
return 1;
}
} // End while
} else {
/*
* Consumer
*/
// Get an identifier on a shared memory segment
idm = shmget(SHM_KEY, SHM_SIZE, 0);
if (idm < 0) {
ERR("shmget(0x%x): '%m' (%d)\n", SHM_KEY, errno);
return 1;
}
// Attach the memory segment to the virtual address space
pShm = shmat(idm, 0, 0777);
if (pShm == (char *)-1) {
ERR("shmat(%d): '%m' (%d)\n", idm, errno);
return 1;
}
printf("%s: Shared memory segment attached at %p\n", name, pShm);
// Data zone (right after the semaphore)
pData = (char *)(((sem_t *)pShm) + 1);
data_sz = (pShm + SHM_SIZE) - pData;
// Infinite loop
while (1) {
sleep(1);
rc = P((sem_t *)pShm);
if (rc != 0) {
return 1;
}
// If there is something to read in the shared memory segment
if (pData[0]) {
printf(" %s\r", pData);
fflush(stdout);
// Reset the zone
memset(pData, 0, data_sz);
}
rc = V((sem_t *)pShm);
if (rc != 0) {
return 1;
}
} // End while
} // End if producer
return 0;
} // main