在这里我记录一下process.c文件,别忘了如果在printk里面写了fprintk,需要再在kernel.h头文件里加上fprintk的函数原型
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <sys/times.h>
#include <sys/wait.h>
#define HZ 100
void cpuio_bound(int last, int cpu_time, int io_time);
int main()
{
int pid;
int process_num[10];
int i = 0;
for (i = 0; i < 10; i++)
{
if (!(process_num[i] = fork()))
{
cpuio_bound(20,2*i,20-2*i);
return 0;/*return 0的妙用,直接结束子进程,就不用考虑子进程又开孙进程*/
}
else
{
printf("new_pid=%d\n", process_num[i]);/*创建进程*/
}
}
for (i = 0; i < 10; i++)
{
pid = wait(NULL);
printf("quit_pid=%d\n", pid);
}
}
/*
* 此函数按照参数占用CPU和I/O时间
* last: 函数实际占用CPU和I/O的总时间,不含在就绪队列中的时间,>=0是必须的
* cpu_time: 一次连续占用CPU的时间,>=0是必须的
* io_time: 一次I/O消耗的时间,>=0是必须的
* 如果last > cpu_time + io_time,则往复多次占用CPU和I/O
* 所有时间的单位为秒
*/
void cpuio_bound(int last, int cpu_time, int io_time)
{
struct tms start_time, current_time;
clock_t utime, stime;
int sleep_time;
while (last > 0)
{
/* CPU Burst */
times(&start_time);/*我猜测是获取当前时间*/
/* 其实只有t.tms_utime才是真正的CPU时间。但我们是在模拟一个
* 只在用户状态运行的CPU大户,就像“for(;;);”。所以把t.tms_stime
* 加上很合理。*/
do
{
times(¤t_time);
utime = current_time.tms_utime - start_time.tms_utime;
stime = current_time.tms_stime - start_time.tms_stime;
} while ( ( (utime + stime) / HZ ) < cpu_time );
last -= cpu_time;
if (last <= 0 )
break;
/* IO Burst */
/* 用sleep(1)模拟1秒钟的I/O操作 */
sleep_time=0;
while (sleep_time < io_time)
{
sleep(1);
sleep_time++;
}
last -= sleep_time;
}
}
个人想法:这个sleep(1)就像io一样,如果该进程时间片用完了,恰好运行到sleep(1),那么又回到该进程时sleep时间就会减少(不需要等那么长时间了),提高cpu利用率—>说明io不需要cpu