




已阅读5页,还剩47页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
计算机科学与技术学院实验报告:3实验题目:信号量同步问题日期:2010-11-10姓名: 实验目的:在本次实验中,通过使用信号量,在原有的程序框架的基础上添加关键代码实现生产者/消费者同步问题。从而深入理解nachos的信号量的使用以及实现,生产者/消费者问题是如何用信号量实现的以及在nachos中是如何创建线程,实现多线程。硬件环境:软件环境:linux 实验步骤:1.首先初始化三个信号量,代码如下:mutex = new semaphore(mutux,1);信号量初始化为1,才能起到加锁功能nfull = new semaphore(full,0);nfull的大小在生产者没生产前为0nempty = new semaphore(empty,buff_size);nempty的大小应该为buffer的大小2.首先考虑生产者进程,首先要查看buffer是否有空, nempty-p();if nempty0,nempty=nempty -1,当对缓冲区操作时必须要加锁:mutex-p();加锁.然后向ring中放入message信息,其次还要解锁mutex-v();解锁.最后通知消费者buffer有新信息, nfull-v();nfull=nfull+1;具体实现代码如下:3. 考虑消费者进程,像生产者进程一样,查看buffer中是否有信息nfull-p();if nfull0,nfull-1;取消息时也要上锁,即:mutex-p();加锁.然后从ring buffer中取出信息;其次mutex-v();解锁;最后通知生产者bufferr有空nempty-v();nempty=nempty+1,具体代码如下:4. 创建线程生成一个生产者的代码:producersi = new thread(prod_namesi);producersi - fork(producer,i);4. 创建线程生成一个消费者的代码:producersi = new thread(prod_namesi);producersi - fork(producer,i);关键代码:voidproducer(_int which) int num; slot *message = new slot(0,0); for (num = 0; num thread_id=which; message-value=num;/p,v 操作 nempty-p(); mutex-p(); ring-put(message);/p,v 操作 mutex-v(); nfull-v(); voidconsumer(_int which) char strmaxlen; char fnamelinelen; int fd; slot *message = new slot(0,0); sprintf(fname, tmp_%d, which); / create a file. note that this is a unix system call. if ( (fd = creat(fname, 0600) ) = -1) perror(creat: file create failed);exit(1); for (; ; ) / p,v,操作 nfull-p(); mutex-p(); ring-get(message); / p,v,操作 mutex-v(); nempty-v(); / form a string to record the message sprintf(str,producer id - %d; message number - %d;n,message-thread_id,message-value); /把信息写入文件 if ( write(fd, str, strlen(str) = -1 ) perror(write: write failed); exit(1); /-/ prodcons/ 初始化信号量以及需要的生产者消费者线程/-voidprodcons() int i; debug(t, entering prodcons);/ 初始化信号量,包括一个访问互斥信号量,初值为1;/一个nempty信号量,初值为缓冲区的大小/一个nfull的信号量,初值为0 mutex=new semaphore(mutex,1); nempty=new semaphore(nempty,buff_size); nfull=new semaphore(nfull,0); / 新建一个缓冲区 ring=new ring(buff_size+1); / create and fork n_prod of producer threads for (i=0; i fork(producer,i); ; / create and fork n_cons of consumer threads for (i=0; i fork(consumer,i); ;调试记录:在源代码中exit(0)没有大写,调试过程发现了这个问题改正,在使用linux系统调用写入文件时,有一个头文件没有引入,因而需要修改#include #include copyright.h#include system.h#include #include 而且对于新添加的头文件的方法其中源文件使用的一个方法是废弃的,所以改成相应的方法write(fd, str, strlen(str),实验结果:生成两个文件分别代表两个消费者取得的产品的记录。文件tmp_0producer id - 0; message number - 3;文件tmp_1producer id - 0; message number - 0;producer id - 1; message number - 0;producer id - 1; message number - 1;producer id - 0; message number - 1;producer id - 0; message number - 2;producer id - 1; message number - 2;producer id - 1; message number - 3;分析结果:从实验结果中可以看出,两个消费者取得的产品的顺序和生成者生产的顺序是一致的。结果正确。(实验所在目录:home/lu/csc2404/nachos-3.4/code/lab3)结论分析与体会:在本次实验中,实现生产者/消费者同步问题,通过使用信号量,即nachos 提供的系统调用,进一步理解nachos的信号量的使用以及实现同时,学会在nachos中是如何创建线程,实现多线程,理解了多线程的问题。计算机科学与技术学院实验报告:5实验题目:扩展nachos的文件系统学号:200800130090 日期:2010-11-10姓名:陆思思 email:实验目的:nachos的文件系统的文件的大小是不可扩展的:文件被创建后,文件的大小就不能再改变。本次实验的目的即是设计扩展nachos的文件系统,使得文件的大小是可以被扩展的。这样就可以实现在一个文件尾部或者中间追加文件。硬件环境:软件环境:linux操作系统,nachos操作系统 实验步骤:1, 了解nachos文件系统的结构,为一级目录结构,其中目录结构以及目录的使用记录保存在文件中。使用bitmap来获取空闲的扇区号。 class directoryentry public: bool inuse;/ is this directory entry in use? int sector;/ location on disk to find the / fileheader for this file char namefilenamemaxlen + 1;/ text name for file, with +1 for / the trailing 0;这个是directoryentry类,也就是目录项。directory:directory(int size) table = new directoryentrysize; tablesize = size; for (int i = 0; i tablesize; i+)tablei.inuse = false;这个是目录类,也就是一级目录结构的定义。booldirectory:add(char *name, int newsector) if (findindex(name) != -1)return false; for (int i = 0; i tablesize; i+) if (!tablei.inuse) tablei.inuse = true; strncpy(, name, filenamemaxlen); tablei.sector = newsector; return true; return false;/ no space. fix when we have extensible files.这个是添加一个目录项的方法,当创建一个新文件的时候使用。boolfilesystem:create(char *name, int initialsize)这个是创建一个新的文件,其中主要工作是新建一个fileheader,作为一个目录项中保存的 int sector;fileheader,即文件头,中保存了这个文件的大小,所占的扇区的数目,以及所占用的全部的扇区号。即: int numbytes;/ number of bytes in the file int numsectors;/ number of data sectors in the file int datasectorsnumdirect;/ disk sector numbers for each data / block in the file因此,为了实现对文件的追加工作,首先对fileheader类里面加入新的方法bool appsectors(bitmap *freemap, int filesize);,为了改变一个文件的文件头的大小。2, 实现在一个已有的文件尾部追加新的内容。首先写改变文件头中对文件所在扇区的描述,由appsectors来实现;该方法将在openfile类的对象执行append file 时被调用。对fileheader类里面加入新的方法bool appsectors(bitmap *freemap, int filesize);boolfileheader:appsectors(bitmap *freemap, int appfilesize)/如果要追加的文件大小小于等于0,则直接函数返回 if(appfilesize= appfilesize) numbytes += appfilesize; return true; else int needfilesize = appfilesize - restfilesize; if(freemap-numclear() divroundup(needfilesize, sectorsize) return false; int i = numsectors; numbytes += appfilesize; numsectors += divroundup(needfilesize, sectorsize); for( ; i find(); return true; printf(the fileheader see filesize %dn,filelength();3,在openfile.cc文件中,openfile类中加入方法,用于追加文件的appfilesize(int numbyte)。这个方法首先从文件系统中获取空闲的扇区的位试图文件,构造bitmap对象,传给fileheader类的对象(也就是这个openfile的文件头),执行appsectors(bitmap *freemap, int filesize)方法,扩大文件的长度。boolopenfile:appfilesize(int numbytes) /fetch the bitmap openfile *freemapfile= new openfile(freemapsector); bitmap *freemap = new bitmap(numsectors); freemap-fetchfrom(freemapfile); /ask for new space if(!(hdr-appsectors(freemap, numbytes)return false;printf(openfile see filesize %dn,length(); /write back the bitmap freemap-writeback(freemapfile); delete freemapfile; delete freemap; return true;4,在fstest.cc文件中的修改append方法,也就是在nachos运行的时候执行命令,例如:nachos x ap ./test/small small这个方法是nachos系统中本来就提供好的一个方法,只需要在局部修改一下语句,就可以正确的运行。voidappend(char *from, char *to, int half)/*filelength 是计算出来的linux文件中的文件的大小filelengthbefore是指追加之前的nachos系统的文件的大小start是文件开始追加的位置*/ file *fp; openfile* openfile; int amountread, filelength; char *buffer;/ start position for appending int start;/ open unix file if (fp = fopen(from, r) = null) printf(copy: couldnt open input file %sn, from);return; / figure out length of unix file fseek(fp, 0, 2); filelength = ftell(fp); fseek(fp, 0, 0); if (filelength = 0) printf(append: nothing to append from file %sn, from);return; if ( (openfile = filesystem-open(to) = null) / file to does not exits, then create oneif (!filesystem-create(to, 0) printf(append: couldnt create the file %s to appendn, to); fclose(fp); return;openfile = filesystem-open(to); int filelengthbefore=openfile-length();/*执行追加,并判断要追加文件的大小是否超过最大长度*/ if(!(openfile-appfilesize(filelength) printf(the appending file is too big!nappending file failedn);/too long file to append return; assert(openfile != null); / append from position start start=openfile-length()-filelengthbefore;printf(value of start %dn,start); if (half) start = start / 2;/如果是从文件的中部追加 openfile-seek(start); / append the data in transfersize chunks buffer = new chartransfersize; while (amountread = fread(buffer, sizeof(char), transfersize, fp) 0) int result;printf(start value: %d, amountread %d, , start, amountread);result = openfile-writeat(buffer, amountread, start);printf(result of write: %dn, result);start += amountread; delete buffer;/ write the inode back to the disk, because we have changed it/需要把该文件的文件头写回磁盘 openfile-writeback(to); printf(inodes have been written backn);/ close the unix and the nachos files delete openfile; fclose(fp);5,运行结果:首先执行./nachos f ,格式化nachos磁盘luubuntu:/csc2404/nachos-3.4/code/appfilesys$ ./nachos -fno threads ready or runnable, and no pending interrupts.assuming the program completed.machine halting!ticks: total 82600, idle 82350, system 250, user 0disk i/o: reads 3, writes 5console i/o: reads 0, writes 0paging: faults 0network i/o: packets received 0, sent 0cleaning up.然后执行追加文件的操作:./nachos -ap test/small smallluubuntu:/csc2404/nachos-3.4/code/appfilesys$ ./nachos -ap test/small smallopenfile see filesize 38value of start 38start value: 38, amountread 10, result of write: 0start value: 48, amountread 10, result of write: 0start value: 58, amountread 10, result of write: 0start value: 68, amountread 8, result of write: 0inodes have been written backno threads ready or runnable, and no pending interrupts.assuming the program completed.machine halting!ticks: total 99100, idle 98400, system 700, user 0disk i/o: reads 16, writes 6console i/o: reads 0, writes 0paging: faults 0network i/o: packets received 0, sent 0cleaning up.再执行从文件中部追加文件的操作:./nachos -hap test/small smallluubuntu:/csc2404/nachos-3.4/code/appfilesys$ ./nachos -hap test/small smallopenfile see filesize 76value of start 38start value: 19, amountread 10, result of write: 10start value: 29, amountread 10, result of write: 10start value: 39, amountread 10, result of write: 10start value: 49, amountread 8, result of write: 8inodes have been written backno threads ready or runnable, and no pending interrupts.assuming the program completed.machine halting!ticks: total 83100, idle 82470, system 630, user 0disk i/o: reads 14, writes 6console i/o: reads 0, writes 0paging: faults 0network i/o: packets received 0, sent 0cleaning up. luubuntu:/csc2404/nachos-3.4/code/appfilesys$ ./nachos -hap test/small smallopenfile see filesize 76value of start 38start value: 19, amountread 10, result of write: 10start value: 29, amountread 10, result of write: 10start value: 39, amountread 10, result of write: 10start value: 49, amountread 8, result of write: 8inodes have been written backno threads ready or runnable, and no pending interrupts.assuming the program completed.machine halting!ticks: total 83100, idle 82470, system 630, user 0disk i/o: reads 14, writes 6console i/o: reads 0, writes 0paging: faults 0network i/o: packets received 0, sent 0cleaning up.最后执行“显示“操作,即显示nachos磁盘当前的内容:luubuntu:/csc2404/nachos-3.4/code/appfilesys$ ./nachos -dbit map file header:fileheader contents. file size: 128. file blocks:2 file contents:7f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000directory file header:fileheader contents. file size: 200. file blocks:3 4 file contents:10005000small0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000bitmap set:0, 1, 2, 3, 4, 5, 6, directory contents:name: small, sector: 5fileheader contents. file size: 76. file blocks:6 file contents:this is the spring this is the spring of our discontent.aof our discontent.ano threads ready or runnable, and no pending interrupts.assuming the program completed.machine halting!ticks: total 6500, idle 6090, system 410, user 0disk i/o: reads 12, writes 0console i/o: reads 0, writes 0paging: faults 0network i/o: packets received 0, sent 0cleaning up.(实验所在目录:csc2404/nachos-3.4/code/appfilesys主要修改文件,filehdr.cc,openfile.cc,fstest.cc,filehdr.h,openfile.h)结论分析与体会:通过增加nachos文件系统对文件的追加功能,理解了文件系统的管理方式,通过目录,目录项,文件头,进一步理解了文件的存储方式,以及空闲磁盘空间的管理,使用位试图 的方式管理空闲的磁盘空间。返回计算机科学与技术学院实验报告:6,7,8实验题目:地址空间的扩展,实现系统调用exec,exit日期:2010-11-5email:实验目的:扩展现有的class addrspace的实现,使得nachos可以实现多用户程序,完成系统调用exec使nachos按页分配内存空间硬件环境:软件环境:linux,nachos,mips 实验步骤:1. 首先编写实验6的print函数,为了可以显示内存空间的分配。以下为print函数的实现:2. 编写一个exec.c的源码,然后编译生成一个可执行文件,exec.noff exec.c的源码:3. nachos原来实现addressspace分配的时候没有使用bitmap来寻找空闲页,而是直接的从0号内存空间开始分配,因而需要修改成使用bitmap的find函数分配内存空间其中,要声明一个位试图的是对象,在addrspace中并且数据段代码段按页存储。4. 计算加载一个程序需要的页表的数目5. 实现nachos的系统调用,采用的是触发异常中断的,在userprog/exception.cc,添加sc_exec异常,存放要执行的文件的路径的字符串的首地址存放在4号寄存器中,因此可以通过这个方式找到文件的路径,从而使用文件系统提供的方法打开这个文件:6. 结果分析:编译成功之后,输入命令 ./nachos x ./test/exec.noff运行结果:luubuntu:/csc2404/nachos-3.4/code/userprog$ ./nachos -x ./test/exec.noffpage table dump: 12 pages in total=virtpage,physpage0,01,12,23,34,45,56,67,78,89,910,1011,11=the allocated physical address:0he address in the executable file :40copy size 128the allocated physical address:128he address in the executable file :168copy size 128the allocated physical address:256he address in the executable file :296copy size 128sc_exec in exceptionhandlerthe value of the register 4 is, .i.e. the address of the string 272add content of fn is now: 742f2e2ethe char view is . . / tadd content of fn is now: 2f747365the char view is e s t /add content of fn is now: 746c6168the char view is h a l tadd content of fn is now: 666f6e2ethe char view is . n o fadd content of fn is now: 66the char view is f add content of fn is now: 0the char view is add content of fn is now: 0the char view is add content of fn is now: 0the char view is add content of fn is now: 0the char view is add content of fn is now: 0the char view is exec file is ./test/halt.noffpage table dump: 11 pages in total=virtpage,physpage0,121,132,143,154,165,176,187,198,209,2110,22=the allocated physical address:1536he address in the executable file :40copy size 128the allocated physical address:1664he address in the executable file :168copy size 128the allocated physical address:1792he address in the executable file :296copy size 128machine halting!ticks: total 50, idle 0, system 10, user 40disk i/o: reads 0, writes 0console i/o: reads 0, writes 0paging: faults 0network i/o: packets received 0, sent 0cleaning up.luubuntu:/csc2404/nachos-3.4/code/userprog$加载进来的程序的地址空间没有覆盖原来那个程序的地址空间,使用新的地址空间去执行关机操作machine halting!实现了一个程序加载另一个程序运行。(实验所在路径: csc2404/nachos-3.4/code/ multiprocess 主要修改的文件,addrspace.cc exception.cc )结论分析与体会:nachos之前没有实现按页分配地址空间,物理页和逻辑页地址一致,而且数据段代码段连续分配每当一个新的用户程序建立的时候,虚拟地址和物理地址都是从0开始分配,这样新的用户程序将会冲掉原来在物理0开始的程序。因而使用位示图分配物理地址。使用bitmap的find函数分配虚存对应的物理地址,在为数据段和代码段写入数据的时候是以扇区为单位的,而不是原有的连续一个文件的读入连续的内存。nachos操作系统通过中断的方式实现系统调用。需要增加userprog/exception.cc中的内容,即必须在此类中添加处理exec的方法。 返回计算机科学与技术学院实验报告:9实验题目:设计并实现具有优先级的线程调度策略 日期:2010-11-13 email:实验目的:nachos系统采用基本的fcfs的线程调度策略,修改成为具有优先级的线程调度策略硬件环境:软
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 《谁画的鱼最大》课件
- 颅内高压危象和脑疝护理
- 亲子沟通课件
- 亲子行李箱课件
- 亲子安全培训课件
- 自动双层停车场控制系统设计
- 腾讯会议录制讲解方法
- 公司级安全培训经验课件
- 新入职怎么写年终总结
- 腹股沟斜疝术后护理查房
- 湿地巡护员培训课件
- 2025鄂尔多斯市城市建设投资集团招聘92人考试参考题库及答案解析
- 2025年地质实验室技术员综合素质考核试卷及答案解析
- 小班海浪滚滚课件
- 国家中医药管理局《中医药事业发展“十五五”规划》全文
- 2025年全国企业员工全面质量管理知识竞赛题库及答案(共132题) - 副本
- 会计学全套课件第一学期公开课一等奖省优质课大赛获奖课件
- 公开课第一课素描基础入门课件
- 新旧西藏的对比(分析“西藏”)共22张课件
- 杭州市主城区声环境功能区划分图
- 门机防腐施工方案
评论
0/150
提交评论