This repository has been archived by the owner on Jun 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
semaphore.c
274 lines (213 loc) · 6.44 KB
/
semaphore.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <errno.h>
#include <stdio.h>
#include <sys/shm.h>
#include <stdarg.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#define SEMKEY 21
#define SEMNUM 4
#define NSOPS 6
#define SHMKEY 22
#define SHMSIZE 32
int err_printf (char* format, ...);
int producer (char* pathname);
int consumer ();
int semid = -1;
int shmid = -1;
void* shmaddr = (void *) -1;
struct sembuf sops [NSOPS] = {};
int main (int argc, char* argv[])
{
int is_producer = (argc == 2);
if (argc > 2)
{
errno = EINVAL;
return err_printf ("Too many arguments\n");
}
if ((semid = semget (SEMKEY, SEMNUM, IPC_CREAT | 0666)) == -1)
return err_printf ("Can't create semaphore's set\n");
if ((shmid = shmget (SHMKEY, SHMSIZE + 8, IPC_CREAT | 0666)) == -1)
return err_printf ("Can't create shared memory\n");
if ((shmaddr = shmat (shmid, NULL, 0)) == (void *) -1)
return err_printf ("Can't attach shared memory\n");
if (is_producer)
{
if (producer (argv[1]) == -1)
{
fprintf (stderr, "producer error\n");
exit (EXIT_FAILURE);
}
}
else
{
if (consumer () == -1)
{
fprintf (stderr, "consumer error\n");
exit (EXIT_FAILURE);
}
}
return 0;
}
#define BYTES_WRITTEN ((int *)shmaddr)[0]
#define BYTES_READ ((int *)shmaddr)[1]
#define SEMPROD 0
#define SEMCONS 1
#define SHMRD 2
#define SHMWR 3
#define SEMPUSH( num, op, flg ) \
do \
{ \
sops[counter].sem_num = num; \
sops[counter].sem_op = op; \
sops[counter++].sem_flg = flg; \
} while(0)
#define SEMOP_NORET() \
do \
{ \
semop (semid, sops, counter); \
counter = 0; \
} while(0)
#define SEMOP() \
do \
{ \
if (semop (semid, sops, counter) == -1) \
return err_printf ("semop error\n"); \
\
counter = 0; \
} while(0)
#define SEMCHECK( num, val ) \
({ \
SEMPUSH (num, -1 * val, IPC_NOWAIT); \
SEMPUSH (num, 0, IPC_NOWAIT); \
SEMPUSH (num, val, IPC_NOWAIT); \
\
int res = semop (semid, sops, counter); \
counter = 0; \
res; \
})
/*
ALGORITHM:
case (SEMPROD == 0): no producer;
case (SEMPROD == 1): single producer;
case (SEMPROD == 2): producer which has (had) consumer;
case (SEMCONS == 0): no consumer;
case (SEMCONS == 1): consumer exists;
*/
int producer (char* pathname)
{
int counter = 0;
SEMPUSH (SEMPROD, 0, 0);
SEMPUSH (SEMCONS, 0, 0);
SEMPUSH (SEMPROD, 2, SEM_UNDO);
SEMPUSH (SEMPROD, -1, 0);
SEMOP(); // if no {consumer + producer} => producer enters
SEMPUSH (SHMWR, -1, IPC_NOWAIT);
SEMOP_NORET();
SEMPUSH (SEMCONS, -1, 0);
SEMPUSH (SEMCONS, 0, 0);
SEMPUSH (SEMCONS, 1, 0);
SEMOP(); // waits for consumer
int filefd = -1;
if ((filefd = open (pathname, O_RDONLY)) == -1)
return err_printf ("can't open %s\n", pathname);
int read_result = -1;
while (1)
{
SEMPUSH (SEMCONS, -1, IPC_NOWAIT);
SEMPUSH (SEMCONS, 1, 0);
SEMPUSH (SHMWR, 0, 0);
SEMPUSH (SHMWR, 1, SEM_UNDO);
SEMPUSH (SHMRD, 1, SEM_UNDO);
SEMPUSH (SHMRD, -1, 0);
SEMOP();
void* buf = shmaddr + 8 + BYTES_WRITTEN % SHMSIZE;
int size_available = SHMSIZE - (BYTES_WRITTEN - BYTES_READ);
if ((read_result = read (filefd, buf, size_available)) == -1)
return err_printf ("err while read() from file\n");
if ((size_available != 0) && (read_result == 0))
break;
BYTES_WRITTEN += read_result;
SEMPUSH (SHMRD, 1, 0);
SEMPUSH (SHMRD, -1, SEM_UNDO);
SEMPUSH (SHMRD, -1, IPC_NOWAIT);
SEMPUSH (SHMWR, 1, 0);
SEMPUSH (SHMWR, -1, SEM_UNDO);
SEMOP();
}
SEMPUSH (SHMRD, -1, 0);
SEMPUSH (SEMPROD, 1, SEM_UNDO);
SEMOP(); // producer stopped use of shmem => SEMPROD == 3
SEMPUSH (SEMCONS, 0, 0);
SEMOP(); // waits till consumer ends or dies
return 0;
}
int consumer ()
{
int counter = 0;
SEMPUSH (SEMPROD, -1, 0);
SEMPUSH (SEMPROD, 0, 0);
SEMPUSH (SEMPROD, 2, 0);
SEMPUSH (SEMCONS, 0, 0);
SEMPUSH (SEMCONS, 1, SEM_UNDO);
SEMPUSH (SHMRD, 1, SEM_UNDO);
SEMOP(); // if there is free producer => consumer enters
int write_result = -1;
while (1)
{
if ((BYTES_WRITTEN == BYTES_READ) && (SEMCHECK (SEMPROD, 3) == 0))
return 0;
SEMPUSH (SEMPROD, -2, IPC_NOWAIT);
SEMPUSH (SEMPROD, 2, 0);
SEMPUSH (SHMRD, 0, 0);
SEMPUSH (SHMRD, 1, SEM_UNDO);
SEMPUSH (SHMWR, 1, SEM_UNDO);
SEMPUSH (SHMWR, -1, 0);
SEMOP();
void* buf = shmaddr + 8 + BYTES_READ % SHMSIZE;
int size_available = BYTES_WRITTEN - BYTES_READ;
if ((write_result = write (STDOUT_FILENO, buf, size_available)) == -1)
return err_printf ("err while write() to stdout\n");
BYTES_READ += write_result;
SEMPUSH (SHMWR, 1, 0);
SEMPUSH (SHMWR, -1, SEM_UNDO);
SEMPUSH (SHMWR, -1, IPC_NOWAIT);
SEMPUSH (SHMRD, 1, 0);
SEMPUSH (SHMRD, -1, SEM_UNDO);
SEMOP();
}
}
#undef BYTES_WRITTEN
#undef BYTES_READ
#undef SHMRD
#undef SHMWR
#undef SEMOP
#undef SEMOP_NORET
#undef SEMPUSH
#undef SEMCHECK
#undef SHMCHECK
#ifdef __GNUC__
int err_printf (char *format, ...) __attribute__ ((format(printf, 1, 2)));
#endif /*__GNUC__*/
int err_printf (char* format, ...)
{
int err_tmp = errno;
(shmid == -1) || shmctl (shmid, IPC_RMID, NULL);
va_list ap;
va_start (ap, format);
vfprintf (stderr, format, ap);
va_end (ap);
fprintf(stderr, "\t");
errno = err_tmp;
perror (NULL);
return -1;
}
#undef SEMKEY
#undef SEMNUM
#undef SHMKEY
#