fileno()

Donn Cave donn at u.washington.edu
Mon Sep 18 13:47:05 EDT 2000


Quoth "Arild Hansen" <arildh at stud.cs.uit.no>:
| What is the exact purpose of fileno()? I keep searching for a good
| explanation of this function returning a file handler to an object, but what
| is the precise meaning and purpose of this? All help greatly appreciated !

The file object is an I/O buffer and a device.  The C library allocates
space for the buffer in your process memory, and when you read or write
the data comes from or goes to that buffer.  The stdio C library functions
manage the buffer by reading or writing to the device.

Since physical device access is a very expensive operation for the computer,
buffering like that basically allows you to work with small or random size
I/O requests while economizing on system resources.  However, there are
times when you need to do something that the stdio library doesn't account
for, like select(), or a filesystem lock, or some terminal driver function.

The fileno() function exposes the file object's file descriptor or unit
number, which on UNIX is the raw device.  Select() already knows about
fileno(), so you can give it a socket or whatever and it will call fileno()
itself, but usually it's up to you to call fileno() and get the descriptor.

Of course the socket fileno() function is more or less the same idea as
the file object's fileno(), but the socket isn't a file object and doesn't
have its own buffer.  (On UNIX you can make a file object from a socket,
but it's difficult to make that work both properly and efficiently with
select().)  The result from socket fileno() is a file descriptor on UNIX,
but it isn't necessarily so on other operating systems.

You can get file descriptors directly if you use the posix.open() function,
(A.K.A. os.open().)

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list