Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

b. [10 points] Suppose you are given the inode structure as shown below. Assume

ID: 3585278 • Letter: B

Question

b. [10 points] Suppose you are given the inode structure as shown below. Assume that our inode structure has 10 direct pointers, and that Block size is 8K bytes and pointers are 4 bytes. Write a function int count_ di (struct inode *myinode, char *word) ; which counts the number of blocks containing the given string word only in double indirect blocks. Note that double indirect level might be partially filled with blocks! So if there is no more block or level the corresponding pointer will contain NULL. Please take that into account! If there is a data block, we assume that this data block contains a string text. Thus, if needed, you can use strcmpO or any other functions from the standard libraries as they are included here. mode owners (2 int count_ di (struct inode *myinode, char *word) l int i, j, k; int count=0; int num blk = omestamps size block count data db dfirect blocks data data l single indirect double indirect data di

Explanation / Answer

Solution to given problem is mentioned below. Comments are also added in below function for code readability.

int count_di(struct inode *myinode, char *word) {
int i, j, k;
int count = 0;
int num_blk = 0;
  
// Iterate through first level of 10 pointers   
for (i = 0; i < 10; i++) {
  
if ((myinode->di + 4*i) != NULL) { // Not an empty first level block
  
// Iterate through second level of 10 pointers
for (j = 0; j < 10; j++) {
  
if ((*(myinode->di + 4*i) + (4*j)) != NULL) { // Not an empty second level block
  
if (strcmp((*(myinode->di + 4*i) + (4*j), word) == 0)) { // Found data block matching given word
  
num_blk += 1; // Increment count of matching data blocks
  
}
  
}
  
}
}
  
}
  
return num_blk;
}