1、tar -zxf 、解压后如下 、拷贝linux,命名为 -r linux 、移动到根目录,替换掉.config 、进入目录,配置和编译内核模块 make oldconfig make dep make clean make bzImage make modules 内核安装与测试 安装内核映像文件 cp arch/i386/boot/bzImage /boot/ 拷贝和安装Linux系统映射文件,并创建其与系统映射文件之间的符号链接执行命令make modules_install 以安装可动态加载的内核模块 添加启动项的配置 利用vi编辑器,vi 查看/ 所在的位置,为/dev/sda3 重
2、新启动系统,从自己创建的内核启动系统启动后查看内核:分别用uname r,和dmesg查看三、Linux 系统调用添加与实现 在内核增加系统调用结构体struct srz_rusage可声明如下:.struct srz_rusage struct timeval ru_utime; /* user time used */struct timeval ru_stime; /* system time used */long ru_majflt; /* major page faults */long ru_minflt; /* minor page faults */long ru_nswap
3、; /* swaps */;添加到下的中添加的系统调用名称为:int get_process_usage(pid_t, struct srz_rusage*);参考的getrusage和sys_getrusage的代码在下面分析getrusage()和sys_getrusage()的源代码1)数据结构rusage 在头文件 中定义。struct rusage struct timeval ru_utime; struct timeval ru_stime; long ru_maxrss; /* maximum resident set size */ long ru_ixrss; /* int
4、egral shared memory size */ long ru_idrss; /* integral unshared data size */ long ru_isrss; /* integral unshared stack size */ long ru_minflt; /* page reclaims */ long ru_majflt; /* page faults */ long ru_nswap; /* swaps */ long ru_inblock; /* block input operations */ long ru_oublock; /* block outp
5、ut operations */ long ru_msgsnd; /* messages sent */, long ru_msgrcv; /* messages received */ long ru_nsignals; /* signals received */ long ru_nvcsw; /* voluntary context switches */ long ru_nivcsw; /* involuntary */2)函数getrusage()的作用是获取系统资源使用情况。 /* * It would make sense to put struct rusage in the
6、task_struct, * except that would make the task_struct be *really big*. After * task_struct gets moved into malloced memory, it would * make sense to do this. It will make moving the rest of the information * a lot simpler! (Which were not doing right now because were not * measuring them yet). * * T
7、his is SMP safe. Either we are called from sys_getrusage on ourselves * below (we know we arent going to exit/disappear and only we change our * rusage counters), or we are called from wait4() on a process which is * either stopped or zombied. In the zombied case the task wont get * reaped till shor
8、tly after the call to getrusage(), in both cases the * task being examined is in a frozen state so the counters wont change. * FIXME! Get the fault counts properly!int getrusage(struct task_struct *p, int who, struct rusage *ru) struct rusage r; memset(char *) &r, 0, sizeof(r); switch (who) case RUS
9、AGE_SELF: = CT_TO_SECS(p-; = CT_TO_USECS(p-: = p-min_flt;maj_flt;nswap; break; case RUSAGE_CHILDREN:cmin_flt;cmaj_flt;cnswap; default: + p-min_flt + p-maj_flt + p-nswap + p- return copy_to_user(ru, &r, sizeof(r) -EFAULT : 0;3)sys_getrusage()只是调用了内核函数getrusage(),是内核提供给用户的接口。asmlinkage long sys_getrus
10、age(int who, struct rusage *ru) if (who != RUSAGE_SELF & who != RUSAGE_CHILDREN) return -EINVAL; return getrusage(current, who, ru); 编写应用程序调用该系统(调用)过程 在中添加函数get_process_usage()和系统调用函数sys_get_process_usage()的代码; intget_process_usage(structtask_struct*p,structsrz_rusage*ru)structr;memset(char*)r,0,siz
11、eof(r); ong SYMBOL_NAME()的清单, 该清单用来对sys_call_table数组进行初始化,该数组包含指向内核中每个系统调用的指针。我们在清单最后添加一行:.long SYMBOL_NAME(sys_get_process_usage)编译安装内核以后的启动界面四、编写程序调用新的系统调用 程序一头文件testsyscall pid_no执行结果Dmesg查看信息 程序二dmesg查看附件:。源程序一第一种实现方法/#include /root / int get (pid_t pid);int main(int argc,char *argv) pid_t pid;
12、pid=atoi(argv1); get (pid);int get (pid_t pid) struct srz_rusage ru; long r=0; if(pid=0) printf(n error: Pid must int!nn); return 0; r=syscall(238,pid,&ru); if(!r) ShangRongZhu success! printf(User_time :t%d seconds: %d msecondsn, System_time : %d mseconds n, printf(Min lost page:t%dn,;Max lost page:?Change page times: t%dn elseError,cheak the pid: n,r);第二种实现方法#define _NR_get_process_usage 238_syscall2(long,get_process_usage,pid_t,pid,struct srz_rusage *,ru) r=get_process_usage(pid,&
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1