Use C++ to develop a simple file system using the following properties listed be
ID: 3793534 • Letter: U
Question
Use C++ to develop a simple file system using the following properties listed below.
Overview
In this project you will develop a simple File System. T
he FS should operate on the simulated persistent memory with following properties:
• 20 Sectors, 64KB each
• Erase operation can be performed either on the single sector or on the whole simulated memory. Erase operation sets all bits of the sector(s) to value 1.
• Write operation can be performed only in WORDs and on a WORD boundary (WORD is defined as 2 bytes). Write operation can set bits to 0, but cannot set bits to 1, i.e., it is equal to the AND operation between the WORD on the simulated memory and the WORD to be written.
• Addressing is “flat” beginning with the 0 byte of the first sector Part 1 Implement following “driver” operations (functions) operating on a simulated memory device:
Part 1
Implement following “driver” operations (functions) operating on a simulated memory device:
Reads a WORD (2 bytes) from the specified address.
• nAddress – address of the WORD to be read. Address is the offset from the beginning of the simulated memory (i.e., byte 0 in Sector 0) and not from the particular sector. Address should be on the WORD boundary, i.e., addresses 0, 2, …, 2n are valid, addresses 1, 3, …, 2n-1 are not. If specified address is not WORD aligned or is outside the size of the simulated medium, this operation should fail.
Writes a WORD (2 bytes) to the specified address.
• nAddress – address in which the WORD should be written
• nWord – WORD to be written in the specified address If specified address is not WORD aligned or is outside the size of the simulated memory, this operation should fail, i.e., no information should be written in the simulated memory. If address is valid, the value written in the specified address should be equal to nWord AND ReadWord (nAddress) Whether this function returns value or not and what is the meaning of the returned value is a decision you should make.
Return values: It is your responsibility to decide whether these functions are void or return value, and which one in which case.
All these functions should be implemented using existing file operations provided by C/C++ and/or Windows API. The memory should be simulated using a binary file.
Note: if the file that simulates “physical medium” does not exist, you should create it whenever any of the “driver” functions is called.
Function Description EraseAllSectors () Sets all bits in all sectors in simulated memory to value 1. If necessary, create the file simulating the medium. EraseSector (nSectorNr) Sets all bits in the specified sector to 1. Sector numbers are 0-19. If file simulating simulated memory is not present, EraseAllSectors () is executed first ReadWord (nAddress)Reads a WORD (2 bytes) from the specified address.
• nAddress – address of the WORD to be read. Address is the offset from the beginning of the simulated memory (i.e., byte 0 in Sector 0) and not from the particular sector. Address should be on the WORD boundary, i.e., addresses 0, 2, …, 2n are valid, addresses 1, 3, …, 2n-1 are not. If specified address is not WORD aligned or is outside the size of the simulated medium, this operation should fail.
WriteWord (nAddress, nWord)Writes a WORD (2 bytes) to the specified address.
• nAddress – address in which the WORD should be written
• nWord – WORD to be written in the specified address If specified address is not WORD aligned or is outside the size of the simulated memory, this operation should fail, i.e., no information should be written in the simulated memory. If address is valid, the value written in the specified address should be equal to nWord AND ReadWord (nAddress) Whether this function returns value or not and what is the meaning of the returned value is a decision you should make.
In this project you wildevelop asimple Fie system. The FS should operate on the imulated persistent memory with felowingproperties: 20 sectors, Beach Erase operation can be performed either on thesinde sector or on the whole simulated memory. Erase operation sets albits ofthe sector to value 1. Write operation can be performed only in WORDs and on a WORD boundary WORD is as 2bytesl Writeoperation can sett bits toa butannot set bitsto l, ie, tisequal tothe AND operation between the WORDonthe simulated memory and the WORD to bewritten. Addressing flat" beginning with the Obyte the sector Implement following driver operations functions operatingonasimulated memory devices Erase Alisectors0 necessary create these mating the medium Sets allbits in the specified sector to 1.Sector numbers are 0-19. iffie simulating memory mot present, EraseAlSectors Read word InAddressI Reads awORD (2 bytes from the specified address. of the WORD to be read. Address is the offset from the beginning of the simulated memory byte Sector and not from the particular sector. Address should be on the WORD boundary. Le addresses 2, specified address is not wORD aligned or is outside the siae of the simulated medium thi operation should fail WritewordInAddress.nlWordl Writes a WORDRbytes to thespecified address rWord-WORD to be written in the specified address specified address is not WORDalgned or is outside the sate of the simulated memory this operation should fal Le noinformation should be writteninthe simulated memory address is vaid the value written in the specified address should Whether this function retumsvalue or not and what is the meaning of the returned value a Return values: It is your responsibility to decide whether these functions are voidor retum values and All these functions should be implemented using eitingfie operations provided by and/or Windows APLThe memory should be simulated inga Note: the file that simulates"physicalmedium does not exist youshouldereate it whenever any ofExplanation / Answer
Perhaps you are already using C++ as your main programming language to solve TopCoder problems. This means that you have already used STL in a simple way, because arrays and strings are passed to your function as STL objects. You may have noticed, though, that many coders manage to write their code much more quickly and concisely than you.
Or perhaps you are not a C++ programmer, but want to become one because of the great functionality of this language and its libraries (and, maybe, because of the very short solutions you’ve read in TopCoder practice rooms and competitions).
Regardless of where you’re coming from, this article can help. In it, we will review some of the powerful features of the Standard Template Library (STL) – a great tool that, sometimes, can save you a lot of time in an algorithm competition.
The simplest way to get familiar with STL is to begin from its containers.
Containers
Any time you need to operate with many elements you require some kind of container. In native C (not C++) there was only one type of container: the array.
The problem is not that arrays are limited (though, for example, it’s impossible to determine the size of array at runtime). Instead, the main problem is that many problems require a container with greater functionality.
For example, we may need one or more of the following operations:
Of course, one can implement this functionality in an ordinal array. But the trivial implementation would be very inefficient. You can create the tree- of hash- structure to solve it in a faster way, but think a bit: does the implementation of such a container depend on elements we are going to store? Do we have to re-implement the module to make it functional, for example, for points on a plane but not strings?
If not, we can develop the interface for such a container once, and then use everywhere for data of any type. That, in short, is the idea of STL containers.
Before we begin
When the program is using STL, it should #include the appropriate standard headers. For most containers the title of standard header matches the name of the container, and no extension is required. For example, if you are going to use stack, just add the following line at the beginning of your program:
Container types (and algorithms, functors and all STL as well) are defined not in global namespace, but in special namespace called “std.” Add the following line after your includes and before the code begin:
Another important thing to remember is that the type of a container is the template parameter. Template parameters are specified with the ‘<’/’>’ “brackets” in code. For example:
When making nested constructions, make sure that the “brackets” are not directly following one another – leave a blank between them.
Vector
The simplest STL container is vector. Vector is just an array with extended functionality. By the way, vector is the only container that is backward-compatible to native C code – this means that vector actually IS the array, but with some additional features.
Actually, when you type
the empty vector is created. Be careful with constructions like this:
Here we declare ‘v’ as an array of 10 vector<int>’s, which are initially empty. In most cases, this is not that we want. Use parentheses instead of brackets here. The most frequently used feature of vector is that it can report its size.
Two remarks: first, size() is unsigned, which may sometimes cause problems. Accordingly, I usually define macros, something like sz(C) that returns size of C as ordinal signed int. Second, it’s not a good practice to compare v.size() to zero if you want to know whether the container is empty. You’re better off using empty() function:
This is because not all the containers can report their size in O(1), and you definitely should not require counting all elements in a double-linked list just to ensure that it contains at least one.
Another very popular function to use in vector is push_back. Push_back adds an element to the end of vector, increasing its size by one. Consider the following example:
Don’t worry about memory allocation — vector will not allocate just one element each time. Instead, vector allocates more memory then it actually needs when adding new elements with push_back. The only thing you should worry about is memory usage, but at TopCoder this may not matter. (More on vector’s memory policy later.)
When you need to resize vector, use the resize() function:
The resize() function makes vector contain the required number of elements. If you require less elements than vector already contain, the last ones will be deleted. If you ask vector to grow, it will enlarge its size and fill the newly created elements with zeroes.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.