Is the file an object local to each process or to the whole system?

As a developer of Linux device drivers, I thought that the file object is a local structure for each process, and its address is available in the fd table for the corresponding fd. But when I came across section 5.6 in the Linux Programming interface from Michale Kerrisk, which claims to be

Two different file descriptors that reference the same open file description file are the file offset value. Therefore, if the file offset is changed through one file descriptor (as a result of read (), write () or lseek () calls), this change is visible through another file descriptor. This refers to the description of two files. Torahs belong to the same process and when they belong to different processes.

I'm confused ... Please help me improve my understanding.

+4
source share
1 answer

Each process has its own file descriptor table, and each time the open() ed file gives a separate file description. So there’s common sense!

The exception is duplication of the file descriptor either inside the process (via dup() ), or through processes (by one fork() process with a copy with all the same FDs) or by passing the file descriptor through UNIX). When this happens, the two descriptors ultimately share some properties with each other, including the offset.

This is not necessarily a bad thing. This means, for example, that two processes that are simultaneously written to a common file descriptor will not overwrite each other output. However, this can sometimes have unexpected results. But usually this is not something you could learn about without knowing it.

+5
source

Source: https://habr.com/ru/post/1496844/


All Articles