Q.  What are holes in Files?

A.  A hole in a file area  are represented by '0'.   Holes are created when user writes data beyond the length of the file (beyond file size). The space between new data and old file size data will be filled with zero's and are called Holes.


Q.  Below question asked by one of the student.

I've written a simple program to get the current offset. It always prints '0', as if the offset is per process. I've open the log.txt in vim and was changing the cursor and was also adding the new symbols. Do you know what I'm missing here?

offset1.c

  1. #include <sys/types.h>

  2. #include <sys/stat.h>

  3. #include <fcntl.h>

  4. #include <unistd.h>

  5. #include <stdio.h>

  6. #include <stdlib.h>


  7. int main()

  8. {

  9. printf("\noffset1 sleeping\n");

  10. sleep(3);


  11. ssize_t fd = open("log.txt", O_RDWR);


  12. off_t offset;

  13. offset = lseek(fd, 0, SEEK_CUR);

  14. if (offset == (off_t)-1)

  15. {

  16. perror("lseek");

  17. exit(1);

  18. }


  19. printf("\noffset1 finished: (%ld)\n", offset);

  20. printf("\noffset1 current position: (%ld)\n", offset);

  21. }

offset2.c

  1. #include <sys/types.h>

  2. #include <sys/stat.h>

  3. #include <fcntl.h>

  4. #include <unistd.h>

  5. #include <stdio.h>

  6. #include <stdlib.h>


  7. int main()

  8. {

  9. ssize_t fd = open("log.txt", O_RDWR);


  10. off_t offset;

  11. offset = lseek(fd, 10, SEEK_SET);

  12. if (offset == (off_t)-1)

  13. {

  14. perror("lseek");

  15. exit(1);

  16. }


  17. printf("\noffset2 current position: (%ld)\n", offset);

  18. printf("\noffset2 sleeping\n");


  19. sleep(3);

  20. printf("\noffset2 finished\n");

  21. }

Below output recorded after running both above programs simultaneoulsy

offset1 sleeping

offset2 current position: (10)

offset2 sleeping


offset1 finished: (0)

offset1 current position: (0)

offset2 finished


Explanation:

Yes the Offset works on Per process basis, Refer to Lecture 17 and 18.

1. Here the 2 programs acts as 2 separate process.

2. Each Process will have dedicated file descriptor table.

3. There is one system wide 'open file table'.

4. Now when each process open same file, each process has entry in its own 'file descriptor table'

5. And each process creates its own entry in 'open file table'. Hence Offset, mode in which file is opened is specific to each Process. (Say row1 in open file table has entry for process 1, row2 in open file table has entry for process 2, finally the inode pointer of both entries will point to same inode table)