-
Notifications
You must be signed in to change notification settings - Fork 5
/
exechack.cc
69 lines (59 loc) · 986 Bytes
/
exechack.cc
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
#include "types.h"
#include "user.h"
#include "mtrace.h"
#include "amd64.h"
#include <stdio.h>
#include <unistd.h>
#define NITERS 100
#define PROCMAX NCPU
#define PROCMIN (NCPU-4)
static void
worker0(void)
{
const char* av[] = { "exechack", "w", 0 };
execv(av[0], const_cast<char * const *>(av));
die("worker exec");
}
static void
worker1(void)
{
exit(0);
}
static void
master(void)
{
u64 pcount = 0;
u64 i = 0;
u64 t0 = rdtsc();
while (i < NITERS) {
while (pcount < PROCMAX) {
int pid;
pid = fork();
if (pid < 0)
die("master fork");
if (pid == 0)
worker0();
pcount++;
}
while (pcount > PROCMIN) {
if (wait(NULL) < 0)
die("master wait");
pcount--;
i++;
}
}
while (pcount) {
wait(NULL);
pcount--;
}
u64 t1 = rdtsc();
printf("%lu\n", (t1-t0)/i);
}
int
main(int ac, char **av)
{
if (ac > 1 && av[1][0] == 'w')
worker1();
master();
return 0;
}