您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 冶金工业 > 添加系统调用实验报告
1一、构建基本的实验环境1.1基本实验环境与前提条件Windows7、Word2010、VmwareWorkStation8.5、AdobeReaderReadHatLinux9.0,gcc,viLinux内核[V2.4.18]1.2虚拟机的安装及使用1.3将Linux内核源代码及配置文件传送给虚拟机上的RedHatLinuxV9.0系统配置网络时遇到这个问题,DeterminingIPinformationforeth0...failed;nolinkpresent.Checkcable?通过查找资料发现是系统的Bug,解决方法如下:到/etc/sysconfig/network-scripts/ifcfg-eth0在文件最后一行中加入check_link_down(){return1;}另外如果存在/etc/sysconfig/networking/profiles/default/ifcfg-eth0文件,则同样在其中加入这一段东西即可,然后重启系统。设置网络为DHCP,重新启动就可以,啦,直接上图2最后将内核代码下载到/root目录下二、Linux内核编译、配置与调试2.1内核配置与编译2.1.1、解压内核源代码文件tar-zxflinux-2.4.18.tar.gz2.1.2、解压后如下32.1.3、拷贝linux,命名为linux-2.4.18cp-rlinuxlinux-2.4.182.1.4、移动config-2.4.18forMP.txt到linux-2.4.18根目录,替换掉.config2.1.5、进入linux-2.4.18目录,配置和编译内核模块makeoldconfigmakedep4makecleanmakebzImagemakemodules2.2内核安装与测试2.2.1安装内核映像文件5cparch/i386/boot/bzImage/boot/vmlinux-2.4.182.2.2拷贝和安装Linux系统映射文件System.map,并创建其与系统映射文件System.map之间的符号链接2.2.3执行命令makemodules_install以安装可动态加载的内核模块2.2.4添加启动项的配置利用vi编辑器,vigrub.conf查看/所在的位置,为/dev/sda362.2.5reboot重新启动系统,从自己创建的内核启动系统启动后查看内核分别用uname–r,和dmesg查看三、Linux系统调用添加与实现73.1在内核增加系统调用3.1.1结构体structsrz_rusage可声明如下:.structsrz_rusage{structtimevalru_utime;/*usertimeused*/structtimevalru_stime;/*systemtimeused*/longru_majflt;/*majorpagefaults*/longru_minflt;/*minorpagefaults*/longru_nswap;/*swaps*/};3.1.2添加到linux-2.4.18/include/linux下的resource.h中3.1.3添加的系统调用名称为:intget_process_usage(pid_t,structsrz_rusage*);参考的getrusage和sys_getrusage的代码在linux-2.4.18/linux/kernel/sys.c下面3.1.4分析getrusage()和sys_getrusage()的源代码1)数据结构rusage在头文件resource.h中定义。structrusage{structtimevalru_utime;/*usertimeused*/structtimevalru_stime;/*systemtimeused*/longru_maxrss;/*maximumresidentsetsize*/longru_ixrss;/*integralsharedmemorysize*/longru_idrss;/*integralunshareddatasize*/longru_isrss;/*integralunsharedstacksize*/longru_minflt;/*pagereclaims*/longru_majflt;/*pagefaults*/longru_nswap;/*swaps*/longru_inblock;/*blockinputoperations*/longru_oublock;/*blockoutputoperations*/longru_msgsnd;/*messagessent*/longru_msgrcv;/*messagesreceived*/longru_nsignals;/*signalsreceived*/longru_nvcsw;/*voluntarycontextswitches*/longru_nivcsw;/*involuntary*/};2)函数getrusage()的作用是获取系统资源使用情况。/*8*Itwouldmakesensetoputstructrusageinthetask_struct,*exceptthatwouldmakethetask_structbe*reallybig*.After*task_structgetsmovedintomalloc'edmemory,itwould*makesensetodothis.Itwillmakemovingtherestoftheinformation*alotsimpler!(Whichwe'renotdoingrightnowbecausewe'renot*measuringthemyet).**ThisisSMPsafe.Eitherwearecalledfromsys_getrusageonourselves*below(weknowwearen'tgoingtoexit/disappearandonlywechangeour*rusagecounters),orwearecalledfromwait4()onaprocesswhichis*eitherstoppedorzombied.Inthezombiedcasethetaskwon'tget*reapedtillshortlyafterthecalltogetrusage(),inbothcasesthe*taskbeingexaminedisinafrozenstatesothecounterswon'tchange.**FIXME!Getthefaultcountsproperly!*/intgetrusage(structtask_struct*p,intwho,structrusage*ru){structrusager;memset((char*)&r,0,sizeof(r));switch(who){caseRUSAGE_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;caseRUSAGE_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);9r.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;}returncopy_to_user(ru,&r,sizeof(r))?-EFAULT:0;}3)sys_getrusage()只是调用了内核函数getrusage(),是内核提供给用户的接口。asmlinkagelongsys_getrusage(intwho,structrusage*ru){if(who!=RUSAGE_SELF&&who!=RUSAGE_CHILDREN)return-EINVAL;returngetrusage(current,who,ru);}3.2编写应用程序调用该系统(调用)过程3.2.1在sys.c中添加函数get_process_usage()和系统调用函数sys_get_process_usage()的代码;目录linux-2.4.18/kernel/sys.cintget_process_usage(structtask_struct*p,structsrz_rusage*ru){structsrz_rusager;memset((char*)&r,0,sizeof(r));//比上次的实验报告里,更新的内容printk(“Theprogram(get_process_usage)issuccessful!\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){10structtask_struct*p;p=find_task_by_pid(pid);if(p){returnget_process_usage(p,ru);}return-EINVAL;}3)修改完源程序以后,下一个任务是使Linux内核知道该程序的存在。为了从已有的内核程序中增加到新的函数的连接,需要编辑两个文件。第一个要修改的文件是/root/linux-2.4.
本文标题:添加系统调用实验报告
链接地址:https://www.777doc.com/doc-4362134 .html