diff options
Diffstat (limited to 'files/process.c')
-rw-r--r-- | files/process.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/files/process.c b/files/process.c new file mode 100644 index 0000000..46eb0b0 --- /dev/null +++ b/files/process.c | |||
@@ -0,0 +1,58 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <unistd.h> | ||
3 | #include <time.h> | ||
4 | #include <sys/times.h> | ||
5 | |||
6 | #define HZ 100 | ||
7 | |||
8 | void cpuio_bound(int last, int cpu_time, int io_time); | ||
9 | |||
10 | int main(int argc, char * argv[]) | ||
11 | { | ||
12 | return 0; | ||
13 | } | ||
14 | |||
15 | /* | ||
16 | * 此函数按照参数占用CPU和I/O时间 | ||
17 | * last: 函数实际占用CPU和I/O的总时间,不含在就绪队列中的时间,>=0是必须的 | ||
18 | * cpu_time: 一次连续占用CPU的时间,>=0是必须的 | ||
19 | * io_time: 一次I/O消耗的时间,>=0是必须的 | ||
20 | * 如果last > cpu_time + io_time,则往复多次占用CPU和I/O | ||
21 | * 所有时间的单位为秒 | ||
22 | */ | ||
23 | void cpuio_bound(int last, int cpu_time, int io_time) | ||
24 | { | ||
25 | struct tms start_time, current_time; | ||
26 | clock_t utime, stime; | ||
27 | int sleep_time; | ||
28 | |||
29 | while (last > 0) | ||
30 | { | ||
31 | /* CPU Burst */ | ||
32 | times(&start_time); | ||
33 | /* 其实只有t.tms_utime才是真正的CPU时间。但我们是在模拟一个 | ||
34 | * 只在用户状态运行的CPU大户,就像“for(;;);”。所以把t.tms_stime | ||
35 | * 加上很合理。*/ | ||
36 | do | ||
37 | { | ||
38 | times(¤t_time); | ||
39 | utime = current_time.tms_utime - start_time.tms_utime; | ||
40 | stime = current_time.tms_stime - start_time.tms_stime; | ||
41 | } while ( ( (utime + stime) / HZ ) < cpu_time ); | ||
42 | last -= cpu_time; | ||
43 | |||
44 | if (last <= 0 ) | ||
45 | break; | ||
46 | |||
47 | /* IO Burst */ | ||
48 | /* 用sleep(1)模拟1秒钟的I/O操作 */ | ||
49 | sleep_time=0; | ||
50 | while (sleep_time < io_time) | ||
51 | { | ||
52 | sleep(1); | ||
53 | sleep_time++; | ||
54 | } | ||
55 | last -= sleep_time; | ||
56 | } | ||
57 | } | ||
58 | |||