What are the possible values for file descriptors?

humanityANDpeace :

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?

Oliver Charlesworth :

From the man page:

open() returns a file descriptor, a small, nonnegative integer.

and then:

open() and creat() return the new file descriptor, or -1 if an error occurred

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What and Why? - File Descriptors

What are these file descriptors for?

What are these auxiliary file descriptors?

Is it possible to write to other file descriptors in C?

What are file descriptors, explained in simple terms?

What do /proc/fd file descriptors show?

What happens to file descriptors when the process is killed?

What's the max number of file descriptors on Android?

What's going on with closed file descriptors?

SIGIO arriving for file descriptors I did not set it for and when no IO is possible

Is it possible to wait on both channels and file descriptors at the same time in Go?

What can I assume about pthread_create and file descriptors?

What are file descriptors other than 0, 1 and 2

What are the dangers of setting a high limit to max File Descriptors per process?

What's the impact of large number of file descriptors on Java applications

What is the historical reason for limits on file descriptors (ulimit -n)

What was the reason for lseek function to return file descriptors as long integer?

Within a .vcxproj file what are the possible values for the <ConfigurationType> and what do those values mean?

Python: Cache values of descriptors

what is the difference between data descriptors and accessor descriptors?

Duplication of file descriptors in redirection

File descriptors and redirect in bash

Sharing file descriptors

Special file descriptors

closing the unwanted file descriptors

Python not closing file descriptors

Threads and file descriptors

File descriptors across exec

file descriptors and /dev/fd