File Descriptor Environment
3-2
3.1
File Descriptor Environment
In most embedded operating system environments, support for file descriptors
varies greatly. In most cases, only the bare minimum functionality is provided,
and trimmed down support functions are provided using the common “re-
served” names (read(), write(), close(), etc.).
Since this stack supports the standard sockets interface functions, and these
functions require file descriptor support, the stack provides its own small file
system. This section describes the basic mechanics of the file system.
3.1.1
Organization
The basic building block of the stack code internally is an object handle. Inter-
nally to the stack, both sockets and pipes are addressed by object handles.
However, at the application level, sockets and pipes are treated as file descrip-
tors, which are integer indices. Integer representation is necessary to support
the classic Unix concept of the select() call.
The stack API supports the use of integer file descriptors, by adding a file des-
criptor layer of abstraction to the native operating environment. It is this layer
that implements the standard sockets and file IO functions. The stack works
by associating a file descriptor table with each caller’s thread (or in our ter-
minology, ’task’). In this system, each task has its own file descriptor table. The
file descriptor index passed to a file function is used to lookup the actual file
handle contained in the task’s associated file descriptor table. This enables the
stack to support both an object oriented and the classical Unix sockets API.
Note again that file descriptors are private to each particular task thread while
the actual pipe and socket objects are addressed by object handles. It is some-
times necessary to convert a file descriptor to a handle and back again in order
to pass the object to another task.
3.1.2
Initializing the File System Environment
In order to use the file system and socket functions provided by the stack, a
task must first allocate a file descriptor table (called a file descriptor session).
This is accomplished at the application layer by calling the file descriptor func-
tion fdOpenSession(). When the task is finished using the file descriptor API,
or when it is about to terminate, fdCloseSession() is called.