Add Book to My BookshelfPurchase This Book Online

Chapter 3 - Low-Level I/O Routines

UNIX Systems Programming for SVR4
David A. Curry
 Copyright © 1996 O'Reilly & Associates, Inc.

File Descriptors
All of the functions described in this chapter use a file descriptor to reference an open file. A file descriptor is simply a small integer that identifies the open file to the operating system. There are three file descriptors that are predefined when each program is invoked. The standard input, usually the keyboard, is identified by file descriptor 0. The standard output, usually the screen, is identified by file descriptor 1. The standard error output, also usually the screen, is identified by file descriptor 2.
File descriptors are allocated from a table maintained for each process by the operating system, and each file descriptor is simply an index into that table. Most older versions of UNIX limit the maximum number of files a process can have open at once to approximately 20. Newer versions have larger limits such as 32 or 64, and SVR4 allows up to 256. One of the features of this table-based implementation is that opening a file always returns the lowest-numbered available file descriptor. Thus, because a process starts out with three open files (0, 1, and 2), the first file it opens is attached to file descriptor 3. If the program later closes its standard input (file descriptor 0), then the next file it opens is attached to file descriptor 0, not file descriptor 4. This behavior is found in all versions of UNIX, and is also specified by the POSIX standard.

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback