首页 > 其他分享 >CSC3150 memory-mapped files

CSC3150 memory-mapped files

时间:2024-11-13 17:57:59浏览次数:1  
标签:files CSC3150 mmap memory virtual should file mapped your

CSC3150-Instruction-A3:

Introduction

This assignment uses xv6, a simple and Unix-like teaching operating system, as the platform toguide you in implementing the mmap and munmp system calls. These two are used to sharememory among proceses and to map files to process address spaces. Generally speaking, thisssignment focuses on memory-mapped files. A mechanismsupporting memory-mapped filesan handle files as if they are a portion of the program's memory. This is achieved by mapping a

ie to a segment of the virtual memory space (Reminder: Each process has its own virtual addressspace). Such mapping between a file and memory space is achieved using the mmap() system call,and the mapping is removed using the munmap() system call. We provide a virtual machine imagewhere everything is configured and set. The image is available on Blackboard.

submission

Due on: 23:59, 13 November, 2024

Plagiarism is strictly forbidden. Please note that TAs may ask you to explain the meaning ofyour program to ensure that the codes are indeed written by yourself. Please also note thatwe would check whether your program is too similar to your fellow students' code andsolutions available on the internet using plagiarism detectors.Late submission: A late submission within 15 minutes will not induce any penalty on yourgrades. But 00:16 am-1:00 am: Reduced by 10%; 1:01 am-2:00 am: Reducedby 20%; 2:01

am-3:00 am: Reduced by 30% and so on. (e.g. Li Hua submit a perfect attemp ofassignment3 on 2:10 am. He will get (100+10 (bonus)) * 0.7 = 77 points for his assignment3.)You should submit a zip file to the Blackboard. The zip file structure is as follows.Format guide

s fine. Structure mismatch would cause grade deduction.For this assignment, you don't need a specific folder for the extra credit part. The source folder

should contain four files: proc.c, proc.h, sysfile.c, trap.c Please compress all files in the file structure root folder into a single zip file and name it using your student ID as the code shown below and above, for example, Assignment_3_xxxxxxxxx.zip. The report should be submitted in the format of pdf, together withyour source code. Format mismatch would cause grade deduction. Hereis the sample step forcompressing your code.main@ubuntu:~/Desktop/Assignment_3_120010001$lsReport.pdf source/

(One directory and one pdf.)main@ubuntu:~/Desktop/Assignment_3_120010001/source$ lsproc.c proc.h sysfile.c trap.c(three .c files and one .h file)Tips on interactions between host and virtual Here are some useful tips for you to interact between the host machine and the virtual machine. Ifou are familiar with it and "Format guide", you can ignore this section.

 the terminal, you should not include "<" and ">". Here, they are just to present a custom string

variable.

  1. Copy the assignment folder to your virtual machine. You can copy the folder in thVSCode or use the scp command below.$\newline$

In the host machine:

$\newline$

If you have spaces in the path, use the double quote to include your path, e.g. cd "your host

path" .

  1. Unzip the assignment folder in your virtual machine.

$\newline$In the virtual machine:Then, 代写CSC3150 memory-mapped files you can browse the assignment folder.After finishing the project, you should wrap your file following the format instructions.Weprepare a script for you to generate the submission zip. This optional script is just for yourconvenience to wrap the files. You can wrap your file in your own way, only ensuring that youfollow the format.

  1. Suppose that you have already copied your Report.pdf to the virtual machine (like the way

you copy the assignment zip from the host machine to the virtual machine).In the virtual machine:gen_submission.sh script will ask for your student id and path of your Report.pdf .

Then you can find your submission folder under ~/csc3150

project3/submission/Assignment_3_<your_student_id>.zip

  1. You can use the following command to copy the submission zip to your host machine.

main@ubuntu:~/Desktop$

zip -q -r Assignment_3_xxxxxxxxx.zip Assignment_3_xxxxxxxxx

main@ubuntu:~/Desktop$ ls

Assignment_3_xxxxxxxxx Assignment_3_xxxxxxxxx.zip

