3
$\begingroup$

I've read from this How data is physically stored in hard drives, that the North/South orientation of the magnetic medium represents data as 0 or 1 physically in the hard drive. Data is stored in binary, which means that it can be stored as either 0 or 1 in the hard drive. But what determines if that particular block of memory in the hard drive is "Used Space" or "Free Space".

In the OS, you can view how much memory is used, and how much is free.

Let's say i have a 1 TB hard drive. I stored 100 GB worth of data with the repeating pattern "1100 1100". This means that I have 100 GB of used space and 900 GB left of free space (does it?). How does it know that it is used or free? If both 1 and 0 can be used to represent the data in that block of memory, then what represents as a "Free Space"? What if i delete 50 GB of data? I'll have 50 GB more free space, but what represents this free space? And how is the data 1 or 0 of "Used Space" converted to "Free Space"?

$\endgroup$
  • 1
    $\begingroup$ It's up to the OS; the hard drive doesn't care. From the OS point of view it's "stuff I shouldn't overwrite because it's important" vs "stuff I can overwrite when I have something to overwrite it with". $\endgroup$ – user253751 Jan 14 '18 at 22:43
6
$\begingroup$

Free space is a concept at the level of the filesystem, which is part of the operating system. It is up to the filesystem to determine what space is free, and how to exploit it.

Data is stored on the hard drive in a structured way. The filesystem divides the drive into sectors (basic units, usually 512, 1024, 2048, or 4096 bytes), and maintains data structures on the drive which store which sectors are used and which are available. These data structures also store the directory structure of the drive, which sectors comprise which file, file names, and other metadata (such as various file timestamps).

Your experiment, of storing a repeated pattern on the raw drive, doesn't constitute a legitimate use of the drive. The drive is usually used only via a filesystem. The filesystem presents to the user (i.e., the software programmer) a logical view of the hard drive. Physical details such as were different parts of a file are stored are the business of the filesystem rather than of the user (though sometimes the user would like to know more, for example in order to defragment the drive).

The same, by the way, is true for memory, which is maintained by the memory manager, another part of the operating system. The operating system allocated memory to the user, takes care of swapping, stores the memory when hibernating, and so on. On modern CPUs, the user can't just use unallocated memory - this will trigger an exception. The supporting data structures are perhaps more lightweight, but there is still an infrastructure supporting memory allocation and deallocation.

$\endgroup$
4
$\begingroup$

The operating system knows what parts of memory are free and what parts are in use by keeping track of all allocations to processes; anything that isn't allocated is free. Disks are exactly the same: the OS keeps track of what is allocated to each file. (Different structures are used, but it's the same principle.)

At all times, every bit of memory and every bit of disk stores either zero or one. Don't think of it as being like a piece of paper that can either have "0" or "1" or nothing at all written on it; think of it like a light switch: the light is either on or off.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.