简体   繁体   中英

Is the file position per inode?

I am confused with the concept of file position as used in lseek. Is this file position maintained at inode level or a simple variable which could have different values for different process working on the same file?

Per the lseek docs , the file position is associated with the open file pointed to by a file descriptor, ie the thing that is handed to your by open . Because of functions like dup and fork , multiple descriptors can point to a single description , but it's the description that holds the location cursor.

Think about it: if it were associated with the inode, then you would not be able to have multiple processes accessing a file in a sensible manner, since all accesses to that file by one process would affect other processes.

Thus, a single process could have track many different file positions as it has file descriptors for a given file.

It's not an 'inode', but FILEHANDLE inside kernel.

Inode is a part of file description of the *nix specific file system on the disk. FAT32, for example, has no inodes, but supported by Linux.

For know the relation between file descriptors and open files, we need to examine three data structures.

  • the per-process file descriptor table
  • the system wide table of open file descriptors
  • the file system i-node table.

For each process kernel maintains a table of open file descriptors. Each entry in this table records information about a single file descriptor including

  • a set of flags controlling the operation of the file descriptor.
  • a reference to the open file description

The kernel maintains a system wide table of all open file descriptors. An open file description stores all information related to an open file including:

  • the current file offset(as updated by read() and write(),or explicitly modified using lseek())
  • status flags specified when opening the file.
  • the file access mode (read-only,write only,or read-write,as specified in open())
  • setting relating to signal-driven I/O and
  • a reference to i-node object for this file.

Reference-Page 94,The Linux Programming Interface by Michael Kerrisk

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM