进程运行轨迹的跟踪与统计

在这里我记录一下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(&current_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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值