3

I am reading a book on Operating Systems, and I am confused in a part about Internal File Structure. Quoting from the book:

Disk systems typically have a well-defined block size determined by the size of a sector. All disk I/O is performed in units of one block (physical record), and all blocks are the same size. It is unlikely that the physical record size will exactly match the length of the desired logical record. Logical records may even vary in length.

Packing a number of logical records into physical blocks is a common solution to this problem. For example, the UNIX operating system defines all files to be simply streams of bytes. Each byte is individually addressable by its offset from the beginning (or end) of the file. In this case, the logical record size is 1 byte. The file system automatically packs and unpacks bytes into physical disk blocks —say, 512 bytes per block— as necessary.

What is meant by Packing a number of logical records into physical blocks in here?

1
  • basically, starting from the left (or right) of the block, start laying out the data contigiously, and once you run out of data, pad right with nulls or whatever. Feb 25, 2015 at 21:32

2 Answers 2

3

It's actually really simple, yet hard to explain. I think we just need to paraphrase what the author already said.

The disk demands that you talk to it in blocks. Assuming the block size is 512 Bytes:

If you want to write 400 Bytes to disk, you must add another 112 Bytes to make up 512 Bytes. The extra 112 Bytes may as well be zeros, but they have to be there.

When you want to read your 400 Bytes from disk, the hard drive will give you 512 Bytes and it's your job to separate out the actual data from the 112 Bytes of padding.

This is what we call Packing and Unpacking. You can get more complex than the example above, but that's the principle.

3
  • Incidentally, block-based IO is everywhere. Modern CPUs might demand 32-bit data, even when you only need to send an 8-bit value. Internally, SSD hard drives implement another layer of block-logic that's transparent to the PC/OS called pages. As if that's not enough, modern file-systems have their own block-logic too (e.g. NTFS default is 4096 Bytes). In all these cases data is being packed and unpacked to create the required blocks or extract meaningful data from such blocks.
    – misha256
    Feb 26, 2015 at 1:45
  • So what if I have a hard drive that has a block size of say 512 bytes but I format it by NTFS system? Then even if I save a 2 byte data, I will need to send 4096 bytes right, not 512? Feb 27, 2015 at 17:54
  • 1
    @KorayTugay No problem. Your 2-byte example works like this: The 2 Bytes will be packed into 4096 Bytes by NTFS. Then, the 4096 Bytes will be split up and sent to the disk as eight consecutive blocks of 512 Bytes.
    – misha256
    Feb 27, 2015 at 20:01
1

What is meant by Packing a number of logical records into physical blocks mean here?

First you have to understand what a "logical record" is.
It can be variable length, such as text in sentences.
Of it can be fixed length, such as a data structure.

Packing determines how to organize data in the form of these logical records into "physical" containers, i.e. the disk blocks.
For records composed of text, the the read and write accesses are typically sequential (i.e. you start from the beginning and work your way to the "end"). So these records would be packed without any regard to block boundaries.
For records composed of (binary) data, the packing could be an issue, especially when the data records will be accessed randomly (e.g. a database lookup) rather than sequentially (like a tape).

Back in the days of punched cards (with 80 characters per card). I have seen a lame program that stored one 80-byte record per 512-byte sector in order to simplify the record-to-sector mapping for random access. For that size of logical record, the maximum number of records that can be packed (of fitted) into one 512-byte block is six, with 32 unused bytes per sector.
For optimal (random) access time (and at the expense of not minimizing storage space) a logical record should not span disk blocks (so that the random access can be satisfied by reading or writing just one block).

Some operating systems, e.g. *nixes, tend to hide the underlying physical block structure when using files. Other OSes may offer filesystems that expose the hardware characteristics more, such as files types that are allocated of contiguous sectors, or linked sectors or by a table of sectors, and syscalls such as readblk() as well as read() (for a record). The packing of fixed-length records for random access is more crucial is such an environment.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .