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?
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).
Stdin, stdout, and stderr On a Unix-like operating system, the first three file descriptors, by default, are STDIN (standard input), STDOUT (standard output), and STDERR (standard error).
File Descriptors (FD) are non-negative integers (0, 1, 2, ...) that are associated with files that are opened. 0, 1, 2 are standard FD's that corresponds to STDIN_FILENO , STDOUT_FILENO and STDERR_FILENO (defined in unistd. h ) opened by default on behalf of shell when the program starts.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With