is the file pointed by stdin file descriptor the same file for different processes? -
i have question in mind. convention, unix associates file descriptor 0, 1, 2 stdin, stdout, stderr on every process. file, e.g. pointed stdin, shared different processes? if shared, when open 2 shells type inputs these 2 shells, how os doing manage shared file?
overview
the descriptor table per-process, it's possible every process in system have different file open in every descriptor table slot
but in practice it's bit more complex. if 2 processes open file independently, each have separate access file, own read , write pointers, , interact if both write same file.
but when process fork(2)'s, parent , child's descriptors point same file table entry, , share single position in file. enables unix processes share access input stream without needing aware of situation.
three tables
access files chained through 3 important tables in unix. descriptor table per-process , points file table. think of file table open file table. there third table, called the inode table manages access actual files.
the key thing realize while there never more 1 entry in inode table 1 file, there may or may not multiple entries in file table. if file descriptor created open(2) inode gets new file table entry, if it's created fork(2) same file table entry reused , read , write pointers shared.
so, 2 shells...
in case of 2 shells 2 different windows typing, or 2 shells running different scripts, aren't using same file @ all, or if it's same script, opened twice positions independent. both may file descriptor "0" within each process that's because every process has own descriptor table.
Comments
Post a Comment