- Put in index/position values!
0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Find the index of the first fat entry that is a 1 (working from the left) – here that would be position 2
In your directory write
a 2 (showing you have a file called a that starts storage in sector 2)
You know you have to set up a chain of 6, so file a is going to occupy 2 -> ? -> ? -> ? -> ? -> ? and we have to find what the question marks should be and record them in the fat according to the rules.
With an empty fat like this, it is dead easy: the chain will simply be 2 -> 3 -> 4 -> 5 -> 6 -> 7 and at position 2 in the fat you record the next sector in the chain (3),
- at position 3 you record the next sector in the chain (4)
- at position 4 you record the next sector in the chain (5)
- at position 5 you record the next sector in the chain (6)
- at position 6 you record the next sector in the chain (7)
- at position 7 you record 0 to show it is the end of the chain 6. so that the fat looks like:
0 0 3 4 5 6 7 0 1 1 1 1 1 1 1 1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Easy, but the principle used is the same even if the fat hadn’t been empty:
To set up a chain of 6, you need to repeatedly
i) find the next 1 fat entry (free sector) right of the sector position you’re currently on and ii) record its index/position back at your current index/position in the fat
until you have six sector numbers involved, then finally you enter a 0 opposite the final position number, to show that sector is at the end of the chain (it is in use for storage, not a 1, so not free, but it doesn’t link to another sector)
In our case:
i) the next 1 entry after position 2 (first sector) is at position 3. In other words sector 3 is empty and file a can spill over from sector 2 into sector 3.
ii) We record this fact (this link of the chain, 2 -> 3) by writing a 3 opposite index 2 (make a fat entry of 3 at position 2).
Then we repeat this process starting from sector/position/index 3 - i) find the next 1 fat entry, right of position 3 (turns out to be at position 4)
iii) record a 4 opposite index 3, indicating that the next sector (after 3) in the 6-sector chain that file a needs for storage, is 4 and so on until we have set up a six-sector chain, in this case: 2 -> 3 -> 4 -> 5 -> 6 -> 7
Then finally we record a 0 at position 7 to mark the end of the chain.
Try the process in the case of a fat that is not empty and see that you can do it even when later sectors in the chain don’t simply follow on directly after earlier ones.