Defrag-o-matic using c# | Computer Science homework help

CMPE2300 – Lab01 – Defrag-o-matic

In this lab you will create a simulator to implement a file system defragmentation. Prior to

SSDs, file systems benefited from ensuring files that were comprised of multiple disk

partition units were allocated and adjacent on disk. This ensured minimum head

movement for read/write of specific files.

Our goal is to read in a text file formatted as one file allocation unit per row with a data

component indicating the file name, a sequence number indicating part N ( of M ) units,

and a next value indicating where the next unit resides. Once loaded and displayed the

simulator will allow a basic defragmentation to be done, attempting to group files into

adjacent allocation blocks.

Program Specification

Your project will require use of the CDrawer.

You will need to define a structure to represent file allocation units, comprised of :

• a string – representing the filename ( it will be one character for our simulation )

• an integer – representing the sequence number of this allocation unit

• an integer – representing the allocation unit of the next part of the file.

Assumptions :

A next value of -1 indicates that this allocation unit is empty

A next value of -2 indicates that this is the last allocation unit of this file

Any file system load will always have > 0 empty units

Your UI will have 3 buttons :

[Load FileSystem]

[Verify FileSystem]

[Defragment] / Toggling to disable defragmentation

And a listbox for status output – population should always use insert so newest additions

are first.

Part I – Load FileSystem

Your application will be allowed to create class level members for the following :

• List of File Allocation Units ( structure )

• CDrawer

• integer representing CDrawer File Allocation Scale ( not CDrawer scale )

• constant integer representing the maximum width for the CDrawer in pixels = 1000

Bind your [Load FileSystem] click event to a method in your form constructor.

[Load FileSystem]

Dynamically create an OpenFileDialog, and using System.IO.Path.GetFullPath and the

appropriate Environment variable, set the Initial directory to the root source ( this will allow

easy loading of the test files in your project ). Using System.IO.File.ReadAllLines(), read the

user selected file in.

For each line read, you may use split() to separate the 3 expected elements to be

processed. Process as required to make and add a structure entry to your List for each.

Upon completion, add a status line in the form : “Load FileSystem: Loaded N units”

Now that the load is complete, it needs to be displayed in the CDrawer. We want our

displayed grid to be square – in number ( ie. 6 x 6 ). Given the load number, determine the

smallest dimension that can be used. The “scale” is the file unit cell height, it will be 4

times wider than tall to fit our output requirements. Given the fixed CDrawer width,

determine the “scale”, and close any existing CDrawer before allocating a new CDrawer of

our calculated dimensions.

To actually display the file units, create a some new helper methods :

MakeColors() – returns nothing and accepts nothing. It will be invoked from the

constructor and will populate a form member List<Color> with a new list then fill the list

with colors derived from r, g and b values of 64, 128, 192 ( notice the pattern – a triple

nested loop is required ). The list will now be comprised of ?? colors, matching our

alphabet filename needs.

RenderUnits() – returns nothing, accepting a List of file units, a CDrawer and the file unit

height. Clear() the CDrawer, and iterate through all the file units. For each, construct a

Rectangle object with the appropriate X, Y coordinates ( Grid work ? ), a width 4 times the

height. If the file unit is empty, fill the rectangle with gray. Otherwise, use the letter as the

offset ( ie. a is 0, b is 1,.. z = 25 ) fill the rectangle with the indexed color from our colors list.

Then using the same Rectangle, use AddText to populate the rectangle with file unit’s

information in the form :

file_name:sequence_number:next_value, ie. h:3:34

Remember to render your CDrawer.

Now include RenderUnits as the last thing in your load function to see your filesystem.

Part II – Verify File System

Just because our file system loaded it does not mean it is valid. We need to validate the

loaded system to ensure all file units ( and supposed empty space ) are error free. This

step is of your own design, with these basic requirements :

• Every file unit will start having a sequence number of 0. A file unit chain must end

with a next value of -2. A file may consist of a single file unit ( sequence = 0 and next

of -2 ).

• Verification requires that every file unit chain is in sequence and terminates


• Verification may discontinue on the first error found – as many possible errors could

