-
Notifications
You must be signed in to change notification settings - Fork 0
/
sanity.c
107 lines (96 loc) · 2.73 KB
/
sanity.c
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
#include "types.h"
#include "stat.h"
#include "user.h"
// sanity.c : gets a nubmer n as an argument and then it will fork 3n times
// and wait until all of them finish for each child process that ends print
// its satistics
enum test_type {CPU = 0, S_CPU = 1 , IO = 2};
const char * get_test_name(enum test_type test){
return (test == CPU) ? "CPU" :
(test == S_CPU) ? "S-CPU" :
"IO" ;
}
void statistics(enum test_type test, int n, int sleeptime,
const char * test_group){
printf(1, "Average %s for %s is %d \n",test_group,
get_test_name(test), sleeptime/n);
}
void sleeptime(enum test_type test, int n, int info) {
statistics(test, n, info, "sleeptime");
}
void readytime(enum test_type test, int n, int info) {
statistics(test, n, info, "readytime");
}
void turnaroundtime(enum test_type test, int n, int info) {
statistics(test, n, info, "turn around time");
}
int
main(int argc, char *argv[]){
int n, pid, i , j, h;
int stime_cpu = 0,stime_scpu = 0, stime_IO = 0;
int retime_cpu = 0, retime_scpu = 0, retime_IO = 0;
int tatime_cpu = 0, tatime_scpu = 0, tatime_IO = 0;
int retime, rutime, stime;
enum test_type test;
if (argc != 2)
return -1;
n = atoi(argv[1]);
for(i = 0 ; i < 3 * n ; ++i) {
pid = fork();
if(pid == 0) {
test = getpid() % 3;
if(test == CPU) {
for(j = 0 ; j < 100 ; ++j)
for(h = 0 ; h < 1000000 ; ++h){}
}
else if(test == S_CPU) {
for(j = 0 ; j < 100 ; ++j) {
for(h = 0 ; h < 1000000 ; ++h){}
yield();
}
}
else { //IO
for(j = 0 ; j < 100 ; ++j)
sleep(1);
}
exit();
}
else if (pid < 0) {
printf(1, "fork number %d, failed!!! \n", i);
}
else{}
}
while((pid =wait2(&retime, &rutime, &stime)) > 0) { // pid != 0 (parent code)
test = pid % 3;
printf(1, "process id: %d, type: %s \n", pid, get_test_name(test));
printf(1,"wait time: %d, run time: %d, IO time: %d \n",
retime, rutime, stime);
switch(test) {
case CPU:
stime_cpu += stime;
retime_cpu += retime;
tatime_cpu += stime + retime + rutime;
break;
case S_CPU:
stime_scpu += stime;
retime_scpu += retime;
tatime_scpu += stime + retime + rutime;
break;
case IO:
stime_IO += stime;
retime_IO += retime;
tatime_IO += stime + retime + rutime;
break;
}
}
sleeptime(CPU,n,stime_cpu);
sleeptime(S_CPU,n,stime_scpu);
sleeptime(IO,n,stime_IO);
readytime(CPU,n,retime_cpu);
readytime(S_CPU,n,retime_scpu);
readytime(IO,n,retime_IO);
turnaroundtime(CPU,n,tatime_cpu);
turnaroundtime(S_CPU,n,tatime_scpu);
turnaroundtime(IO,n,tatime_IO);
exit();
}