《山大操作系统课程设计报告(全套).doc》由会员分享,可在线阅读,更多相关《山大操作系统课程设计报告(全套).doc(56页珍藏版)》请在三一办公上搜索。
1、计算机科学与技术学院实验报告:3实验题目:信号量同步问题日期:2010-11-10姓名: 实验目的:在本次实验中,通过使用信号量,在原有的程序框架的基础上添加关键代码实现生产者/消费者同步问题。从而深入理解Nachos的信号量的使用以及实现,生产者/消费者问题是如何用信号量实现的以及在Nachos中是如何创建线程,实现多线程。硬件环境:软件环境:Linux 实验步骤:1.首先初始化三个信号量,代码如下:mutex = new Semaphore(mutux,1);信号量初始化为1,才能起到加锁功能nfull = new Semaphore(full,0);nfull的大小在生产者没生产前为0n
2、empty = 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,
3、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);关键代码:voidProd
4、ucer(_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(fnam
5、e, 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,pr
6、oducer 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的
7、信号量,初值为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); ;
8、调试记录:在源代码中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; Me
9、ssage 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/csc
10、2404/nachos-3.4/code/lab3)结论分析与体会:在本次实验中,实现生产者/消费者同步问题,通过使用信号量,即Nachos 提供的系统调用,进一步理解Nachos的信号量的使用以及实现同时,学会在Nachos中是如何创建线程,实现多线程,理解了多线程的问题。计算机科学与技术学院实验报告:5实验题目:扩展Nachos的文件系统学号:200800130090 日期:2010-11-10姓名:陆思思 Email:实验目的:Nachos的文件系统的文件的大小是不可扩展的:文件被创建后,文件的大小就不能再改变。本次实验的目的即是设计扩展Nachos的文件系统,使得文件的大小是可以被扩展
11、的。这样就可以实现在一个文件尾部或者中间追加文件。硬件环境:软件环境: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;
12、/ 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
13、)return FALSE; for (int i = 0; i tableSize; i+) if (!tablei.inUse) tablei.inUse = TRUE; strncpy(tablei.name, name, FileNameMaxLen); tablei.sector = newSector; return TRUE; return FALSE;/ no space. Fix when we have extensible files.这个是添加一个目录项的方法,当创建一个新文件的时候使用。boolFileSystem:Create(char *name, int ini
14、tialSize)这个是创建一个新的文件,其中主要工作是新建一个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因此,为了实
15、现对文件的追加工作,首先对FileHeader类里面加入新的方法bool AppSectors(BitMap *freeMap, int fileSize);,为了改变一个文件的文件头的大小。2, 实现在一个已有的文件尾部追加新的内容。首先写改变文件头中对文件所在扇区的描述,由AppSectors来实现;该方法将在OpenFile类的对象执行Append File 时被调用。对FileHeader类里面加入新的方法bool AppSectors(BitMap *freeMap, int fileSize);boolFileHeader:AppSectors(BitMap *freeMap, i
16、nt 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 += divRoundU
17、p(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)方法,扩大文件的
18、长度。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(); /
19、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文件中的
20、文件的大小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
21、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
22、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
23、); / 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), TransferSi
24、ze, 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-WriteB
25、ack(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
26、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 smallop
27、enFile 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
28、 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:
29、/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 o
30、f 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, s
31、ent 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:
32、 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
33、 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:7f000000000000000000000000000000000000000000000000000000000000000000000000000000000000
34、0000000000000000000000000000000000000000000Directory file header:FileHeader contents. File size: 200. File blocks:3 4 File contents:10005000small00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
35、00000000000000000000000000000000Bitmap 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
36、.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.
37、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函数分配内存空间其中,要声明一个位试图的是对象,在AddrSp