-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplexdec.cpp
330 lines (281 loc) · 7.34 KB
/
complexdec.cpp
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
/*
* Copyright (C) 1999,2001,2010 by Manfred Spraul.
*
* Redistribution of this file is permitted under the terms of the GNU
* General Public License (GPL)
* $Header: /home/manfred/cvs-tree/manfred/ipcsem/undotest.c,v 1.2 2003/06/28 15:19:43 manfred Exp $
*/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <wait.h>
union semun {
int val;
struct semid_ds *buf;
unsigned short int *array;
struct seminfo* __buf;
};
int getval(const char *str, int id)
{
union semun arg;
int res;
res = semctl(id,0,GETVAL,arg);
if(res==-1) {
printf("GETVAL failed for %s.\n", str);
exit(4);
}
printf(" %s: GETVAL now %d.\n",str, res);
return res;
}
void setval(int id, int val)
{
union semun arg;
int res;
arg.val = val;
res = semctl(id,0,SETVAL,arg);
if(res==-1) {
printf("SETVAL failed, errno %d.\n", errno);
exit(4);
}
printf(" SETVAL succeeded.\n");
}
long int print_ctime(const char *str, int id)
{
union semun arg;
struct semid_ds info;
time_t cur = time(NULL);
int res;
arg.buf = &info;
res = semctl(id,0,IPC_STAT,arg);
if(res==-1) {
printf("semctl(IPC_STAT) failed (%d).\n",errno);
exit(5);
}
printf("%s: ctime now %ld (%ld).\n", str,
(long int) info.sem_ctime, (long int) cur - info.sem_ctime);
return info.sem_ctime;
}
#define CHILD_COUNT 5
#define EXIT_SUCCESS_MAGIC 0x42
/* test1: check cpu consumption for complex sleeps */
void test1()
{
int childs[CHILD_COUNT];
unsigned long long total_sys, total_user;
int res;
unsigned int i;
int id;
printf(" ****************************************\n");
printf(" test1: do a non-trivial sleep\n");
/* create array */
res = semget(IPC_PRIVATE, 1, 0700 | IPC_CREAT);
printf(" got semaphore array %xh.\n",res);
if(res == -1) {
printf(" create failed.\n");
return;
}
id = res;
setval(id, 3);
/* create sub-process */
for (i=0;i<CHILD_COUNT;i++) {
res = fork();
if (res < 0) {
printf("Fork failed (errno=%d). Aborting.\n", errno);
res = semctl(id,1,IPC_RMID,NULL);
exit(1);
}
fflush(stdout);
if (!res) {
struct sembuf sop[15];
/* child: */
/* Do an "increase by 15, decrease by 30" in step size 3*/
for (i=0;i<sizeof(sop)/sizeof(sop[0]);i++) {
sop[i].sem_num=0;
if (i<sizeof(sop)/sizeof(sop[0])/3)
sop[i].sem_op=3;
else
sop[i].sem_op=-3;
sop[i].sem_flg=0;
}
errno = 0;
res = semop(id,sop,sizeof(sop)/sizeof(sop[0]));
if(res!=-1 || errno !=EIDRM) {
printf("child: semop returned %d, errno %d, expected %d/%d.\n", res, errno, -1, EIDRM);
exit(1);
}
fflush(stdout);
exit(EXIT_SUCCESS_MAGIC);
}
childs[i] = res;
}
sleep(5);
errno = 0;
res = semctl(id,1,IPC_RMID,NULL);
printf(" IPC_RMID returned %d, errno now %d (expected: 0, 0).\n", res, errno);
total_user = 0;
total_sys = 0;
for (i=0;i<CHILD_COUNT;i++) {
int retval;
struct rusage r;
retval = wait4(childs[i], &res, 0, &r);
if (retval != childs[i]) {
printf("wait4 returned unexpeted value %d (expected %d), errno now %d.\n",
retval, childs[i], errno);
}
if (WIFEXITED(res) == 0 || WEXITSTATUS(res) != EXIT_SUCCESS_MAGIC) {
printf("unexpected child exit code %d (raw 0x%x. Expected: %d).\n",
WEXITSTATUS(res), res, EXIT_SUCCESS_MAGIC);
}
printf(" Child %d(%d): sys %ld.%06ld, user %ld.%06ld.\n",
i, childs[i],
(long)r.ru_utime.tv_sec, (long)r.ru_utime.tv_usec,
(long)r.ru_stime.tv_sec, (long)r.ru_stime.tv_usec);
total_user += r.ru_utime.tv_sec*1000000;
total_user += r.ru_utime.tv_usec;
total_sys += r.ru_stime.tv_sec*1000000;
total_sys += r.ru_stime.tv_usec;
fflush(stdout);
}
printf(" Total time: sys %ld.%06ld, user %ld.%06ld.\n",
(long)total_sys/1000000, (long)total_sys,
(long)total_user/1000000, (long)total_user);
if (total_user < 100000 && total_sys < 100000) {
printf("+++ test1 success: No live-lock\n");
fflush(stdout);
} else {
printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n");
printf("test 1 bad: Probably a live-lock.\n");
fflush(stdout);
}
}
/* test2: test sem_ctime updates */
void test2(int id)
{
unsigned long ctime_before, ctime_after;
printf(" ****************************************\n");
printf(" test2: does SETVAL update sem_ctime?\n");
sleep(3);
ctime_before = print_ctime(" before SETVAL", id);
setval(id, 2);
ctime_after = print_ctime(" before SETVAL", id);
if (ctime_after != ctime_before)
printf(" INFO: OS updates sem_ctime in semctl(SETVAL)\n");
else
printf(" INFO: OS performs no update to sem_ctime in semctl(SETVAL)\n");
}
/* test3: test wait for non-zero */
void test3(int id)
{
int res;
printf(" ****************************************\n");
printf(" test3: check that kernel wakes up child that waits for non-zero value.\n");
setval(id, 2);
/* create sub-process */
res = fork();
if (res < 0) {
printf("Fork failed (errno=%d). Aborting.\n", errno);
res = semctl(id,1,IPC_RMID,NULL);
exit(1);
}
fflush(stdout);
if (!res) {
struct sembuf sop[3];
/* child: */
/* Do an "increase by 1 if value is 1" operation */
sop[0].sem_num=0;
sop[0].sem_op=-1;
sop[0].sem_flg=0;
sop[1].sem_num=0;
sop[1].sem_op=0;
sop[1].sem_flg=0;
sop[2].sem_num=0;
sop[2].sem_op=2;
sop[2].sem_flg=0;
res = semop(id,sop,3);
if(res==-1) {
printf("child: semop failed.\n");
exit(1);
}
res = getval("child completed, before exit", id);
if (res != 2) {
printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n");
printf("Bad: got unexpected value.\n");
exit(99);
}
fflush(stdout);
exit(EXIT_SUCCESS_MAGIC);
} else {
struct sembuf sop[1];
int retval;
int childid;
childid = res;
sleep(1);
/* Do an "decrease by 1" operation */
sop[0].sem_num=0;
sop[0].sem_op=-1;
sop[0].sem_flg=0;
res = semop(id,sop,1);
if(res==-1) {
printf("parent: semop failed.\n");
exit(1);
}
sleep(1);
res = getval("parent: after sleep", id);
if (res != 2) {
printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n");
printf("Test 3 bad: got unexpected value (child not woken up?).\n");
return;
}
retval = waitpid(childid, &res, 1);
if (retval != childid) {
printf("waitpid returned unexpeted value %d (expected %d), errno now %d.\n",
retval, childid, errno);
}
if (WIFEXITED(res) == 0 || WEXITSTATUS(res) != EXIT_SUCCESS_MAGIC) {
printf("unexpected child exit code %d (raw 0x%x. Expected: %d).\n",
WEXITSTATUS(res), res, EXIT_SUCCESS_MAGIC);
}
res = getval("parent: after waitpid", id);
if (res != 2) {
printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n");
printf("Bad: got unexpected value.\n");
return;
}
}
printf(" +++ test3 success: kernel wakes up child correctly.\n");
}
int main(int argc,char** argv)
{
int res;
int id;
/* test 1 (array created/destroyed in test1()) */
test1();
/* test 2 */
/* create array */
res = semget(IPC_PRIVATE, 1, 0700 | IPC_CREAT);
printf(" got semaphore array %xh.\n",res);
if(res == -1) {
printf(" create failed.\n");
return 1;
}
id = res;
test2(id);
test3(id);
printf(" ****************************************\n");
printf(" Done, cleanup.\n");
errno = 0;
res = semctl(id,1,IPC_RMID,NULL);
printf(" IPC_RMID returned %d, errno now %d (expected: 0, 0).\n", res, errno);
return 0;
}