简体   繁体   中英

How does a system call know what process a file descriptor belong to?

I've heard that file descriptors are unique to each process. But, are they unique to the entire system? How are they handled within the kernel? If file descriptors are not unique system-wide, how does a system call, like read , write , close , etc, know what process the file descriptor they receive belong to?

Invoking a system call does not involve a change of PID; the system call simply looks at the FD table of the current PID.

The operating system simply has to maintain a mapping of the combination of process ID and file descriptor to identify a particular file. It might do so by maintaining a list of file descriptors with a given process's task control block, or by whatever other means the implementer decided to use.

But, are they unique to the entire system?

No, they are unique to a process.

A typical kernel has access to a data structure representing the process when a system call is made. A file descriptor can be as simple as an index into an array in that data structure.

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