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
|
/*
* Copyright (c) 2000-2002 Sendmail, Inc. and its suppliers.
* All rights reserved.
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the sendmail distribution.
*/
#include <sm/gen.h>
SM_RCSID("@(#)$Id: t-shm.c,v 1.18 2002/01/31 04:11:41 ca Exp $")
#include <stdio.h>
#if SM_CONF_SHM
# include <stdlib.h>
# include <unistd.h>
# include <sys/wait.h>
# include <sm/heap.h>
# include <sm/string.h>
# include <sm/test.h>
# include <sm/shm.h>
# define SHMSIZE 1024
# define SHM_MAX 6400000
# define T_SHMKEY 21
/*
** SHMINTER -- interactive testing of shared memory
**
** Parameters:
** owner -- create segment.
**
** Returns:
** 0 on success
** < 0 on failure.
*/
int shminter __P((bool));
int
shminter(owner)
bool owner;
{
int *shm, shmid;
int i, j, t;
shm = (int *) sm_shmstart(T_SHMKEY, SHMSIZE, 0, &shmid, owner);
if (shm == (int *) 0)
{
perror("shminit failed");
return -1;
}
while ((t = getchar()) != EOF)
{
switch (t)
{
case 'c':
*shm = 0;
break;
case 'i':
++*shm;
break;
case 'd':
--*shm;
break;
case 's':
sleep(1);
break;
case 'l':
t = *shm;
for (i = 0; i < SHM_MAX; i++)
{
j += i;
++*shm;
}
if (*shm != SHM_MAX + t)
fprintf(stderr, "error: %d != %d\n",
*shm, SHM_MAX + t);
break;
case 'v':
printf("shmval: %d\n", *shm);
break;
}
}
return sm_shmstop((void *) shm, shmid, owner);
}
/*
** SHMBIG -- testing of shared memory
**
** Parameters:
** owner -- create segment.
** size -- size of segment.
**
** Returns:
** 0 on success
** < 0 on failure.
*/
int shmbig __P((bool, int));
int
shmbig(owner, size)
bool owner;
int size;
{
int *shm, shmid;
int i;
shm = (int *) sm_shmstart(T_SHMKEY, size, 0, &shmid, owner);
if (shm == (int *) 0)
{
perror("shminit failed");
return -1;
}
for (i = 0; i < size / sizeof(int); i++)
shm[i] = i;
for (i = 0; i < size / sizeof(int); i++)
{
if (shm[i] != i)
{
fprintf(stderr, "failed at %d: %d", i, shm[i]);
}
}
return sm_shmstop((void *) shm, shmid, owner);
}
/*
** SHMTEST -- test of shared memory
**
** Parameters:
** owner -- create segment.
**
** Returns:
** 0 on success
** < 0 on failure.
*/
# define MAX_CNT 10
int
shmtest(owner)
int owner;
{
int *shm, shmid;
int cnt = 0;
shm = (int *) sm_shmstart(T_SHMKEY, SHMSIZE, 0, &shmid, owner);
if (shm == (int *) 0)
{
perror("shminit failed");
return -1;
}
if (owner)
{
int r;
*shm = 1;
while (*shm == 1 && cnt++ < MAX_CNT)
sleep(1);
SM_TEST(cnt <= MAX_CNT);
/* release and re-acquire the segment */
r = sm_shmstop((void *) shm, shmid, owner);
SM_TEST(r == 0);
shm = (int *) sm_shmstart(T_SHMKEY, SHMSIZE, 0, &shmid, owner);
SM_TEST(shm != (int *) 0);
}
else
{
while (*shm != 1 && cnt++ < MAX_CNT)
sleep(1);
SM_TEST(cnt <= MAX_CNT);
*shm = 2;
/* wait a momemt so the segment is still in use */
sleep(2);
}
return sm_shmstop((void *) shm, shmid, owner);
}
int
main(argc, argv)
int argc;
char *argv[];
{
bool interactive = false;
bool owner = false;
int big = -1;
int ch;
int r = 0;
int status;
extern char *optarg;
# define OPTIONS "b:io"
while ((ch = getopt(argc, argv, OPTIONS)) != -1)
{
switch ((char) ch)
{
case 'b':
big = atoi(optarg);
break;
case 'i':
interactive = true;
break;
case 'o':
owner = true;
break;
default:
break;
}
}
if (interactive)
r = shminter(owner);
else if (big > 0)
r = shmbig(true, big);
else
{
pid_t pid;
extern int SmTestNumErrors;
if ((pid = fork()) < 0)
{
perror("fork failed\n");
return -1;
}
sm_test_begin(argc, argv, "test shared memory");
if (pid == 0)
{
/* give the parent the chance to setup data */
sleep(1);
r = shmtest(false);
}
else
{
r = shmtest(true);
(void) wait(&status);
}
SM_TEST(r == 0);
if (SmTestNumErrors > 0)
printf("add -DSM_CONF_SHM=0 to confENVDEF in devtools/Site/site.config.m4\nand start over.\n");
return sm_test_end();
}
return r;
}
#else /* SM_CONF_SHM */
int
main(argc, argv)
int argc;
char *argv[];
{
printf("No support for shared memory configured on this machine\n");
return 0;
}
#endif /* SM_CONF_SHM */
|