potentially create an avalanche of cascading and compounding errors.

• Any error found should result in a status addition in the following form :

Validation:Error:error_condition with appropriate values.

Error conditions would include, but not be limited to :

• File_name : termination missing

• File_name : out of sequence

• File_name : start unit missing

Your instructor will discuss possible approaches.

Part III – Defragmentation

We need some helper functions to implement defragmentation.

FirstEmpty() – returns an integer representing the found empty index, accepts a list of file

units and a bool representing if the search should start halfway through the list – set a

default value of false. This method is used to find an available spot to move an existing file

unit for defragmentation. Depending on the bool flag, start searching the list returning the

index of the first empty spot encountered in the supplied list. Starting halfway must still

cycle around through to the start looking for an empty spot. Only a single loop and

remembering our assumptions is required.

MoveUnit() – returns void, accepts a list of file units, a list index to move and a list index to

move the file unit to. In order to move a file unit, there may be an existing file unit that has

a next value referencing the move location. This unit must be modified in order to maintain

order in the move. Therefore, using the file name and move index determine ( if any ) prior

unit exists – save this index ( or -1 for no prior unit found ).

Now perform the move – the order is critical here – copy the unit to be moved to the new

empty location, modify ( if required ) the prior unit, lastly set the move index unit to empty.

Why is this important if this were real defragmentation ?

NeedDefrag() – returns an index indicating a unit that should be defragged. It accepts a

list of file units. We will use a “lazy” evaluation of whether defragmentation is required. It

involves 2 distinct passes through the list.

The first pass determines if we have a “starter” unit ( seq = ? ) and if the next unit is not next

in line. If so, we want this unit defragged, return this index.

The second pass, if the first pass found nothing, determine if a unit is found where the next

unit is not the next in line. If so, we want to this unit defragged, return the index.

This method is biased to try to defragment units which are the start of a file unit chain.

Thread method Defrag(), accepts an object ( the list of units to defragment ) and returns

nothing. This method will be used as a Thread target, created and started when the user

clicks the Defragmentation button. The thread reference should be created as a form

member and will be used by the thread to monitor for termination.

Repeat forever while the thread reference is valid :

Render your file system, have short sleep ( 25ms for checkoff, 500ms or more for debug )

Invoke your NeedDefrag() method saving the returned unit index. If no units need debug

break your loop, set your thread reference to null and let the method complete.

Otherwise we have a unit whose next adjacent unit is wrong. There is a special case here,

if the last unit is indicated, we can’t move ANY adjacent units. So, if the unit indicated is the

last node and is not the last node in the file chain, invoke MoveUnit with the unit index and

the index returned by FirstEmpty() retrieving the true first empty index. If this special case

occurred, this pass is complete, continue.

Normal processing is to determine the swap index for our move of the adjacent next unit

that is out of place. Invoke FirstEmpty() with a midway start saving the returned index as

our swap index. Now we have a unit to move and a place to move it to, but we are moving

the next unit to make room for the unit that belongs there !

MoveUnit() the adjacent unit to our swap index, ie. move it out of the way.

MoveUnit() the unit that belongs next, to the next index spot. ( use next value )

The cycle is complete, one down, many to go. Prior to exiting your thread method, ensure

you set the thread member to null, indicating the thread is complete.

You will need to update the main UI as to the progress made by Defrag(). Determine an

appropriate delegate type, and using Invoke() ( not Dispatcher.Invoke() ), invoke a string

message back to the main UI thread to update the status listbox for each MoveUnit

operation in the form “Defrag: Moved [N] to [M]”.

To finish wiring up the UI, implement a toggle action on the button so it toggles between

[Defragment] and [Cancel Defrag], using the thread member as both the indicator for the

button text and the flag for your thread termination. Your thread should Invoke a message

back prior to exiting in the form “Defrag:Thread terminated”

Paper Writing Center
Calculate your paper price
Pages (550 words)
Approximate price: -

Why Work with Us

Top Quality and Well-Researched Papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

Professional and Experienced Academic Writers

We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.


Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.


Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.


Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.


Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.