defragmention

Other topics related to computer science
Post Reply
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

defragmention

Post by Face » Tue Apr 06, 2010 7:40 pm

we know defragmention in windows..we use it also.But don't know what is happening in the Hard disk when we defragmention it.I want to know the technical view of this.....

Can you explain that what is exactly happen in hard disk when we defrag it..?
Is there any time to defrag our hard disks..(ex-once a week ;) )...?
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: defragmention

Post by Neo » Tue Apr 06, 2010 11:44 pm

To understand this properly, you need to understand the architecture of typical file system.
  1. Master Boot Record
    This is commonly known as MBR (or Track 0) and this is the first sector of a hard disk. This contains important information like bootstrap code, partition table, total sectors on disk, etc...
    System BIOS passes execution to Bootstrap code as soon as system board initialisation phase is done.
  2. Boot Sectors
    Each partition has a Boot sector as the start sector. Boot sector can be either bootable or non-bootable.
    If it is bootable, then again it has a boot code that passes execution to required boot files of the operating system.
  3. File Allocation Table (FAT)
    The whole Data area is divided in to units called clusters. Each cluster is typically 4 KB in size (i.e.: 8 sectors where each sector is 512 Bytes). Even you store a 1 Byte file, it will take a cluster size in the disk. Every cluster in disk is represented as a File Allocation Unit in File Allocation Table. In FAT32, file allocation unit is 32-bit or 4 Bytes. In FAT16, it is 16-bit or 2 bytes. Number of FAT sectors depend of the disk size and the cluster size.
  4. Directory Area
    Only available in FAT12 and 16. This is where those FSs used to store File names and Directory names.
  5. Data Area
    File data are stored in this area. Data area is divided in to small blocks called clusters. Each cluster is usually 4KB unit.
Now I'm going to explain you about fragmentation and the need for de-fragmentation. Think that you are going to store some files on a blank disk with a cluster size of 4KB.
  1. You store a file of 2 KB.
    Required number of clusters = 2 KB / 4 KB = 0.5 = 1 cluster required
    On the FAT table, we mark the 1st entry (that represents 1st cluster) as used then store data to cluster 1 in data area.
  2. Next you store a 33 KB file
    Required number of clusters = 33 KB / 4 KB = 8.25 = 9 clusters required
    On the FAT table, we mark from 2nd to 10th entries as used.
  3. After that you are going to store a 15 KB file
    Required number of clusters = 15 KB / 4 KB = 3.75 = 4 clusters required
    On the FAT table, we mark from 11th to 14th entries as used.
  4. Now you delete 2nd file (33 KB one)
    On the FAT table, we mark from 2nd to 10th entries as not used.
Now consider your FAT table.
1st - used, 2nd - not used......., 10th - not used, 11th used........, 14th used
So far so good.


  1. Now we are going to store a 63 KB file.
    Required number of clusters = 63 KB / 4 KB = 15.75 = 16 clusters required
  2. We have 2nd to 10th cluster marked as unused. There are 9 free clusters.
  3. We store first half of file to those 9 clusters and mark the FAT entries as marked.
  4. We need 7 more clusters to store the complete file. We store those data to cluster 15th to 21st.
Now on the FAT table, we have entries 1st to 21st as used.

However note that the 63 KB file is stored in two parts to disk. This is called fragmentation of files. Though you might be happy with it, the Hard disk is not. Now I'm going to tell you why.

Hard disks have a caching system as it handles MBs of data every second. Continuous files are cache friendly where as fragmented files are not. Due to this problem, the access performance of a fragmented disk goes rapidly down. Especially critical files (mostly accessed files) need to be kept continuously.

The process of converting fragmented files to continuous is called de-fragmentation. What happens here is the fragmented files are first moved to a free area of the disk. Then the holes are filled with small files or data of existing files are moved by keeping them continuous. Finally the moved files are copied back to be continuous. There are lots of algorithms to do this. You can read more on that if you are interested of making one.

However at the end of de-fragmentation process there will be no fragmented files on disk which increase the performance of the hard disk.

This is just a simple explanation. Do you get the idea?
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: defragmention

Post by Face » Sun May 09, 2010 7:40 pm

yes BRO I got the idea.Sorry about the late reply.Honestly I hadn't much time those days because of my job.now I started my training section in the job & got use to that.Now have more time to read about technology.:)

Today I got the time to read this post from you bro.thank you for all.I tried a lot to find some simple notes about this section but couldn't.from your post I got the idea.

:P :P :P

G-sparkZ
Post Reply

Return to “Other”