操作系统实验报告.doc_第1页
操作系统实验报告.doc_第2页
操作系统实验报告.doc_第3页
操作系统实验报告.doc_第4页
操作系统实验报告.doc_第5页
已阅读5页,还剩59页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

课 程 实 验 报 告 课程名称: 操作系统 专业班级:计算机科学与技术xxxx班 学 号: xxxxxxx 姓 名: xxxx 指导老师: xxxxx 报告日期: 20xx年xx月xx日 计算机科学与技术学院课程设计概述一、 实验目的掌握Linux操作系统的使用方法;了解Linux系统内核代码结构;掌握实例操作系统的实现方法。二、 实验要求1、 掌握Linux操作系统的使用方法,包括键盘命令、系统调用;掌握在Linux下的编程环境。l 编一个C程序,其内容为实现文件拷贝的功能;l 编一个C程序,其内容为分窗口同时显示三个并发进程的运行结果。要求用到Linux下的图形库。2、 掌握系统调用的实现过程,通过编译内核方法,增加一个新的系统调用。另编写一个应用程序,调用新增加的系统调用。实现的功能是:文件拷贝;3、 掌握增加设备驱动程序的方法。通过模块方法,增加一个新的设备驱动程序,其功能可以简单。实现字符设备的驱动;4、 了解和掌握/proc文件系统的特点和使用方法l 了解/proc文件的特点和使用方法l 监控系统状态,显示系统中若干部件使用情况l 用图形界面实现系统监控状态。5、 设计并实现一个模拟的文件系统(选作)三、 设计说明Linux系统版本12.10新编译的内核版本3.6.11虚拟机VMware Fusion 3.0环境MAC OS X 10.68实验一一、 实验目的 1. 编写一个C程序实现文件拷贝的功能2. 编写一个C程序,其内容为分窗口同时显示三个并发进程的运行结果,要求用到Linux下的图形库。二、 实验过程1. 文件拷贝功能要实现文件拷贝功能,主要用到的函数是open、write、read。以前在windows下写C语言打开文件常用的fopen,此时不能用,因为fopen是ANSIC标准中的C语言库函数,在不同的系统中应该调用不同的内核api ;所以应该直接使用linux中的系统函数open。主要用到的头文件:Unistd.h包含了许多Linux系统服务的函数原型,如:read、writeFcntl.h定义了很多宏和open,fcntl函数原型Stdio.h标准输入输出头文件sys/types.h此头文件包含适当时应使用的多个基本派生类型sys/stat.h 包含了获取文件属性的一些函数errno.h用于调试错误代码是所需要的一些errno变量string.h包含了处理字符串的一些函数设计思路:由命令行参数获取2个文件名,根据其文件名和路径分别打开该2个文件,设置一个循环,从源文件复制N个字节到目的文件,直到源文件指针到文件尾,最后关闭2个文件。在可能出错的地方需要加上相应的报错代码和中断,并输出错误信息,以方便调试或是往后应用在第2小题中可能发生的错误。理清楚设计思路后,根据需求写出相应的源代码见后页源程序代码scopy.c ;在Linux终端使用编译命令 gcc o scopy scopy.c将程序编译并生产exe可执行文件。然后手动创建一个测试文件test.txt ,在终端输入命令./scopy test.txt target.txt这样就能将源文件test.txt复制到目标文件target.txt2. 分窗口显示并发进程的运行结果 安装Linux下的GTK+:首先要在Linux下载GTK+相关库文件并安装。在终端输入sudo apt-get install gnome-core-devel ,然后根据提示操作,就会安装 libgtk2.0-dev libglib2.0-dev 等开发所需的相关库文件。 $sudo apt-get install build-essential$sudo apt-get install gnome-core-devel$sudo apt-get install pkg-config$sudo apt-get install libgtk2.0*编译GTK+代码时需要包含的头文件是gtk/gtk.h,此外,还必须连接若干库;比如编译test.c时用以下命令。gcc o test test.c pkg-config -cflags -libs gtk+-2.0在编写代码时需要用到的控件、窗口等视窗物件形态,用类GtkWidget定义其为指针类型。编写一个GTK+程序的基本步骤如下:l 初始化Gtkl 建立控件l 登记消息与消息处理函数l 执行消息循环函数gtk_main()之后所设计的3个进程,基本上都是以这样的方式编写代码的,因为之前曾用过OpenGL,所以在这方面掌握的比较快。初始化主要使用的函数有gtk_init(&argc,&argv); /启动GTK gtk_window_new(GTK_WINDOW_TOPLEVEL); /创建窗口 gtk_window_set_title(GTK_WINDOW(window),标题名); /设置窗口标题名 gtk_widget_set_usize(window, 200, 200); /设置窗口大小 gtk_widget_show(window); /显示窗口建立控件的一般流程/*创建表格准备封装*/gtk_table_new (/创建多少列gint rows,/创建多少栏gint columns,/用来决定表格如何来定大小gint homogeneous);/*这个函数是将表格table,结合到窗口window里*/gtk_container_add(GTK_CONTAINER(window),table);gtk_widget_show(table);/ 显示该表格/*要把物件放进box中,可用以下函数*/void gtk_table_attach_defaults (GtkTable*table,/参数(table)是选定某表格GtkWidget*widget,/(child)是想放进去的物件gintleft_attach,/以下参数是指定把物件放在哪里, 及用多少个boxesgintright_attach,ginttop_attach,gintbottom_attach);三、 实验结果1. 文件拷贝功能 2. 分窗口显示并发进程的运行结果四、 源代码1. 文件拷贝功能#include #include #include #include #include #include #include #define BUFFER_SIZE 1024 /缓冲区大小int main(int argc,char *argv) int from_fd,to_fd; int bytes_read,bytes_write; char bufferBUFFER_SIZE; /设定一个缓冲区 char *ptr; if(argc!=3) /三个参数 fprintf(stderr,Usage:%s fromfile tofilena,argv0); return(-1); /* 打开源文件 */ if(from_fd=open(argv1,O_RDONLY)=-1) fprintf(stderr,Open %s Error:%sn,argv1,strerror(errno); return(-1); /* 创建目的文件 */ if(to_fd=open(argv2,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR)=-1) fprintf(stderr,Open %s Error:%sn,argv2,strerror(errno); return(-1); while(bytes_read=read(from_fd,buffer,BUFFER_SIZE) /* 出错*/ if(bytes_read=-1)&(errno!=EINTR) break; else if(bytes_read0) ptr=buffer; while(bytes_write=write(to_fd,ptr,bytes_read) /* 出错*/ if(bytes_write=-1)&(errno!=EINTR)break; /* 写完了所有读的字节 */ else if(bytes_write=bytes_read) break; /* 只写了一部分,继续写 */ else if(bytes_write0) ptr+=bytes_write; bytes_read-=bytes_write; /* 写的时候出错*/ if(bytes_write=-1)break; close(from_fd); close(to_fd); return(1);2. 分窗口显示并发进程的运行结果#include #include #include gint progress_timeout( gpointer pbar ) gdouble new_val; char s10; new_val = gtk_progress_bar_get_fraction (GTK_PROGRESS_BAR (pbar) + 0.01; if (new_val 1.0) new_val = 0.0; sprintf (s, %.0f%, new_val*100); gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (pbar), new_val); gtk_progress_bar_set_text (GTK_PROGRESS_BAR (pbar),s); return TRUE; void destroy_progress( GtkWidget *widget) gtk_main_quit ();void show(int argc,char *argv,char *title ) GtkWidget *window; GtkWidget *vbox; GtkWidget *pbar; GtkWidget *pbar2; GtkWidget *button; GtkWidget *label; int timer; char id_char50; gtk_init (&argc, &argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_resizable (GTK_WINDOW (window), TRUE); gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); g_signal_connect (G_OBJECT (window), destroy, G_CALLBACK (destroy_progress), NULL); gtk_window_set_title (GTK_WINDOW (window), title); gtk_container_set_border_width (GTK_CONTAINER (window), 0); vbox = gtk_vbox_new (FALSE, 10); gtk_container_set_border_width (GTK_CONTAINER (vbox), 10); gtk_container_add (GTK_CONTAINER (window), vbox); gtk_widget_show (vbox); sprintf (id_char, 本进程ID:%d, getpid (); label = gtk_label_new (id_char); gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0); gtk_widget_show (label); sprintf (id_char, 父进程ID:%d, getppid (); label = gtk_label_new (id_char); gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0); gtk_widget_show (label); pbar = gtk_progress_bar_new (); gtk_box_pack_start (GTK_BOX (vbox), pbar, FALSE, FALSE, 0); gtk_widget_show (pbar); timer = gtk_timeout_add (100, progress_timeout, pbar); button = gtk_button_new_with_label (close); g_signal_connect_swapped (G_OBJECT (button), clicked, G_CALLBACK (gtk_widget_destroy), window); gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0); GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT); gtk_widget_grab_default (button); gtk_widget_show (button); gtk_widget_show (window); gtk_main ();int main(int argc, char *argv) int pid = fork (); if (pid 0)printf (error!n); else if (pid = 0) int pid = fork (); if (pid 0) printf (error!n);else if (pid = 0) show (argc,argv,process3);else show (argc,argv,process2); elseshow (argc,argv,process1); 实验二一、 实验目的 掌握系统调用的实现过程,通过编译内核方法,增加一个新的系统调用。另编写一个应用程序,调用新增加的系统调用。实现的功能是:文件拷贝。二、 实验过程 1. 下载并解压内核到官方网站/下载内核linux-3.6.11.tar.xz,将其放入/usr/src中,打开终端,使用下列命令对其解压:#tar -xvf linux-3.6.11该目录用来存放内核的源码。 2. 修改内核首先对系统调用模块添加一个自定义函数helloworld.c,即添加一个新的自定义函数到/arch/x86/kernel中。在/include/linux/syscalls.h中添加如下代码,进行对系统调用模块的修改asmlinkage int sys_helloworld(const char* s_file, const char* t_file); 接着在arch/x86/syscalls/syscall_32.tbl添加350 i386 helloworldsys_helloworld 在arch/x86/kernel/Makefile中添加 obj-y+= helloworld.o 3. 编译内核依次在终端键入以下命令进行对内核的编译,此过程要持续2小时左右sudo apt-get install fakeroot build-essential kernel-package libncurses5 libncurses5-devcp /boot/config-3.5.0-17-generic ./.configmake menuconfigload .configmake-kpkg cleanfakeroot make-kpkg -initrd kernel_image kernel_headers 4. 安装内核编译完成内核咋终端输入以下命令对内核进行安装sudo dpkg -i linux-image-3.6.11.Custom_i386.debsudo dpkg -i linux-headers-3.6.11.Custom_i386.deb 5. 系统调用使用如下命令实现对系统函数字符拷贝的调用gcc -o testsys.c testsys ./testsys三、 实验结果四、 源代码helloworld.c#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* Move somewhere else to avoid recompiling? */#include #include #include #include #ifndef SET_UNALIGN_CTL# define SET_UNALIGN_CTL(a,b)(-EINVAL)#endif#ifndef GET_UNALIGN_CTL# define GET_UNALIGN_CTL(a,b)(-EINVAL)#endif#ifndef SET_FPEMU_CTL# define SET_FPEMU_CTL(a,b)(-EINVAL)#endif#ifndef GET_FPEMU_CTL# define GET_FPEMU_CTL(a,b)(-EINVAL)#endif#ifndef SET_FPEXC_CTL# define SET_FPEXC_CTL(a,b)(-EINVAL)#endif#ifndef GET_FPEXC_CTL# define GET_FPEXC_CTL(a,b)(-EINVAL)#endif#ifndef GET_ENDIAN# define GET_ENDIAN(a,b)(-EINVAL)#endif#ifndef SET_ENDIAN# define SET_ENDIAN(a,b)(-EINVAL)#endif#ifndef GET_TSC_CTL# define GET_TSC_CTL(a)(-EINVAL)#endif#ifndef SET_TSC_CTL# define SET_TSC_CTL(a)(-EINVAL)#endifasmlinkage int sys_helloworld(const char* s_file, const char* t_file) /printk(hello,world!n); int bytes_read, bytes_write; /file id int from_fd, to_fd; char buffer100; char *ptr; mm_segment_t old_fs; old_fs = get_fs(); set_fs(KERNEL_DS); if (from_fd = sys_open(s_file,O_RDONLY,0) = -1) return -1; if (to_fd = sys_open(t_file,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR) = -1) return -2; /read source file while(bytes_read=sys_read(from_fd,buffer,1) if(bytes_read=-1) break; else if(bytes_read0) ptr=buffer; /write to the target file while(bytes_write=sys_write(to_fd,ptr,bytes_read) if(bytes_write=-1)break; else if(bytes_write=bytes_read) break; else if(bytes_write0) ptr+=bytes_write; bytes_read-=bytes_write; if(bytes_write=-1)break; set_fs(old_fs); return 0; testsys.c#include #include int main(void)syscall(350,./test.txt,./target.txt);return 0;实验三一、 实验目的 掌握增加设备驱动程序的方法。通过模块方法,增加一个新的设备驱动程序,其功能可以简单。实现字符设备的驱动。二、 实验过程 通过以下指令进行字符驱动程序的安装sudo make sudo insmod f device.ko添加设备sudo mknod /dev/device c 147 0 实现功能sudo ./sdevice删除字符设备sudo rmmod device三、 实验结果四、 源代码device.c#include #include #include #include MODULE_LICENSE(GPL);#define MAJOR_NUM 147static ssize_t d_read(struct file *, char *, size_t, loff_t*);static ssize_t d_write(struct file *, const char *, size_t, loff_t*);static ssize_t d_open(struct inode *,struct file *);static ssize_t d_release(struct inode *,struct file *);struct file_operations d_fops= .read=d_read, .write=d_write, .open=d_open, .release=d_release,;static char var1024=Device test:;int counter=12;static int _init device_init(void) int result; result = register_chrdev(MAJOR_NUM, device, &d_fops); if(result) printk(device register failure); else printk(device register success); return result;static void _exit device_exit(void) unregister_chrdev(MAJOR_NUM, device); printk(unloading the device.);static ssize_t d_read(struct file *filp, char *buf, size_t len, loff_t *off) if(copy_to_user(buf,var,1024*sizeof(char) return - EFAULT; return sizeof(char);static ssize_t d_write(struct file *filp,const char *buf,size_t len,loff_t *off) if(copy_from_user(var+counter,buf,len*sizeof(char) return - EFAULT; counter+=len; return sizeof(char); static ssize_t d_open(struct inode *inode,struct file *file) printk(KERN_INFO open it!n); return 0;static ssize_t d_release(struct inode *inode,struct file *file) printk(KERN_INFO close itn); return 0; module_init(device_init);module_exit(device_exit);sdevice.c#include #include #include #include #include main() int fd; char buffer1024; int num; fd=open(/dev/device,O_RDWR,S_IRUSR|S_IWUSR); if(fd!=-1) read(fd,buffer,1024*sizeof(char); printf(The device is %sn,buffer); printf(Please input the characters written to device:n); fgets(buffer,1024,stdin); num=strlen(buffer)-1; write(fd,buffer,num*sizeof(char); read(fd,buffer,1024*sizeof(char); printf(The device is %sn,buffer); close(fd); else printf(Device open failuren); 实验四一、 实验目的了解和掌握/proc文件系统的特点和使用方法了解/proc文件的特点和使用方法监控系统状态,显示系统中若干部件使用情况用图形界面实现系统监控状态。二、 实验过程通过在访问/kernel里的/proc文件获得meninfo等相关信息,并用gtk使其以图形方式显示,相关功能包括:(1)获取并显示主机名(2)获取并显示系统启动的时间 (未实现)(3)显示系统到目前为止持续运行的时间 (未实现)(4)显示系统的版本号(5)显示cpu的型号和主频大小(6)同过pid或者进程名查询一个进程,并显示该进程的详细信息,提供杀掉该进程的功能。(7)显示系统所有进程的一些信息,包括pid,ppid,占用内存大小,优先级等等(8)cpu使用率的图形化显示(2分钟内的历史纪录曲线) (未实现)(9)内存和交换分区(swap)使用率的图形化显示(2分钟内的历史纪录曲线) (未实现)(10)在状态栏显示当前时间 (未实现)(11)在状态栏显示当前cpu使用率(12)在状态栏显示当前内存使用情况(13)用新线程运行一个其他程序 (未实现)(14)关机功能(未实现)三、 实验结果四、 源代码creatwindow.hvoid end_program(GtkWidget *widget, gpointer data) gtk_main_quit();void creatWindow(void) int i; window = g_object_new(GTK_TYPE_WINDOW,title, System Monitor,default_height, windowHeight,default_width, windowWidth,NULL); gtk_container_border_width(GTK_CONTAINER(window),10); notebook = g_object_new(GTK_TYPE_NOTEBOOK, NULL); for (i=0;ipage_quantity;+i) page_titlei=g_object_new(GTK_TYPE_LABEL, label, page_title_chari, NULL); page_tablei = gtk_table_new(gridQuanV,gridQuanH,FALSE); g_signal_connect(window, destroy, G_CALLBACK(end_program), NULL); gtk_container_add(GTK_CONTAINER(window),GTK_WIDGET(notebook); for (i=0;ipage_quantity;+i) gtk_notebook_append_page(GTK_NOTEBOOK(notebook), GTK_WIDGET(page_tablei), GTK_WIDGET(page_titlei); generaldef.hconst int DebugMode=1;const float RefershCycle=1;char *markup1;#define _SVID_SOURCE#include #include #include #include #include #include #include /*=*/#define page_quantity 5const int windowHeight=600;const int windowWidth=900;const int gridQuanV=100;const int gridQuanH=100;GtkWindow *window;GtkNotebook *notebook;GtkLabel *page_titlepage_quantity;char* page_title_charpage_quantity=System Brief,Resource Usage,Process Info,Module Info,Main Filesystem Info,;GtkWidget *page_tablepage_quantity;#include creatwindow.h/*=*/GtkImage *logo;GtkLabel* hostNameLabel;GtkLabel* hostNameText;GtkLabel* linuxSystemLabel;GtkLabel* kernelVersionText;GtkLabel* hardwareBriefLabel;GtkLabel* cpuInfoText;GtkLabel* memInfoText;GtkLabel* authorInfo;GtkHSeparator* hSep1;#include page1.h/*=*/GtkFrame *page2Framepage_quantity;char* page2FrameNamepage_quantity=System Overall Load,CPU Usage,Memory Usage,Network Activity,;int* cpuStruct1;int* cpuStruct2;int countFlag=0;float overallLoad3;GtkLabel* avgLoadLabel;char overallLoadLabelBuffer512;float cpuUsage;char cpuUsageText512;GtkProgress* cpuUsageProgressBar;int memAll;int memFree;int swapAll;int swapFree;float memPercent;float swapPercent;GtkProgress* memProgressBar;GtkProgress* swapProgressBar;char memText512;char swapText512;int netReceive;int netSend;float rece

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论