山大操作系统课程设计报告(全套)

上传人:仙*** 文档编号:34417867 上传时间:2021-10-21 格式:DOC 页数:56 大小:794.72KB
收藏 版权申诉 举报 下载
山大操作系统课程设计报告(全套)_第1页
第1页 / 共56页
山大操作系统课程设计报告(全套)_第2页
第2页 / 共56页
山大操作系统课程设计报告(全套)_第3页
第3页 / 共56页
资源描述:

《山大操作系统课程设计报告(全套)》由会员分享,可在线阅读,更多相关《山大操作系统课程设计报告(全套)(56页珍藏版)》请在装配图网上搜索。

1、 计算机科学与技术学院实验报告:3 实验题目:信号量同步问题 日期:2010-11-10 姓名: 实验目的: 在本次实验中,通过使用信号量,在原有的程序框架的基础上添加关键代码实现生产者/消费者同步问题。从而深入理解 Nachos 的信号量的使用以及实现,生产者/消费者问题是如何用信号量实现的以及 在 Nachos 中是如何创建线程,实现多线程。 硬件环境: 软件环境: Linux 实验步骤: 1.首先初始化三个信号量,代码如下: mutex = new Semaphore(mutux,1);信号量初始化为 1,才能起到加锁功能 nfull = new Semaphore(full,0);nf

2、ull 的大小在生产者没生产前为 0 nempty = 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. 考虑消费者进程,像生产者进程

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_

4、namesi); producersi - Fork(Producer,i); 关键代码:关键代码: void Producer(_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(); void Consumer(_int which) char strMAXLEN; char fn

5、ameLINELEN; 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();

6、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 / 初始化信号量以及需要的生产者消费者线程 /- void ProdCons() int i; DEBUG(t, Enter

7、ing 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); ; /

8、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), 实验结果:实验结果

9、: 生成两个文件分别代表两个消费者取得的产品的记录。 文件 tmp_0 producer id - 0; Message number - 3; 文件 tmp_1 producer 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 -

10、 2; producer id - 1; Message number - 3; 分析结果: 从实验结果中可以看出, 两个消费者取得的产品的顺序和生成者生产的顺序是一致的。结果正确。 (实验所在目录:home/lu/csc2404/nachos-3.4/code/lab3) 结论分析与体会: 在本次实验中,实现生产者/消费者同步问题,通过使用信号量,即 Nachos 提供的系统调用,进一步理解 Nachos 的信号量的使用以及实现 同时,学会在 Nachos 中是如何创建线程,实现多线程,理解了多线程的问题。 计算机科学与技术学院实验报告:5 实验题目:扩展 Nachos 的文件系统 学号:2

11、00800130090 日期:2010-11-10 姓名:陆思思 Email: 实验目的: Nachos 的文件系统的文件的大小是不可扩展的:文件被创建后,文件的大小就不能再改变。 本次实验的目的即是设计扩展 Nachos 的文件系统,使得文件的大小是可以被扩展的。 这样就可以实现在一个文件尾部或者中间追加文件。 硬件环境: 软件环境: Linux 操作系统,Nachos 操作系统 实验步骤: 1, 了解 Nachos 文件系统的结构,为一级目录结构, 其中目录结构以及目录的使用记录保存在文件中。使用 BitMap 来获取空闲的扇区号。 class DirectoryEntry public:

12、 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

13、= size; for (int i = 0; i tableSize; i+) tablei.inUse = FALSE; 这个是目录类,也就是一级目录结构的定义。 bool Directory: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(tablei.name, name, FileNameMaxLen); tablei.secto

14、r = newSector; return TRUE; return FALSE; / no space. Fix when we have extensible files. 这个是添加一个目录项的方法,当创建一个新文件的时候使用。 bool FileSystem:Create(char *name, int initialSize) 这个是创建一个新的文件,其中主要工作是新建一个 FileHeader, 作为一个目录项中保存的 int sector; FileHeader,即文件头,中保存了这个文件的大小,所占的扇区的数目, 以及所占用的全部的扇区号。即: int numBytes; /

