操作系统课程设计班级:姓名:学号:时间:年月日指导教师:实验题目:操作系统课程设计实验目的:1、掌握Linux操作系统安装的一般方法2、掌握Linux内核升级一般方法3、掌握系统引导管理器Grub的使用4、学会编写简单的模块化的字符设备驱动程序。实验内容:(叙述自己编写的程序)1.安装Linux操作系统:在虚拟机平台上安装CentOS6.5:2.实现操作系统内核的升级:使用命令uname–r查看系统内核版本:到下载新内核并解压缩:在终端进入源码目录输入命令:makemrproper(清除当前目录下残留的.config和.o文件)makeclean(清除编译中间文件)makemenuconfig(配置编译选项)开始编译内核:makebzImagemakemodulesmakemodules_install配置系统引导:编辑文件/boot/grub/grub.confdefault=0所得结果如下:3.编译并安装一个简单的模块化字符设备驱动程序:(1)编写驱动程序文件memdev.c#includelinux/module.h#includelinux/types.h#includelinux/fs.h#includelinux/errno.h#includelinux/mm.h#includelinux/sched.h#includelinux/init.h#includelinux/cdev.h#includeasm/io.h#includeasm/system.h#includeasm/uaccess.h#includelinux/slab.h#includememdev.hstaticmem_major=MEMDEV_MAJOR;module_param(mem_major,int,S_IRUGO);structmem_dev*mem_devp;/*设备结构体指针*/structcdevcdev;/*文件打开函数*/intmem_open(structinode*inode,structfile*filp){structmem_dev*dev;/*获取次设备号*/intnum=MINOR(inode-i_rdev);if(num=MEMDEV_NR_DEVS)return-ENODEV;dev=&mem_devp[num];/*将设备描述结构指针赋值给文件私有数据指针*/filp-private_data=dev;return0;}/*文件释放函数*/intmem_release(structinode*inode,structfile*filp){return0;}/*读函数*/staticssize_tmem_read(structfile*filp,char__user*buf,size_tsize,loff_t*ppos){unsignedlongp=*ppos;unsignedintcount=size;intret=0;structmem_dev*dev=filp-private_data;/*获得设备结构体指针*//*判断读位置是否有效*/if(p=MEMDEV_SIZE)return0;if(countMEMDEV_SIZE-p)count=MEMDEV_SIZE-p;/*读数据到用户空间*/if(copy_to_user(buf,(void*)(dev-data+p),count)){ret=-EFAULT;}else{*ppos+=count;ret=count;printk(KERN_INFOread%dbytes(s)from%d\n,count,p);}returnret;}/*写函数*/staticssize_tmem_write(structfile*filp,constchar__user*buf,size_tsize,loff_t*ppos){unsignedlongp=*ppos;unsignedintcount=size;intret=0;structmem_dev*dev=filp-private_data;/*获得设备结构体指针*//*分析和获取有效的写长度*/if(p=MEMDEV_SIZE)return0;if(countMEMDEV_SIZE-p)count=MEMDEV_SIZE-p;/*从用户空间写入数据*/if(copy_from_user(dev-data+p,buf,count))ret=-EFAULT;else{*ppos+=count;ret=count;printk(KERN_INFOwritten%dbytes(s)from%d\n,count,p);}returnret;}/*seek文件定位函数*/staticloff_tmem_llseek(structfile*filp,loff_toffset,intwhence){loff_tnewpos;switch(whence){case0:/*SEEK_SET*/newpos=offset;break;case1:/*SEEK_CUR*/newpos=filp-f_pos+offset;break;case2:/*SEEK_END*/newpos=MEMDEV_SIZE-1+offset;break;default:/*can'thappen*/return-EINVAL;}if((newpos0)||(newposMEMDEV_SIZE))return-EINVAL;filp-f_pos=newpos;returnnewpos;}/*文件操作结构体*/staticconststructfile_operationsmem_fops={.owner=THIS_MODULE,.llseek=mem_llseek,.read=mem_read,.write=mem_write,.open=mem_open,.release=mem_release,};/*设备驱动模块加载函数*/staticintmemdev_init(void){intresult;inti;dev_tdevno=MKDEV(mem_major,0);/*静态申请设备号*/if(mem_major)result=register_chrdev_region(devno,2,memdev);else/*动态分配设备号*/{result=alloc_chrdev_region(&devno,0,2,memdev);mem_major=MAJOR(devno);}if(result0)returnresult;/*初始化cdev结构*/cdev_init(&cdev,&mem_fops);cdev.owner=THIS_MODULE;cdev.ops=&mem_fops;/*注册字符设备*/cdev_add(&cdev,MKDEV(mem_major,0),MEMDEV_NR_DEVS);/*为设备描述结构分配内存*/mem_devp=kmalloc(MEMDEV_NR_DEVS*sizeof(structmem_dev),GFP_KERNEL);if(!mem_devp)/*申请失败*/{result=-ENOMEM;gotofail_malloc;}memset(mem_devp,0,sizeof(structmem_dev));/*为设备分配内存*/for(i=0;iMEMDEV_NR_DEVS;i++){mem_devp[i].size=MEMDEV_SIZE;mem_devp[i].data=kmalloc(MEMDEV_SIZE,GFP_KERNEL);memset(mem_devp[i].data,0,MEMDEV_SIZE);}return0;fail_malloc:unregister_chrdev_region(devno,1);returnresult;}/*模块卸载函数*/staticvoidmemdev_exit(void){cdev_del(&cdev);/*注销设备*/kfree(mem_devp);/*释放设备结构体内存*/unregister_chrdev_region(MKDEV(mem_major,0),2);/*释放设备号*/}MODULE_AUTHOR(Mac.Zhong);MODULE_LICENSE(GPL);module_init(memdev_init);module_exit(memdev_exit);文件memdev.h#ifndef_MEMDEV_H_#define_MEMDEV_H_#ifndefMEMDEV_MAJOR#defineMEMDEV_MAJOR88/*预设的mem的主设备号*/#endif#ifndefMEMDEV_NR_DEVS#defineMEMDEV_NR_DEVS2/*设备数*/#endif#ifndefMEMDEV_SIZE#defineMEMDEV_SIZE4096#endif/*mem设备描述结构体*/structmem_dev{char*data;unsignedlongsize;};#endif/*_MEMDEV_H_*/(2)为include创建连接文件(3)输入如下命令:cd/usr/includerm-rfasmlinuxscsiln-s/usr/src/linux-3.2.2/include/asm-genericasmln-s/usr/src/linux-3.2.2/include/linuxlinuxln-s/usr/src/linux-3.2.2/include/scsiscsi(4)建立相应文件:cd/rootmkdirfirstdrivertouchmemdev.c#建立驱动程序文件touchmemdev.h#头文件touchMakefile#编写MakefileMakefile:ifneq($(KERNELRELEASE),)obj-m:=memdev.oelseKERNELDIR:=/lib/modules/$(shelluname-r)/buildPWD:=$(shellpwd)default:$(MAKE)-C$(KERNELDIR)M=$(PWD)modulesclean:rm-rf*.o*.mod.c*.mod.o*.koendif(5)编译驱动程序:make-C/lib/modules/2.6.32-generic/buildM=/root/firstdrivermodules(6)找到memdev.ko,将它装入内核imsmodmemdev.ko(7)创建文件节点,mknodmemdev0c880(8)编写测试程序,测试驱动文件test.c#includestdio.hintmain(){FILE*fp0=NULL;charBuf[4096];fp0=fopen(/dev/memdev0,r+);if(fp0==NULL){printf(OpenMemdev0Error!\n);return-1;}else{printf(打开Memdev0成功啦!\n);}strcpy(Buf,Memischardevice!);printf(写入内容BUF:%s\n,Buf);fwrite(Buf,sizeof(Buf),1,fp0);fseek(fp0,0,SEEK_SET);strcpy(Buf,BufisNULL!);printf(现在清空BUF:%s\n,Buf);fread(Buf,sizeof(Buf),1,fp0);printf(读回内容BUF:%s\n,Buf);return0;}遇到的问题及解决:(程序编写调试过程中出现的错误及解决方法)1.虚拟机不会使用,听老师讲解以及百度解决;2.linux系统命令不会使用,询问老师以及百度解决;3.字符设备驱动程序不会编译和安装,百度解决