-
Notifications
You must be signed in to change notification settings - Fork 1
/
ezipc.h
414 lines (340 loc) · 8.72 KB
/
ezipc.h
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/* EZIPC.h */
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#define IPC_MAX 20
/* NOTICE: programs compiled using different values of IPC_MAX are not
fully compatable, since IPC_MAX is used in calculating the key
used to request the semaphores and shared memory. */
/* GLOBAL DEFINITIONS */
int semid;
int IPC_KEY;
//#define IPC_KEY getuid()
#define SEM_COUNTING 0
#define SEM_CNT 0
#define SEM_BINARY 1
#define SEM_BIN 1
int EZIPC_SEM_TRANS(int sid);
int EZIPC_SHM_TRANS(int mid);
int EZIPC_ERROR(char *string);
int SHOW(int sid);
int EZIPC_SEM_CALL(int sid,int op);
void *EZIPC_SHM_ADDR(int mid);
void P(int sid);
void V(int sid);
int EZIPC_SEM_MAKE(int sid,int numsems);
void EZIPC_SHM_MAKE(int mid,int size);
void EZIPC_SEM_REMOVE();
void EZIPC_SHM_REMOVE();
int EZIPC_SHM_DET( char *addr);
void SETUP();
void SETUP_KEY(int key);
int SEMAPHORE(int type, int value);
void *SHARED_MEMORY(int size);
int COBEGIN(int X);
void COEND(int X);
/*
SEM_TRANS converts the semaphore id to the form used by the ipc library.
*/
int EZIPC_SEM_TRANS(int sid)
{
int ipcid;
ipcid=semget(((IPC_KEY*IPC_MAX)+sid),1,0666|IPC_CREAT);
return(ipcid);
}
/*
SHM_TRANS converts the memory id to the form used by the ipc library.
*/
int EZIPC_SHM_TRANS(int mid)
{
int ipcid;
ipcid=shmget(((IPC_KEY*IPC_MAX)+mid),1,0666|IPC_CREAT);
return(ipcid);
}
/*
ERROR reports errors and exit
*/
int EZIPC_ERROR(char *string)
{
printf("\nEZIPC Error Encountered:\n");
perror(string);
exit(0);
}
/*
SHOW returns the current value of a semaphore, this is really just for debugging
purposes.
*/
int SHOW(int sid)
{
int value;
value=semctl(EZIPC_SEM_TRANS(semid),sid,GETVAL,0);
return (value);
}
/*
SEM_CALL performs a designated operation on a semaphore in the
block choosen by SEM_TRANS.
sid is the id of the semaphore to be operated on.
op is a number to be added to the current value of
the semaphore.
*/
int EZIPC_SEM_CALL(int sid,int op)
{
int x;
struct sembuf sb;
sb.sem_num = sid;
sb.sem_op = op;
sb.sem_flg = 0;
/* printf("sem_call: sid:%d val:%d op:%d\n",sid,SHOW(sid),op); */
if ( (x=(semop(EZIPC_SEM_TRANS(semid),&sb,1))) ==-1 )
{ printf("TRACE SEM_CALL ERROR\n");
EZIPC_ERROR("SEM_CALL: Semaphore ID Error");
}
return(x);
}
/*
SHM_ADDR attachs and returns a pointer to the shared memory block mid.
*/
void *EZIPC_SHM_ADDR(int mid)
{
char *addr;
// extern char *shmat();
addr=shmat(EZIPC_SHM_TRANS(mid),0,0);
if ( (uintptr_t)addr == -1 )
{
EZIPC_ERROR("EZIPC_SHM_ADDR Error:");
}
return (addr);
}
/*
P and V follow the text-book standard for the P and V operations.
sid is the number of the semaphore indicated by SEM_TRANS.
*/
void P(int sid)
{
EZIPC_SEM_CALL(sid,-1);
}
void V(int sid)
{
char *addr;
EZIPC_SEM_CALL(0,-1);
addr = EZIPC_SHM_ADDR(0);
if ( ((*(addr+1+sid))==SEM_BIN) && (SHOW(sid)==1))
{
/* do not increment binary semaphore past 1 */
}
else
{
EZIPC_SEM_CALL(sid,1);
}
EZIPC_SEM_CALL(0,1);
EZIPC_SHM_DET(addr);
}
/*
SEM_MAKE creates a semaphore and releases it for use.
*/
int EZIPC_SEM_MAKE(int sid,int numsems)
{
int i;
i=0;
if((semid=semget(((IPC_KEY*IPC_MAX)+i),IPC_MAX,0666|IPC_CREAT)==-1 ))
EZIPC_ERROR("SEM_MAKE: Semaphore Creation Error");
/* printf("SEM_MAKE semid %d\n",semid); */
return(semid);
}
/*
SHM_MAKE creates a shared memory block and releases it for use.
*/
void EZIPC_SHM_MAKE(int mid,int size)
{
if(shmget(((IPC_KEY*IPC_MAX)+mid),size,0777|IPC_CREAT)==-1)
EZIPC_ERROR("Shared Memory Creation Error");
}
/*
EZIPC_SEM_REMOVE removes all the semaphores.
*/
void EZIPC_SEM_REMOVE()
{
int x;
for(x=0; x<=IPC_MAX; x++)
semctl(EZIPC_SEM_TRANS(semid),x,IPC_RMID);
}
/*
SHM_REMOVE removes all the blocks of shared memory.
*/
void EZIPC_SHM_REMOVE()
{
int x;
for(x=0;x<=IPC_MAX; x++)
shmctl(EZIPC_SHM_TRANS(x),IPC_RMID,0);
}
/*
EZIPC_SHM_DET detach a shared memory segment
SV has a default limit of 6 attached segments
*/
int EZIPC_SHM_DET( char *addr)
{
shmdt( addr);
return(0);
}
/*
SETUP should be the very first thing executed in your program.
it will set up all block of shared memory that the EZIPC library
uses to keep track of all the requests for shared memory and semaphores
It also begins by forking off a process to execute the program while
the parent waits for the program to terminate so that it can remove all
the IPC structures, which otherwise will not be erased.
*/
void SETUP()
{
char *Maint_Block;
int Child;
IPC_KEY = getuid();
EZIPC_SHM_MAKE(0,2+IPC_MAX);
Maint_Block=EZIPC_SHM_ADDR(0);
*Maint_Block=1;
*(Maint_Block+1)=1;
semid = EZIPC_SEM_MAKE(0,1);
EZIPC_SEM_CALL(semid,1);
if((Child=fork())!=0)
{
wait(&Child);
EZIPC_SEM_REMOVE();
EZIPC_SHM_REMOVE();
exit(0);
}
EZIPC_SHM_DET(Maint_Block);
/* else continue running the program */
}
void SETUP_KEY(int key)
{
char *Maint_Block;
int Child;
IPC_KEY = key;
EZIPC_SHM_MAKE(0,2+IPC_MAX);
Maint_Block=EZIPC_SHM_ADDR(0);
*Maint_Block=1;
*(Maint_Block+1)=1;
semid = EZIPC_SEM_MAKE(0,1);
EZIPC_SEM_CALL(semid,1);
if((Child=fork())!=0)
{
wait(&Child);
EZIPC_SEM_REMOVE();
EZIPC_SHM_REMOVE();
exit(0);
}
EZIPC_SHM_DET(Maint_Block);
/* else continue running the program */
}
/*
SEMAPHORE creates a new semaphore and returns it's sid , it also sets the initial
value and the type of semaphore to be used, the types can be SEM_BIN, SEM_CNT,
SEM_BINARY, SEM_COUNTING. the value of a Binary Semaphore will not get above 1.
*/
int SEMAPHORE(int type, int value)
{
int sid, x;
union sem_num {
int val;
struct semid_ds *buf;
ushort *array;
} semctl_arg;
char *Maint_Block;
EZIPC_SEM_CALL(0,-1);
Maint_Block=EZIPC_SHM_ADDR(0);
if(*Maint_Block < IPC_MAX)
{
sid=(*Maint_Block)++;
EZIPC_SEM_CALL(0,1);
*(Maint_Block+1+sid)=type;
if ( (type == SEM_BIN) && ( value > 1 || value < 0 ) )
EZIPC_ERROR("SEMAPHORE: Binary semaphore not initialized to 1 or 0");
if (value < 0 )
EZIPC_ERROR("SEMAPHORE:Semaphore initialized to negative value");
semctl_arg.val = value;
semctl(EZIPC_SEM_TRANS(semid),sid,SETVAL,semctl_arg);
/* EZIPC_SEM_CALL(sid,value); */
}
else
{
EZIPC_SEM_CALL(0,1);
EZIPC_ERROR("SEMAPHORE: Too Many Semaphores Requested");
}
EZIPC_SHM_DET(Maint_Block);
return(sid);
}
/*
SHARED_MEMORY creates a block of shared memory, and returns a pointer to the
block of memory.
*/
void *SHARED_MEMORY(int size)
{
int mid;
void *addr;
char *Maint_Block;
EZIPC_SEM_CALL(0,-1);
Maint_Block=EZIPC_SHM_ADDR(0);
if(*(Maint_Block+1) < IPC_MAX)
{
mid=(*(Maint_Block+1))++;
EZIPC_SEM_CALL(0,1);
EZIPC_SHM_MAKE(mid,size);
}
else
{
EZIPC_SEM_CALL(0,1);
EZIPC_ERROR("Too Many Shared Memory Blocks Requested");
}
EZIPC_SHM_DET(Maint_Block);
addr=EZIPC_SHM_ADDR(mid);
return(addr);
}
/*
EZIPC was written by Chris Argenta for Dr. Danial A. Canas
at The Math and Computer Science Department of
Wake Forest University, Winston-Salem, NC, 27109
E-mail concerning EZIPC can be sent to [email protected] or
EZIPC was created to help simplify the use of semaphores and shared memory
on the System V UNIX operating system, especially for teaching purposes.
EZIPC may be used, modified, and distributed freely.
*/
/* COBEGIN/COEND construct
COBEGIN spawns "X" number of processes. the returned value for each process
will be it's relative number, the parent being 0, the children going from
1 to "X". */
int COBEGIN(int X)
{
int i;
for (i=1;i<=X;i++)
if(fork()==0) /* creates a new process */
break;
if (i>X)
i=0; /* assigns parent 0 */
return(i); /* returns relative number */
}
/*
COEND requires that the process's relative number (see COBEGIN) be sent.
it will kill and children processes that finish and suspend execution
for the parent until all the children have been killed (either from the coend
or some other by some other means). */
void COEND(int X)
{
int signal;
if(X==0)
while (wait(&signal) != -1); /*parent waits for all children*/
else
exit(0); /* children are killed */
}
/***************************************************************************
This source code was designed and written by Chris Argenta and Ed Mallozzi
for the purpose of simplifing semaphores and concurrency on a System V UNIX
based operating system. Any questions or comments should be addressed through
email to '[email protected]'. MAY 1, 1991
***************************************************************************/