Understanding File Table

Understanding how Files are linked from programming languages to Operating System File Table

Operating System :

  • Its a resource allocator.
  • A OS has multiple resource to manage like Network, CPU, Hard-disk and RAM at its disposal.
  • Any process can request the OS for any  resources using “pre defined System calls made available by the OS”
  • For any resource requested OS returns a resource Descriptor (a token using which process can interact with the resource).
  • Resource Descriptor (token) takes various names like File Descriptor, Socket Descriptor .
  • A resource can be managed only by the Operating System, a process can just pass the instructions to perform a specific action on the resource but can never manage it directly.

 

 

Mappings :

  • Each process maintains a File descriptor mapping .  Each entry in the mapping points to a System wide File Table.
  • File Table is a logical mapping maintained by the Kernel which  holds all logical information about a file(permisson , access right, read write fileOffset)
  • Each entry Of FileTable maintains a pointer to the Indode Table.
  • Inode Table is a logical mapping of the file to actual Hardisk mapping of Tracks and sectors. The mapping is maintained by the Kernel.
  • Do understand FileDescriptor is mapping maintained in the process and not in the kernel.
  • Programming language Wrap the File Descriptor withing its own FileHandler (In Java File(),  in C fs.create()).
  • Programming languages in no way can access the file Directly , it has to interact with OS to get the resource.

 

The logical mapping goes from Process(pid) => File Descriptor => Inode => Hard disk blocks .

 

[addToAppearHere]

Find the File Descriptors opened by a process

  • ls -l  /proc/PID/fd/ : List FileDiscriptors from a proc fileSytsem
ls -l /proc/5123/fd
lrwx------ 1 maverick maverick 64 Mär 25 14:24 0 -> /dev/pts/0
lrwx------ 1 maverick maverick 64 Mär 25 14:24 1 -> /dev/pts/0
lr-x------ 1 maverick maverick 64 Mär 25 14:24 10 -> /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/jaccess.jar
lr-x------ 1 maverick maverick 64 Mär 25 14:24 11 -> /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/nashorn.jar
lr-x------ 1 maverick maverick 64 Mär 25 14:24 12 -> /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/sunpkcs11.jar
lr-x------ 

File Descriptors are 0 , 1, 10, 11, 12
  • Using Lsof  : sudo lsof -p PID
sudo lsof -p 5829
COMMAND  PID        USER   FD        TYPE             DEVICE  SIZE/OFF   NODE     NAME
java    5829     maverick  cwd       DIR                8,2      4096  2360848    /home/maverick/INTELLIJ/idea-IC-163.12024.16/bin
java    5829     maverick  rtd       DIR                8,2      4096        2    /
java    5829     maverick  txt       REG                8,2      6464 14555957    /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
java    5829     maverick  mem       REG                8,2   1366829  1710491    /home/maverick/.IdeaIC2016.3/config/sonarlint/plugins/411504f8d216a6e2984e9235e60e312c/sonar-java-plugin-4.4.0.8066.jar_unzip/META-INF/lib/java-checks-4.4.0.8066.jar
java    5829     maverick  mem       REG                8,2     27000  6291740    /lib/x86_64-linux-gnu/libnss_dns-2.23.so


Inodes : 2360848, 14555957, 1710491, 6291740

 

 

How to get the Inode of a file .

 

  • stat ~/Downloads/PRIVACY
File: '/home/maverick/Downloads/PRIVACY'
Size: 4314      	Blocks: 16         IO Block: 4096   regular file
Device: 802h/2050d	Inode: 12856961    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1001/maverick)   Gid: ( 1001/maverick)
Access: 2017-03-25 13:26:10.716287052 +0100
Modify: 2016-11-10 14:54:10.196178141 +0100
Change: 2016-11-10 14:54:10.200178142 +0100

 

  • ls -li ~/Downloads/File
12856961 -rw-rw-r-- 1 maverick maverick 4314 Nov 10 14:54 /home/maverick/Downloads/PRIVACY

 

[addToAppearHere]

Inode to Actual Block Mapping

  • sudo /sbin/hdparm –fibmap ~/Downloads/PRIVACY
/home/maverick/Downloads/PRIVACY:
 filesystem blocksize 4096, begins at LBA 1050624; assuming 512 byte sectors.
 byte_offset  begin_LBA    end_LBA    sectors
           0  414051752  414051767         16