The binary output example relies on several features covered in subsequent chapters, but most features are similar to those used in Java. Please review the following as needed.
In a C++ program, any value can be used to control an if-statement or loop. The program treats 0 as false and non-0 as true. See Boolean Type and Values.
An operating system's primary task is managing a computer's resources, such as the CPU, main memory, and disk drive. Operating systems often partition a drive's storage media into individually addressable "chunks" called blocks. Blocks are said to be allocated when they are in use - when they store data - and unallocated when not in use. A drive's free space consists of the unused blocks. An operating system must quickly allocate a free block when needed and quickly deallocate it (return it to the free space) when it is no longer needed.
As hardware evolves, operating systems implement and discard many free space management algorithms. One technique, no longer in general use, is based on a bit vector or bit map, which is a long string of bits. Keeping the bit map in main memory allows the operating system to allocate and deallocate disk blocks quickly.
The Bit Map Algorithm
Silberschatz, Galvin, & Gagne, 2011, pp. 441-442, describe the bit map free space allocation algorithm. Logically, we can view a disk drive as a sequence of blocks uniquely numbered from 0 to n-1, where n is disk size. Each bit in the bit map represents one disk block.
0 ≤ b < n
bit[b] == 1 ⇒ block[b] is free
bit[b] == 0 ⇒ block[b] is allocated or in use
(a)
(b)
b = (number of bits per word) * (number of 0-value words) + offset to first 1 bit
(c)
(d)
Number of bits per word = 32
number of 0-value words = 3
offset to the first 1 bit = 3
32 * 3 + 3 = 96 + 3 = 99
(e)
(f)
Managing free space with a bit map. The bit map consists of an array of words or unsigned integers. The bit map in this example has seven words, numbered 0 to 6, and assumes 32 bits per word, numbered 0 to 31 from right to left.
The relationship between n, the disk size, and b, an arbitrary disk block number.
The meaning of the bits in the bit map.
To find a free block, the algorithm searches the bit map from 0 to n-1, returning the block number, b, of the first free block found. In this formula, "word" is the CPU's native word size: 32- or 64-bits currently being the most common sizes. For speed and efficiency, the C++ int type and word size are the same.
In this example, the first three words (0, 1, and 2) consist of all 0s, meaning that the disk's first 32*8 = 96 blocks (blocks 0 through 95) are allocated and in use.
In word 3, the first 3 bits (numbered 0, 1, and 2) are also 0s, meaning that blocks 96, 97, and 98 are also allocated.
Plugging in the numbers, we calculate that block 99 is the first free block.
Program
Silberschatz, A., Galvin, B. G., & Gagne, G. (2011). Operating Systems Concepts Essentials. Hoboken, NJ: John Wiley & Sons, Inc.