-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusage.c
More file actions
95 lines (83 loc) · 1.82 KB
/
usage.c
File metadata and controls
95 lines (83 loc) · 1.82 KB
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
#include "types.h"
#include "stat.h"
#include "user.h"
#include "pstat.h"
#include "param.h"
// print usage info of all procresses
void
printinfo()
{
struct pstat st;
int r = getpinfo(&st);
if (r != 0) {
exit();
}
int i;
for(i = 1; i < NPROC; i++) {
if (st.inuse[i]) {
printf(1, "pid: %d hticks: %d lticks: %d\n", st.pid[i], st.hticks[i], st.lticks[i]);
}
}
}
// spin for a while
int
spin() {
int i, j;
for (i = 0; i < 10000000; i++) {
j = i % 11;
}
return j;
}
int
main(int argc, char *argv[])
{
if (argc < 3) {
printf(1, "Usage: usage ticks tickets1 [tickets2]...\n\n");
printf(1, "Spawns subprocesses, each of which will run for \n"
"approximately the given number of ticks. For each ticket\n"
"ammount given, a child process will spawn and request that\n"
"number of tickets.\n");
exit();
}
int total = 0;
int i;
for (i = 0; i < argc - 2; i++) {
total += atoi(argv[i+2]);
}
int ret = settickets(total);
if (ret < 0) {
printf(1, "settickets failed\n");
exit();
}
// pids of children
int pids[NPROC];
// spawn children
for (i = 0; i < argc - 2; i++) {
pids[i] = fork();
if (pids[i] == 0) {
int ret = settickets(atoi(argv[i + 2]));
if (ret < 0) {
printf(1, "settickets failed\n");
exit();
}
// spin so we get scheduled
while(1) {
spin();
}
}
}
// print usage info
int ticks = atoi(argv[1]);
int start = uptime();
while(uptime() < start + ticks) {
sleep(100);
printf(1, "time: %d\n", uptime() - start);
printinfo();
}
// kill children
for (i = 0; i < argc - 2; i++) {
kill(pids[i]);
wait();
}
exit();
}