cd <your_host_path_to_project_zip>scp -P 2200 ./csc3150-project3.zip [email protected]:~unzip ~/csc3150-project3.zip ~/chmod -R +x ~/csc3150-project3cd ~/csc3150-project3bash gen_submission.shIn the host machine:Then you will get the submission zip inyour_host_machine_folder_path . Don't forget to submit your zipfile to the BlackBoard.Instrction Guideline We limit your implementation within proc.c, proc.h, sysfile.c, trap.c four files, where there aresome missing code sections starting with "TODO" comments. The entry(where you may start

learning) of the test program is the main function in mmaptest.c under the 'csc3150-project3/user'

directory.Sections with (*) are introduction sections. These sections introduce tools and functions that willhelp you understand what this system is about and how the system workswith thesecomponents. You might need to use some of the functions when implementing theTODO parts.You are ONLY allowed to modify the TODO parts in these four files! And we will grade your project

ONLY based on the implementation of the TODO parts. Any other modification will beconsidered invalid.

For the introduction sections, please figure out how functions work and how to use them.

  1. Be sure you have a basic idea of the content before starting your assignment. We believe thatthose would be enough for handling this assignment.
  1. (optional) For students who are interested in the xv6 system and want to learn more about it,you are welcome to read "xv6-book" to get more details.
  1. https://pdos.csail.mit.edu/6.828/2022/xv6/book-riscv-rev3.pdfgiven places.
  1. However, no sample code will be shown here. You need to figure out the implementation

ased on the logic and APIs provided in the introduction sections.

Arguments fetching*

<xv6-book> chapter 4.3The kernel functions argint , argaddr , and argfd retrieve the nth system call argument fromthe trap frame as an integer, pointer, or file descriptor. They all call argraw toretrieve theappropriate saved user register (kernel/syscall.c:34).scp -P 2200 [email protected]:~/csc3150-

short nlink;uint size;uint addrs[NDIRECT+1];};// Write to file f.// addr is a user virtual address.int filewrite(struct file *f, uintaddr, int n);// Increment ref count for file f.struct file* filedup(struct file*);// Close file f. (Decrement ref count, close when reaches 0.)

void fileclose(struct file*);Struct "file" "inode" is presented for your information.

filewrite() will be invoked to write back when the memory map is over. i.e. Calling munmap orCalling exit of process. Similarly to fileclose() .filedup() will be invoked when there is an increment of accessing file. ( mmap() , fork() )// Defined in fs.c// Read data from inode.// Caller must hold ip->lock.// If user_dst==1, then dst is a user virtual address;// otherwise, dst is a kernel address.

int readi(struct inode *ip, int user_dst, uint64 dst, uint off, uint n);// Write data to inode.// Caller must hold ip->lock.// If user_src==1, then src is a user virtual address;// otherwise, src is a kernel address./ Returns the number of bytes successfully written./ there was an error of some kind.nt writei(struct inode *ip, int user_src, uint64 src, uint off, uint n);// Lock the given inode.// Reads the inode from disk if necessary.void ilock(struct inode *ip);// Unlock the given inode.void iunlock(struct inode *ip);Function that you need to use when handling page fault, pay attention to how readi()works andfigure out the parameter you should send to readi() .If you have no idea what readi() is doing, think about read() or memcpy(), whichdeal with pointers and address.imilarly as `writei()`ilock() and iunlock() are locks of inode, which are used to ensure consistency of the memory.Hint You may take a look at sys_open() to know how inode, file, and locks work.(TODO) VMA Struct

Explanation

The VMA (Virtual Memory Area) struct is used to manage and track the memory regions that are

mapped into the address space of a process. Each VMA represents a contiguous region of virtual

memory that has the same permissions and is backed by the same kind of object. The

operating system needs to keep track of these mappings, including where they start, how large

they are, what permissions they have, and what file or device they're associated with. This iswhat the vma struct is used for.Implementation Keep track of what mmaphas mapped for each process.Define a structure corresponding to the VMA (virtual memory area), recording the address,length, permissions, file, etc. for a virtual memory range created by mmap.

Since the xv6 kernel doesn't have a memory allocator in the kernel, it's OK to declare a fixedsize array of VMAs and allocate from that array as needed. A size of 16 should be sufficient. (Ialready define VMASIZE for you)Hint Take a look at what parameter will be sent into mmap() .The VMA should contain a pointer to a struct file for the file being mapped;If you would like to use more variablesin VMA for further implementation, feel free to use them.There is not only one correct answer.

