Suppose a bitmap is used for tracking a disk block free list. The bitmap is represented as an array of 32-bit integers
Posted: Thu May 05, 2022 1:34 pm
Suppose a bitmap is used for tracking a disk block free list.
The bitmap is
represented as an array of 32-bit integers (i.e., each word is an
32 bit integer). Write
C syntax pseudocode to implement the following
function:
/*
* Given a bitmap (first argument) stored in an array of words,
starting from the
* beginning of the bitmap, find the first run of consecutive free
blocks (a hole)
* whose size has at least the number of needed blocks (second
argument), and
* return the starting block index of the found run of free blocks.
If a big enough
* hole cannot be found to fit the number of needed blocks, return
-1.
*/
#define BITSPERWORD 32
int findFreeblocks (int words[], int numOfNeededBlocks)
Error checking is not required. And assume:
1) int type has 32 bits.
2) Each bit in the bitmap represents one block, value 1 is
occupied, 0 is free.
3) The block index starts at 0 from the first bit to the left (most
significant) of the
first word, incrementing one at a time to the right, then
continuing to the next
word. E.g., the block index of the first bit of the second word
would be 32, etc.
4) NO need to worry about the endianness of storing each integer /
word.
Hint:
1) To extract each bit from the word, use a bit mask and bitwise
and.
2) The hole (run of free blocks) would start from a bit with 0 in a
word entry and
runs until it reaches a bit with 1. It could go across the word
boundaries.
The bitmap is
represented as an array of 32-bit integers (i.e., each word is an
32 bit integer). Write
C syntax pseudocode to implement the following
function:
/*
* Given a bitmap (first argument) stored in an array of words,
starting from the
* beginning of the bitmap, find the first run of consecutive free
blocks (a hole)
* whose size has at least the number of needed blocks (second
argument), and
* return the starting block index of the found run of free blocks.
If a big enough
* hole cannot be found to fit the number of needed blocks, return
-1.
*/
#define BITSPERWORD 32
int findFreeblocks (int words[], int numOfNeededBlocks)
Error checking is not required. And assume:
1) int type has 32 bits.
2) Each bit in the bitmap represents one block, value 1 is
occupied, 0 is free.
3) The block index starts at 0 from the first bit to the left (most
significant) of the
first word, incrementing one at a time to the right, then
continuing to the next
word. E.g., the block index of the first bit of the second word
would be 32, etc.
4) NO need to worry about the endianness of storing each integer /
word.
Hint:
1) To extract each bit from the word, use a bit mask and bitwise
and.
2) The hole (run of free blocks) would start from a bit with 0 in a
word entry and
runs until it reaches a bit with 1. It could go across the word
boundaries.