File descriptor assignment in C

When sockets are created or files are opened / created in C, is the file descriptor that is assigned to the socket / file guaranteed to be available for the descriptor with the lowest value? What does the C spec say about the purpose of the file descriptor in this regard, if anything?

+3
source share
6 answers

It is not guaranteed to be the lowest and depends on implementation (1). In general, however, a procedure that assigns open file descriptors uses a method that gives you the first open. Perhaps right after a few lower ones, free you, leaving you with a higher descriptor than you might expect.

, , - select, , , .

(1) , , IEEE, , . IEEE open(), , , .

+5

, C, , . Linux , .

+4

- ? ( stdin/stdout/stderr) . int ( ), , .

+2

; C , / [ ] FILE, .

0

@aib open(), close(), lseek(), read(), write() . -.

@Kyle - select(). .

0

C spec , . Unix, man- open (2) : " , , , ".

This helps if you are trying to attach a specific file to a specific descriptor. Suppose you want to redirect stderr to / dev / null. Sort of

close (2); open ("/ dev / null", O_WRONLY);

must do it. You must, of course, grab the fd returned open and make sure it is 2.

0
source

Source: https://habr.com/ru/post/1696750/


All Articles