




已阅读5页,还剩15页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
一、构建基本的实验环境1.1基本实验环境与前提条件Windows7 、Word 2010、Vmware WorkStation 8.5、AdobeReaderReadHatLinux 9.0,gcc,viLinux内核V2.4.181.2虚拟机的安装及使用1.3将Linux 内核源代码及配置文件传送给虚拟机上的Red Hat Linux V9.0 系统配置网络时遇到这个问题,Determining IP information for eth0. failed; no link present. Check cable? 通过查找资料发现是系统的Bug,解决方法如下:到/etc/sysconfig/network-scripts/ifcfg-eth0在文件最后一行中加入check_link_down () return 1; 另外如果存在/etc/sysconfig/networking/profiles/default/ifcfg-eth0 文件,则同样在其中加入这一段东西即可,然后重启系统。设置网络为DHCP,重新启动就可以,啦,直接上图最后将内核代码下载到/root目录下二、Linux 内核编译、配置与调试2.1 内核配置与编译2.1.1、解压内核源代码文件tar -zxf linux-2.4.18.tar.gz2.1.2、解压后如下 2.1.3、拷贝linux,命名为linux-2.4.18cp -r linux linux-2.4.18 2.1.4、移动config-2.4.18forMP.txt到linux-2.4.18根目录,替换掉.config 2.1.5、进入linux-2.4.18目录,配置和编译内核模块 make oldconfig make dep make clean make bzImage make modules2.2 内核安装与测试 2.2.1安装内核映像文件 cp arch/i386/boot/bzImage /boot/vmlinux-2.4.18 2.2.2拷贝和安装Linux系统映射文件System.map,并创建其与系统映射文件System.map之间的符号链接2.2.3执行命令make modules_install 以安装可动态加载的内核模块 2.2.4添加启动项的配置 利用vi编辑器,vi grub.conf查看/ 所在的位置,为/dev/sda3 2.2.5reboot重新启动系统,从自己创建的内核启动系统启动后查看内核分别用uname r,和dmesg查看三、Linux 系统调用添加与实现3.1 在内核增加系统调用3.1.1结构体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; /* swaps */;3.1.2添加到linux-2.4.18/include/linux下的resource.h中3.1.3添加的系统调用名称为:int get_process_usage(pid_t, struct srz_rusage*);参考的getrusage和sys_getrusage的代码在linux-2.4.18/linux/kernel/sys.c下面3.1.4分析getrusage()和sys_getrusage()的源代码1)数据结构rusage 在头文件 resource.h中定义。structrusage struct timeval ru_utime;/* user time used */struct timeval ru_stime;/* system time used */longru_maxrss;/* maximum resident set size */longru_ixrss;/* integral shared memory size */longru_idrss;/* integral unshared data size */longru_isrss;/* integral unshared stack size */longru_minflt;/* page reclaims */longru_majflt;/* page faults */longru_nswap;/* swaps */longru_inblock;/* block input operations */longru_oublock;/* block output operations */longru_msgsnd;/* messages sent */longru_msgrcv;/* messages received */longru_nsignals;/* signals received */longru_nvcsw;/* voluntary context switches */longru_nivcsw;/* involuntary */;2)函数getrusage()的作用是获取系统资源使用情况。 /* * It would make sense to put struct rusage in the 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). * * This 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 shortly 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 RUSAGE_SELF:r.ru_utime.tv_sec = CT_TO_SECS(p-times.tms_utime);r.ru_utime.tv_usec = CT_TO_USECS(p-times.tms_utime);r.ru_stime.tv_sec = CT_TO_SECS(p-times.tms_stime);r.ru_stime.tv_usec = CT_TO_USECS(p-times.tms_stime);r.ru_minflt = p-min_flt;r.ru_majflt = p-maj_flt;r.ru_nswap = p-nswap;break;case RUSAGE_CHILDREN:r.ru_utime.tv_sec = CT_TO_SECS(p-times.tms_cutime);r.ru_utime.tv_usec = CT_TO_USECS(p-times.tms_cutime);r.ru_stime.tv_sec = CT_TO_SECS(p-times.tms_cstime);r.ru_stime.tv_usec = CT_TO_USECS(p-times.tms_cstime);r.ru_minflt = p-cmin_flt;r.ru_majflt = p-cmaj_flt;r.ru_nswap = p-cnswap;break;default:r.ru_utime.tv_sec = CT_TO_SECS(p-times.tms_utime + p-times.tms_cutime);r.ru_utime.tv_usec = CT_TO_USECS(p-times.tms_utime + p-times.tms_cutime);r.ru_stime.tv_sec = CT_TO_SECS(p-times.tms_stime + p-times.tms_cstime);r.ru_stime.tv_usec = CT_TO_USECS(p-times.tms_stime + p-times.tms_cstime);r.ru_minflt = p-min_flt + p-cmin_flt;r.ru_majflt = p-maj_flt + p-cmaj_flt;r.ru_nswap = p-nswap + p-cnswap;break;return copy_to_user(ru, &r, sizeof(r) ? -EFAULT : 0;3)sys_getrusage()只是调用了内核函数getrusage(),是内核提供给用户的接口。asmlinkage long sys_getrusage(int who, struct rusage *ru)if (who != RUSAGE_SELF & who != RUSAGE_CHILDREN)return -EINVAL;return getrusage(current, who, ru);3.2 编写应用程序调用该系统(调用)过程 3.2.1在sys.c中添加函数get_process_usage()和系统调用函数sys_get_process_usage()的代码; 目录linux-2.4.18/kernel/sys.c intget_process_usage(structtask_struct*p,structsrz_rusage*ru)structsrz_rusager;memset(char*)&r,0,sizeof(r);/比上次的实验报告里,更新的内容printk(“The program(get_process_usage) is successful !n”);r.ru_utime.tv_sec=CT_TO_SECS(p-times.tms_utime);r.ru_utime.tv_usec=CT_TO_USECS(p-times.tms_utime);r.ru_stime.tv_sec=CT_TO_SECS(p-times.tms_stime); r.ru_stime.tv_usec=CT_TO_USECS(p-times.tms_stime); r.ru_minflt=p-min_flt; r.ru_majflt=p-maj_flt;r.ru_nswap=p-nswap;returncopy_to_user(ru,&r,sizeof(r)?-EFAULT:0; asmlinkagelongsys_get_process_usage(pid_tpid,structsrz_rusage*ru)structtask_struct*p;p=find_task_by_pid(pid);if(p)returnget_process_usage(p,ru);return-EINVAL;3)修改完源程序以后,下一个任务是使Linux内核知道该程序的存在。为了从已有的内核程序中增加到新的函数的连接,需要编辑两个文件。第一个要修改的文件是/root/linux-2.4.18/ include/asm-i386/unistd.h,该文件中包含了系统调用清单,用来给每个系统调用分配一个唯一的号码。应该将新的系统调用名称加到清单的最后,并给它分配号码序列中下一个可用的系统调用号。#define _NR_get_process_usage 238根据上图已知系统调用已经用到了237,我们用238 第二个要修改的文件是:/root/linux-2.4.18/arch/i386/kernel/entry.S,该文件中有类似.long SYMBOL_NAME()的清单, 该清单用来对sys_call_table数组进行初始化,该数组包含指向内核中每个系统调用的指针。我们在清单最后添加一行:.long SYMBOL_NAME(sys_get_process_usage)编译安装内核以后的启动界面四、编写程序调用新的系统调用4.1.1 程序一头文件/get_process_usag.h#include #include 源程序/ get_process_usag.c#include get_process_usag.h#include int get (pid_t pid);int main(int argc,char *argv) pid_t pid; pid=atoi(argv1); get (pid); return 1;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) printf(ShangRongZhu success!nn); printf(User_time :t%d seconds: %d msecondsn,ru.ru_utime.tv_sec,ru.ru_utime.tv_usec); printf(System_time :t%d seconds: %d mseconds n,ru.ru_stime.tv_sec,ru.ru_stime.tv_usec); printf(Min lost page:t%dn,ru.ru_minflt); printf(Max lost page:t%dn,ru.ru_majflt); printf(Change page times: t%dn,ru.ru_nswap); else printf(Error,cheak the pid: n,r);编译Gcc o testsyscall getpu_syscall.c执行./testsyscall pid_no执行结果Dmesg查看信息4.1.2程序二/ get_process_syscall2.c#include get_process_usag.h#include #define _NR_get_process_usage 238_syscall2(long,get_process_usage,pid_t,pid,struct srz_rusage *,ru)int get (pid_t pid);int main(int argc,char *argv) pid_t pid; pid=atoi(argv1); get (pid); return 1;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=get_process_usage(pid,&ru); if(!r) printf(ShangRongZhu success!nn); printf(User_time :t%d seconds: %d msecondsn,ru.ru_utime.tv_sec,ru.ru_utime.tv_usec); printf(System_time :t%d seconds: %d mseconds n,ru.ru_stime.tv_sec,ru.ru_stime.tv_usec); printf(Min lost page:t%dn,ru.ru_minflt); printf(Max lost page:t%dn,ru.ru_majflt); printf(Change page times: t%dn,ru.ru_nswap); else printf(Error,cheak the pid: n,r);执行结果如下编译Gcc -o testsyscall2 getpu_syscall2.c./testsyscall pid_nodmesg查看附件:源程序一:第一种实现方法/get_process_usag.h#include #include 源程序/ get_process_usag.c#include get_process_usag.h#include int get (pid_t pid);int main(int argc,char *argv) pid_t pid; pid=atoi(argv1); get (pid); return 1;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) printf(ShangRongZhu success!nn); printf(User_time :t%d seconds: %d msecondsn,ru.ru_utime.tv_sec,ru.ru_utime.tv_usec); printf(System_time :t%d seconds: %d mseconds n,ru.ru_stime.tv_sec,ru.ru_stime.tv_usec); printf(Min lost page:t%dn,ru.ru_minflt); printf(Max lost page:t%dn,ru.ru_majflt); printf(Change
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 建设部建筑设计协议书
- 可爱的小鸟150字(12篇)
- 名著导读:高一语文阅读理解教案
- 时间像小马车节奏课件
- 人教版四年级上册第二单元《公顷和平方千米》单元检测卷(含答案)
- 纪检业务知识培训课件前言
- 早教课程示范课完整课件
- 农民专业合作社种植联产合同
- 2025年日语J.TEST考试A-D级模拟试卷
- 初识化学实验探究课教案
- 环保与物业公司合作协议
- GB/T 2820.12-2002往复式内燃机驱动的交流发电机组第12部分:对安全装置的应急供电
- 设备基础知识-动设备课件
- GB/T 12599-2002金属覆盖层锡电镀层技术规范和试验方法
- 面条制品-课件
- 2023年西安陕鼓动力股份有限公司招聘笔试题库及答案解析
- 四上科学第一单元《多样的动物》知识梳理
- 放射源辐射事故专项应急预案
- 微观经济学-范里安varian中级
- (完整)人教版高一英语必修一单词表
- 第5章金属在自然环境中的腐蚀ppt课件
评论
0/150
提交评论