(TODO) mmap() // we already define size of VMA array for you

#define VMASIZE 16// TODO: complete struct of VMAstruct VMA {};// Defined in user.hvoid *mmap(void *addr, size_t length, int prot, int flags, int fd, off_toffset);// TODO: kernel mmap executed in sysfile.csys_mmap(void){}Arguments explanation:

In the mmaptest.c, we call 'char p = mmap(0, PGSIZE2, PROT_READ, MAP_PRIVATE, fd, 0);'.

This call asks the kernel to map the content of file 'fd' into the address space. The first '0'

argument indicates that the kernel should choose the virtual address (In this homework, you

can assume that 'addr' will always be zero).The second argument 'length' indicates how many bytes to map.The third argument 'PROT_READ' indicates that the mappedmemory should be read-only,i.e.,modification is not allowed.The fourth argument 'MAP_PRIVATE' indicates that if the process modifies then mappedmemory, the modification should not bewrittenback to the file nor shared with otherprocesses mapping the same file (of course, due to PROT_READ, updates are prohibited inhis case).The fifth argument is the file description of the file to be mapped.The last argument 'offset' is the starting offset in the file.The return value indicates whethermmap succeeds or not.sys_xxx() function is the kernel's implementation of the xxx() system call. In the xv6operating system, system calls are prefixed with sys_ to distinguish them from otherfunctions and to indicate that they are system calls. The kernel functions argint ,argaddr ,

and argfd retrieve the n'th system call argument from the trap frame as an integer, pointer,or a file descriptor. See the Arguments fetching section.Run mmaptest after mmap() implemented: the first mmap should succeed, but the firstaccess to the mmap-edmemory will cause a page fault and kill mmaptest.Before mmap() implementedPage fault occurs after mmap() implemented(work correctly)Progress chart

(TODO) PageFault Handle

<xv6-book>chapter 4.5,4.6

Add code to cause a page-fault in a mmap-ed region to allocate a page of physical memory.Find corresponding valid vma by fault address.

Read 4096 bytes of the relevant file onto that page, and map it into the user address space.Read the file with readi, which takes an offset argument at which to read inthefile (but you

will have to lock/unlock the inode passed to readi).Set the permissions correctly on the page. Run mmaptest; it should get to the first munmap.See Section Trap

(TODO) munmap()

Implement munmap:find the VMA for the address range and unmap the specified pages (hint: useuvmunmap).If munmap removes all pages of a previous mmap, it should decrease the referencecount of the corresponding struct file.If an unmapped page has beenmodified and the file is mapped MAP_SHARED, write thepage back to the file. Look at filewrite for inspiration.

Ideally your implementation would only write back MAP_SHARED pages that theprogram actually modified. The dirty bit (D) in the RISC-V PTE indicates whether a pagehas been written. However, mmaptest does not check that non-dirty pages are notwritten back; thus, you can get away with writing pages back without looking at D bits.

(TODO) Page Alignment

This is a reminder to raise your awareness that all the virtual addresses in your kernelimplementation should be page-aligned! It's very important to keep this rule in realimplementation. That is to say, wrap the addresses with PGROUNDUP or PGROUNDOWN underdifferent situations. You have to figure out which to use.

(EXTRA CREDITS) Fork Handle

In your Assignment 1, you should already know that fork() creates a sub process with thesame info. Therefore, you should handle how mmap() works when fork() is invoked.Ensure that the child has the same mapped regions as the parent. Don't forget to incrementthe reference count for a VMA's struct file. In the page fault handler of the child, it is OK toallocate a new physical page instead of sharing a page with the parent. The latter  be

cooler, but it would require more implementation work.Grading Rules

Program part 90' + extra credits

You can test the correctness of your code using the following commands under ~/csc3150-project3 directory.

// TODO: complete munmap()

fork_test OK: all tests succeededReport part 10' You shall strictly follow the provided latex template for the report, where we have emphasized

important parts and respective grading details.Reports based on other templates will not be graded.

LaTex Editor For your convenience, you might use Overleaf, an online LaTex Editor.

  1. Create a new blank project.
  2. Click the following highlight bottom and upload the template we provide.
  3. Click Recompile and you will see your report in PDF format.

