Contents
What are file descriptors explained in simple terms?
A file descriptor is a number that uniquely identifies an open file in a computer’s operating system. It describes a data resource, and how that resource may be accessed. When a program asks to open a file — or another data resource, like a network socket — the kernel: Creates an entry in the global file table.
What is the relationship between file stream pointer and file descriptor?
A FILE pointer is a C standard library-level construct, used to represent a file. The FILE wraps the file descriptor, and adds buffering and other features to make I/O easier. You pass FILE pointers to standard C functions such as fread() and fwrite() .
Are file descriptors unique per process?
File descriptors are generally unique to each process, but they can be shared by child processes created with a fork subroutine or copied by the fcntl, dup, and dup2 subroutines.
How are file descriptors and streams represented in C?
File descriptors are represented as objects of type int, while streams are represented as FILE * objects. File descriptors provide a primitive, low-level interface to input and output operations.
What’s the difference between a stream and a file?
File descriptors provide a primitive, low-level interface to input and output operations. Both file descriptors and streams can represent a connection to a device (such as a terminal), or a pipe or socket for communicating with another process, as well as a normal file.
When do you need to use a file descriptor?
You must also use file descriptors if your program needs to do input or output in special modes, such as nonblocking (or polled) input (see File Status Flags ). Streams provide a higher-level interface, layered on top of the primitive file descriptor facilities.
What is the name of the file descriptor in find?
For example, when you use the find command, successful output goes to stdout (file descriptor 1), and error messages go to stderr (file descriptor 2). Both streams display as terminal output: We’re getting errors because find is trying to search a few system directories that we don’t have permission to read.