15、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 2, 实现在一个已有的文件尾部追加新的内容。首先写改变文件

16、头中对文件所在扇区的描述, 由 AppSectors 来实现;该方法将在 OpenFile 类的对象执行 Append File 时被调用。 对对 FileHeaderFileHeader 类里面加入新的方法类里面加入新的方法 bool AppSectors(BitMap *freeMap, int fileSize);bool AppSectors(BitMap *freeMap, int fileSize); bool FileHeader:AppSectors(BitMap *freeMap, int appFileSize) /如果要追加的文件大小小于等于 0,则直接函数返回 if(a

17、ppFileSize= 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()

18、; return true; printf(The fileheader see filesize %dn,FileLength(); 3 3, 在 openfile.cc 文件中,OpenFile 类中加入方法,用于追加文件的 AppFileSize(int numByte) 。 这个方法首先从文件系统中获取空闲的扇区的位试图文件,构造 BitMap 对象, 传给 FileHeader 类的对象(也就是这个 OpenFile 的文件头) , 执行 AppSectors(BitMap *freeMap, int fileSize)方法,扩大文件的长度。 bool OpenFile:AppFil

19、eSize(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

20、 freeMap-WriteBack(freeMapFile); delete freeMapFile; delete freeMap; return true; 4, 在 fstest.cc 文件中的修改 Append 方法,也就是在 Nachos 运行的时候执行命令, 例如:Nachos x ap ./test/small small 这个方法是 Nachos 系统中本来就提供好的一个方法,只需要在局部修改一下语句, 就可以正确的运行。 void Append(char *from, char *to, int half) /*fileLength 是计算出来的 Linux 文件中的文件的

21、大小 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 len

22、gth 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 one if (!fileSystem-Create(to, 0) printf(Append: couldnt cr

23、eate 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(open

24、File != 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

25、), 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 /需要把该文件的文件头写

26、回磁盘 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 -f No threads ready or runnable, and no pending interrupts. As

27、suming the program completed. Machine halting! Ticks: total 82600, idle 82350, system 250, user 0 Disk I/O: reads 3, writes 5 Console I/O: reads 0, writes 0 Paging: faults 0 Network I/O: packets received 0, sent 0 Cleaning up. 然后执行追加文件的操作:./nachos -ap test/small small luubuntu:/csc2404/nachos-3.4/co

28、de/appfilesys$ ./nachos -ap test/small small openFile see filesize 38 value of start 38 start value: 38, amountRead 10, result of write: 0 start value: 48, amountRead 10, result of write: 0 start value: 58, amountRead 10, result of write: 0 start value: 68, amountRead 8, result of write: 0 inodes ha

29、ve been written back No threads ready or runnable, and no pending interrupts. Assuming the program completed. Machine halting! Ticks: total 99100, idle 98400, system 700, user 0 Disk I/O: reads 16, writes 6 Console I/O: reads 0, writes 0 Paging: faults 0 Network I/O: packets received 0, sent 0 Clean

30、ing up. 再执行从文件中部追加文件的操作:./nachos -hap test/small small luubuntu:/csc2404/nachos-3.4/code/appfilesys$ ./nachos -hap test/small small openFile see filesize 76 value of start 38 start value: 19, amountRead 10, result of write: 10 start value: 29, amountRead 10, result of write: 10 start value: 39, amou

31、ntRead 10, result of write: 10 start value: 49, amountRead 8, result of write: 8 inodes have been written back No threads ready or runnable, and no pending interrupts. Assuming the program completed. Machine halting! Ticks: total 83100, idle 82470, system 630, user 0 Disk I/O: reads 14, writes 6 Con

32、sole I/O: reads 0, writes 0 Paging: faults 0 Network I/O: packets received 0, sent 0 Cleaning up. luubuntu:/csc2404/nachos-3.4/code/appfilesys$ ./nachos -hap test/small small openFile see filesize 76 value of start 38 start value: 19, amountRead 10, result of write: 10 start value: 29, amountRead 10

33、, result of write: 10 start value: 39, amountRead 10, result of write: 10 start value: 49, amountRead 8, result of write: 8 inodes have been written back No threads ready or runnable, and no pending interrupts. Assuming the program completed. Machine halting! Ticks: total 83100, idle 82470, system 6

34、30, user 0 Disk I/O: reads 14, writes 6 Console I/O: reads 0, writes 0 Paging: faults 0 Network I/O: packets received 0, sent 0 Cleaning up. 最后执行“显示“操作,即显示 Nachos 磁盘当前的内容: luubuntu:/csc2404/nachos-3.4/code/appfilesys$ ./nachos -D Bit map file header: FileHeader contents. File size: 128. File blocks:

35、 2 File contents: 7f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 Directory file header: FileHeader contents. File size: 200. File blocks: 3 4 File contents: 10005000small00000000000000000000000000000000000000000000000

36、00000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000 Bitmap set: 0, 1, 2, 3, 4, 5, 6, Directory contents: Name: small, Sector: 5 FileHeader contents. File size: 76. File blocks: 6 File contents: This is the spri

37、ng This is the spring of our discontent.aof our discontent.a No threads ready or runnable, and no pending interrupts. Assuming the program completed. Machine halting! Ticks: total 6500, idle 6090, system 410, user 0 Disk I/O: reads 12, writes 0 Console I/O: reads 0, writes 0 Paging: faults 0 Network

38、 I/O: packets received 0, sent 0 Cleaning up. (实验所在目录:csc2404/nachos-3.4/code/appfilesys 主要修改文件,filehdr.cc,openfile.cc,fstest.cc,filehdr.h,openfile.h) 结论分析与体会: 通过增加 Nachos 文件系统对文件的追加功能,理解了文件系统的管理方式,通过目录, 目录项,文件头,进一步理解了文件的存储方式,以及空闲磁盘空间的管理,使用位试图 的方式管理空闲的磁盘空间。 返回 计算机科学与技术学院实验报告:6,7,8 实验题目:地址空间的扩展,实现系统调

39、用 Exec,Exit 日期:2010-11-5 Email: 实验目的: 扩展现有的 class AddrSpace 的实现,使得 Nachos 可以实现多用户程序,完成系统调用 Exec 使 Nachos 按页分配内存空间 硬件环境: 软件环境: Linux,Nachos,mips 实验步骤: 1. 首先编写实验 6 的 Print 函数,为了可以显示内存空间的分配。以下为 Print 函数的实现: 2. 编写一个 exec.c 的源码,然后编译生成一个可执行文件,exec.noff exec.c 的源码: 3. Nachos 原来实现 AddressSpace 分配的时候没有使用 bit

40、map 来寻找空闲页,而是直接的从 0 号内存空间 开始分配,因而需要修改成使用 bitmap 的 find 函数分配内存空间 其中,要声明一个位试图的是对象,在 AddrSpace 中 并且数据段代码段按页存储。 4. 计算加载一个程序需要的页表的数目 5. 实现 Nachos 的系统调用,采用的是触发异常中断的,在 userprog/exception.cc,添加 SC_Exec 异常, 存放要执行的文件的路径的字符串的首地址存放在 4 号寄存器中,因此可以通过这个方式找到文件的路径, 从而使用文件系统提供的方法打开这个文件: 6. 结果分析: 编译成功之后,输入命令 ./nachos x

41、 ./test/Exec.noff 运行结果: luubuntu:/csc2404/nachos-3.4/code/userprog$ ./nachos -x ./test/exec.noff page table dump: 12 pages in total = VirtPage, PhysPage 0, 0 1, 1 2, 2 3, 3 4, 4 5, 5 6, 6 7, 7 8, 8 9, 9 10, 10 11, 11 = the allocated physical Address:0 he address in the executable file :40 copy size

42、128 the allocated physical Address:128 he address in the executable file :168 copy size 128 the allocated physical Address:256 he address in the executable file :296 copy size 128 SC_Exec in exceptionHandler The value of the register 4 is, .i.e. the address of the String 272 add content of fn is now

43、: 742f2e2e the char view is . . / t add content of fn is now: 2f747365 the char view is e s t / add content of fn is now: 746c6168 the char view is h a l t add content of fn is now: 666f6e2e the char view is . n o f add content of fn is now: 66 the char view is f add content of fn is now: 0 the char

44、 view is add content of fn is now: 0 the char view is add content of fn is now: 0 the char view is add content of fn is now: 0 the char view is add content of fn is now: 0 the char view is Exec file is ./test/halt.noff page table dump: 11 pages in total = VirtPage, PhysPage 0, 12 1, 13 2, 14 3, 15 4

45、, 16 5, 17 6, 18 7, 19 8, 20 9, 21 10, 22 = the allocated physical Address:1536 he address in the executable file :40 copy size 128 the allocated physical Address:1664 he address in the executable file :168 copy size 128 the allocated physical Address:1792 he address in the executable file :296 copy

46、 size 128 Machine halting! Ticks: total 50, idle 0, system 10, user 40 Disk I/O: reads 0, writes 0 Console I/O: reads 0, writes 0 Paging: faults 0 Network I/O: packets received 0, sent 0 Cleaning up. luubuntu:/csc2404/nachos-3.4/code/userprog$ 加载进来的程序的地址空间没有覆盖原来那个程序的地址空间,使用新的地址空间去执行关机操作 Machine halt

47、ing! 实现了一个程序加载另一个程序运行。 (实验所在路径: csc2404/nachos-3.4/code/ multiProcess 主要修改的文件,addrspace.cc exception.cc ) 结论分析与体会: Nachos 之前没有实现按页分配地址空间,物理页和逻辑页地址一致,而且数据段代码段连续分配 每当一个新的用户程序建立的时候,虚拟地址和物理地址都是从 0 开始分配,这样新的用户程序 将会冲掉原来在物理 0 开始的程序。 因而使用位示图分配物理地址。使用 bitmap 的 find 函数分配虚存对应的物理地址,在为数据段和 代码段写入数据的时候是以扇区为单位的,而不是

48、原有的连续一个文件的读入连续的内存。 Nachos 操作系统通过中断的方式实现系统调用。需要增加 userprog/exception.cc 中的内容,即 必须在此类中添加处理 Exec 的方法。 返回 计算机科学与技术学院实验报告:9 实验题目:设计并实现具有优先级的线程调度策略 日期:2010-11-13 Email: 实验目的: Nachos 系统采用基本的 FCFS 的线程调度策略,修改成为具有优先级的线程调度策略 硬件环境: 软件环境: linux 操作系统,Nachos 操作系统 实验步骤: 1修改 Thread 类的构造函数,加入优先级 priority 属性,并且加入 getP

49、rioty 方法。以便在线程的 等待队列中找到优先级最高的线程。其中,线程优先级的范围从 1 到 7,默认为 7,即最低优先级。 修改代码如下: (thread.cc,thread.h) class Thread public: Thread(char* debugName, int priority=7);/ initialize a Thread int getPriority()return this-priority; Thread:Thread(char* threadName, int p) if(p7) priority = 7; else priority = p; name

50、= threadName; stackTop = NULL; stack = NULL; status = JUST_CREATED; #ifdef USER_PROGRAM space = NULL; #endif 2,首先,了解 Nachos 系统原来的线程调度方式。通过读 threadtest.cc,thread.cc,scheduler.cc 文件 中的内容了解线程调度的方式。 首先,修改 threadtest.cc 文件中的 ThreadTest 方法,创建有优先级的 Thread,代码如下: 然后,从 Thread 类中找到 Fork 方法,代码如下: 这说明 ThreadTest

51、 方法的目的是,实例化新的线程,调用 Fork 方法,也就是让新产生的线程去执行 SimpleThread 方法,并且把 当前执行的线程加入到等待队列。 从 SimpleThread 的定义中可以知道,新生产的线程就是打印一条信息然后去执行 Yield(); 通过查看 yield,可知,先从等待队列中找到一个线程保留在 nextThread 中,并将当前线程加到等待队列,然后使 nextThread 运行 void Thread:Yield () Thread *nextThread; IntStatus oldLevel = interrupt-SetLevel(IntOff); ASSER

52、T(this = currentThread); DEBUG(t, Yielding thread %sn, getName(); nextThread = scheduler-FindNextToRun(); if (nextThread != NULL) scheduler-ReadyToRun(this); scheduler-Run(nextThread); (void) interrupt-SetLevel(oldLevel); 3, 为了实现按优先级调度,需要按优先级选择等待队列中的,即状态为 ready 的线程,因而,在线程插入, 移除等待队列的时候使用 List 类中提供好的

53、SortedInsert(void *item, int sortKey) SortedRemove(int *keyPtr) 方法,而不是原来使用的 Append,Remove 方法; void Scheduler:ReadyToRun (Thread *thread) DEBUG(t, Putting thread %s on ready list.n, thread-getName(); thread-setStatus(READY); readyList-SortedInsert(void *)thread, thread-getPriority(); /- Thread * Sche

54、duler:FindNextToRun () int p; return (Thread *)readyList-SortedRemove(&p); 4, 结果显示: luubuntu:/csc2404/nachos-3.4/code/threadsWithPrioty$ ./nachos * thread 2 looped 0 times * thread 4 looped 0 times * thread 2 looped 1 times * thread 2 looped 2 times * thread 4 looped 1 times * thread 2 looped 3 time

55、s * thread 4 looped 2 times * thread 2 looped 4 times * thread 4 looped 3 times * thread 4 looped 4 times * thread 5 looped 0 times * thread 5 looped 1 times * thread 5 looped 2 times * thread 3 looped 0 times * thread 5 looped 3 times * thread 3 looped 1 times * thread 5 looped 4 times * thread 3 l

56、ooped 2 times * thread 1 looped 0 times * thread 3 looped 3 times * thread 1 looped 1 times * thread 3 looped 4 times * thread 1 looped 2 times * thread 1 looped 3 times * thread 1 looped 4 times No threads ready or runnable, and no pending interrupts. Assuming the program completed. Machine halting

57、! Ticks: total 400, idle 10, system 390, user 0 Disk I/O: reads 0, writes 0 Console I/O: reads 0, writes 0 Paging: faults 0 Network I/O: packets received 0, sent 0 Cleaning up. 5, 结果分析: 可以看出,线程执行的顺序不是在 ThreadTest 中的生成 Thread(1,2,3,4,5)的顺序,而是按照优先级的顺序 调度的。首先从等待队列中找一个优先级最高的线程,然后把当前运行的线程加入到等待队列中,然后再运行 (

58、本次实验所在所在位置:csc2404/nachos-3.4/code/threadsWithPrioty) 结论分析与体会: 通过这个实验,为了实现线程优先级的调度,细致分析了 Thread 目录下的各个文件的内容,尤其是 thread.cc, Scheduler.cc,文件中对于线程的状态,以及调度函数的实现等问题上面有了更深一步的了解。从而,知道如何 修改可以实现线程的优先级调度问题,也就是管理一个 SortedList,其内容是状态为 Ready 的线程。 由概念到实践,了解了简单的线程调度的实现。 返回 计算机科学与技术学院实验报告:10 实验题目:具有二级索引的文件系统 学号:200

59、800130090 日期:2010-11-18 姓名:陆思思 Email: 实验目的: Nachos 的文件系统中保存文件内容所在扇区索引的“文件头“目前只占用一个扇区, 为了可以使 Nachos 文件系统创建较大的文件,将”文件头”扩大到两个扇区,也就是实现二级索引。 硬件环境: 软件环境: Linux 操作系统,Nachos 操作系统 实验步骤: 1, 通过实验 5 的扩展文件大小的实验,了解了 nachos 系统的对文件系统的管理。本次实验的目的主要是 扩大 Nachos 系统可以创建的文件的大小,使用两个扇区来保存文件头的信息。 为了达到这个目的,首先了解 nachos 自带的文件系统

60、的文件头的结构: 保存在一个扇区中,第一个 int 保存了文件的字节数(numBytes) ,第二个 int 保存了使用的扇区数 (numSectors) ,第三个数据结构是文件所在的各个扇区号(dataSectorsNumDiresct) 。 也就是说,Nachos 系统采用的是索引式的文件管理方式。 因而,要实现 nachos 文件系统的二级索引,可以使第一个索引节点(也就是原有的文件头那个扇区)的 dataSectors 数组的最后一个元素保留第二个索引节点(也就是第二个扇区)的引用(也就是扇区号) 。 如果文件大小不超过一个索引节点可以保留的内容,则这个最后一个元素的值为-1。 2,

61、通过分析可知,需要修改 filehdr.cc 中的内容。 代码如下: boolbool FileHeader:Allocate(BitMap *freeMap, int fileSize)FileHeader:Allocate(BitMap *freeMap, int fileSize) numBytes = fileSize; numSectors = divRoundUp(fileSize, SectorSize); if (freeMap-NumClear() numSectors) return FALSE; / not enough space /*如果文件大小超过索引节点中保存扇区

62、号的数目,则返回 false*/ else if(NumDirect + NumDirect2 = numSectors) return FALSE;/the filesys cannot support such big file /*toNextNode 是保留第二个索引节点的扇区号*/ int toNextNode=NumDirect-1; /toNextNode is the Sector number of the second node of the filehdr /if the second node is not needed, then dataSectortoNextNo

63、de=-1 if(numSectors toNextNode) for (int i = 0; i Find();/为文件分配扇区 dataSectorstoNextNode = -1; /If the numSectors excends the rage of dataSectors, else for (int i = 0; i Find(); dataSectorstoNextNode = freeMap-Find();/找一个空闲的扇区,作为第二个扇区,索引节点 /this is the content,i.e.filehdr of the allocated sectors, of

64、 the second node int dataSectors2NumDirect2; for (int i = 0; i Find();/为文件分配扇区 /the fefault synchDisk-WriteSector do not include the second node /so write back the new build node synchDisk-WriteSector(dataSectorstoNextNode, (char *)dataSectors2); return TRUE; /*revised*/ void void FileHeader:Dealloc

65、ate(BitMap *freeMap)FileHeader:Deallocate(BitMap *freeMap) /*toNextNode 是保留第二个索引节点的扇区号*/ int toNextNode= NumDirect - 1; / test if has the second node if(dataSectorstoNextNode=-1) for (int i = 0; i Test(int) dataSectorsi); / ought to be marked! freeMap-Clear(int) dataSectorsi); /has a second node, th

66、en find it, then clean the bitmap, then else /clear the first n-1 bit,remain the toNextNode int i=0; for ( ; i Test(int) dataSectorsi); / ought to be marked! freeMap-Clear(int) dataSectorsi); int dataSectors2NumDirect2; synchDisk-ReadSector(dataSectorstoNextNode, (char *)dataSectors2); freeMap-Clear(int) dataSectorstoNextNode);/clear the toNextNode for( ; i Clear(int) dataSectors2i-toNextNode);/toNextNode=the number of filehdr items of the first node intint FileHeader:ByteToSector(int offset)Fil

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

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!