标签:files,CSC3150,mmap,memory,virtual,should,file,mapped,your
From: https://www.cnblogs.com/comp9321/p/18543435

相关文章

  • ffmpeg Preset files
    Apresetfilecontainsasequenceofoption=valuepairs,oneforeachline,specifyingasequenceofoptionswhichwouldbeawkwardtospecifyonthecommandline.Linesstartingwiththehash(’#’)characterareignoredandareusedtoprovidecomments.......
  • 前端使用 jszip.js 和 FileSaver.js 下载并压缩文件
    asyncexport_data(){letzip=newJSZip()//下载文件并添加到ZIPfor(constiofthis.tableData){constdata=awaitfetch(i.path).then(response=>response.arrayBuffer())constimageByteStream=newUint8Array(data).subarray(1024)......
  • 三周精通FastAPI:31 使用 StaticFiles从目录中自动提供静态文件
    官方文档:静态文件-FastAPI静态文件¶您可以使用 StaticFiles从目录中自动提供静态文件。使用StaticFiles¶导入StaticFiles。"挂载"(Mount)一个 StaticFiles() 实例到一个指定路径。fromfastapiimportFastAPIfromfastapi.staticfilesimportStaticFilesapp......
  • 【已解决】vmware+ubunt14,编译海思3798MV100 ,HiSTBLinuxV100R005C00SPC050-master,报f
    于2023-07-1609:49:36发布没看懂,不知道问题出在哪里make[1]:Enteringdirectory/home/andy1231/Downloads/HiSTBLinuxV100R005C00SPC050-master/tools/linux/utils'make[1]:Enteringdirectory/home/andy1231/Downloads/HiSTBLinuxV100R005C00SPC050-master/source/kern......
  • Scifi Modern flat themed GUI UI kit - over 700 PNG files PSD AI sources
    一款完整的现代主题UI套件,专为科幻游戏和应用设计。主要特点包括:六种颜色主题的窗口,涵盖暂停、设置、商店、库存、任务等多个功能页面。提供138个独特的项目和图标,支持5种尺寸(32x32至512x512像素)。丰富的按钮类型,包括常规按钮、轮廓按钮、带图标按钮等,每种按钮有2-3......
  • ECE 4122/6122 OpenGL with OBJ files and Multiple Objects
    ECE4122/6122Lab3:OpenGLwithOBJfilesandMultipleObjects(100pts)Category:3DGraphicsDue:TuesdayOctober22th,2023by11:59PMObjective:Tocreateadynamic3Dgraphicsapplicationusinglighting,shading,modeltransformations,andkeyboa......
  • java 文件的操作(Path、Paths、Files)
    Path、Paths和Files是JavaNIO(NewI/O)文件处理系统中的核心组件,它们提供了比传统java.io.File更加灵活和高效的文件操作方式。1.概述随着Java7引入NIO.2(即JavaNewI/O2),文件处理得到了显著改进。Path、Paths和Files是NIO.2中用于文件和目录操作的三个关键组件......
  • java 文件的操作(Path、Paths、Files)
    Path、Paths和Files是JavaNIO(NewI/O)文件处理系统中的核心组件,它们提供了比传统java.io.File更加灵活和高效的文件操作方式。1.概述随着Java7引入NIO.2(即JavaNewI/O2),文件处理得到了显著改进。Path、Paths和Files是NIO.2中用于文件和目录操作的三个关......
  • jar包内替换依赖jar后无法启动,错误日志:It has been compressed and nested jar files
    jar包内替换依赖jar后无法启动,错误日志:Ithasbeencompressedandnestedjarfilesmustbestoredwithoutcompression.ruoyi、springboot、java、jar、libs、压缩背景某服务jar包足足90MB有余,远程传输太慢,目前在改动的是其中的某子jar(项目内部依赖,另一个jar)。之前......
  • jar包内替换依赖jar后无法启动,错误日志:It has been compressed and nested jar files
    jar包内替换依赖jar后无法启动,错误日志:Ithasbeencompressedandnestedjarfilesmustbestoredwithoutcompression.ruoyi、springboot、java、jar、libs、压缩背景某服务jar包足足90MB有余,远程传输太慢,目前在改动的是其中的某子jar(项目内部依赖,另一个jar)。之前......