Reputation: 4637
I am interested to know the valid values which I can expect for a file descriptor.
Please let me explain a bit. I know that, for instance, when I use #include <unistd.h>
on my linux system then a call to open a file for reading:
int fileDescriptor;
fileDescriptor = open("/some/filename",O_RDONLY);
an error might occur and I receive -1 as a result.
Incidently the (-1) negative one must have somewhat of a special meaning. Is it that all other values are valid file descriptors? i.e. also negative ones like -2 and -1023?
Assuming that int is 4 bytes (sizeof(int)==4
), then would
(-1) = 10000000 0000000 00000000 00000001
would be the only detectable invalid file descriptor? Would others like:
(0) = 00000000 0000000 00000000 00000000
(-2) = 10000000 0000000 00000000 00000010
(2) = 00000000 0000000 00000000 00000010
be ok? Since the file descriptor could store 4 bytes I could have therefore a maximum of (2^(8*4)-1) valid file descriptors and consequently this would be the maximum number of files I can have open, correct?
To put it plain again:
What should I expect a (valid) file descriptor to be?
any value but -1?
Upvotes: 22
Views: 54071
Reputation: 19
File descriptors are basically a system's way of keeping track of all the open files (and some other types of I/O channels) within a running program. They are represented as integers, and you can think of them as a kind of "ID" or "handle" that your program uses to work with files.
Let's consider a real-life analogy. Imagine you're a librarian, and every time someone checks out a book, you give them a ticket with a unique number. Later, when they want to return the book or ask about it, they show you the ticket, and you can immediately know which book they're talking about. In this analogy, the file descriptors are the numbers on the tickets.
When you open a file in a program, the operating system gives you a file descriptor – the smallest unused number – that you can use to refer to that file in the future. When you read from or write to the file, you use that file descriptor. When you're done with the file, you "close" it, which tells the system you're done with that file descriptor, and it can use that number for something else.
The exact limit of fd depends on your system, but it's typically quite high (thousands or even tens of thousands). Each open file, pipe, or other resource consumes one file descriptor.
Here are some standard file descriptor values:
0: Standard input (stdin)
1: Standard output (stdout)
2: Standard error (stderr)
These are the default file descriptors that are available in every process. When you open a file using the open function, you get a file descriptor that is always the lowest-numbered file descriptor not currently open for the process. This is usually 3 for the first file opened, 4 for the second, and so on.
A file descriptor remains open until it is explicitly closed, or until the process terminates. It's important to always close file descriptors when you're done with them to prevent a file descriptor leak, which can cause a process to exhaust the number of file descriptors available to it.
Example:
write(1, "a", 1); //First argument represent the Standard Output.
write(4, "a", 1); //4 is not currently open or valid, then the write function will fail, and it will return -1 to indicate an error.
int fd;
fd = open("42", O_RDWR | O_CREAT | O_APPEND, 0777); //Create file "42"
printf("%d\n", fd); // => fd value is 3 because it takes the next smallest int available (0, 1 and 2) are always taken.
fd can go up to thousands depending on your system.
Upvotes: 1
Reputation: 165
Range of possible values of file descriptors is from 0 to 1023 for Linux system (32-bit or 64-bit system).
You cannot create a file descriptor with value more then 1023. In case of file descriptor of value 1024, it will return an error of EBADF (bad file descriptor, error no-9).
When a negative value of file descriptor is returned it indicates that an error has occurred.
Upvotes: 3
Reputation: 22946
Here's what a Linux manual page says:
open()
andcreat()
return the new file descriptor, or-1
if an error occurred (in which case,errno
is set appropriately).
Other systems may return other negative values in case of error.
Upvotes: 1
Reputation:
When open
fail, it returns -1
, or 0xffffffff
. It has no meaning but open
failed:
Upon successful completion, the function shall open the file and return a non-negative integer representing the lowest numbered unused file descriptor. Otherwise, -1 shall be returned and errno set to indicate the error. No files shall be created or modified if the function returns -1.
The failure reason is stored in errno
, you can read its value and check if it's one of the possible failures reasons EACCES
, EEXIST
, EINTR
.. etc, or just use perror
to print the error message.
Upvotes: 3
Reputation: 272497
From the man page:
open()
returns a file descriptor, a small, nonnegative integer.
and then:
open()
andcreat()
return the new file descriptor, or -1 if an error occurred
Upvotes: 23