File Control Block

Source: Wikipedia, the free encyclopedia.

A File Control Block (FCB) is a file system structure in which the state of an open file is maintained. A FCB is managed by the operating system, but it resides in the memory of the program that uses the file, not in operating system memory. This allows a process to have as many files open at one time as it wants, provided it can spare enough memory for an FCB per file.

The FCB originates from CP/M and is also present in most variants of DOS, though only as a backwards compatibility measure in MS-DOS versions 2.0 and later. A full FCB is 36 bytes long; in early versions of CP/M, it was 33 bytes. This fixed size, which could not be increased without breaking application compatibility, led to the FCB's eventual demise as the standard method of accessing files.

The meanings of several of the fields in the FCB differ between CP/M and DOS, and also depending on what operation is being performed. The following fields have consistent meanings:[1]

Offset Byte
size
Contents
0x00 1 Drive number — 0 for default, 1 for A:, 2 for B:, ...
0x01 8 File name and extension — together these form a 8.3 file name.
0x09 3
0x0C 20 Implementation dependent — should be initialised to zero before the FCB is opened.
0x20 1 Record number in the current section of the file — used when performing sequential access.
0x21 3 Record number to use when performing random access.

The 20-byte-long field starting at offset 0x0C contained fields which (among others) provided further information about the file:[2]

Offset Byte
size
Contents
0x0E 2 File's record length in bytes.
0x10 4 Total file size in bytes.
0x14 2 Date of last modification to file contents.
0x16 2 Time of last modification.

Further values were used by newer versions of DOS until new information could no longer fit in these 20 bytes. Some preceding "negative offset" bytes were squeezed from reserved spaces in CP/M Zero Page and DOS Program Segment Prefix for storing file attributes.[1]

Usage

In CP/M,

UNIX and its derivatives.[3]
File handles are simply consecutive integer numbers associated with specific open files.

If a program uses the newer file handle API to open a file, the operating system will manage its internal data structure associated with that file in its own memory area. This has the great advantage that these structures can grow in size in later operating system versions without breaking compatibility with application programs; its disadvantage is that, given the rather simplistic

FCBS= directive in the CONFIG.SYS file, it may be increased beyond that number if necessary. Under DR-DOS, both FILES and FCBS come from the same internal pool of available handles structures and are assigned dynamically as needed.[5]

FCBs were supported in all versions of MS-DOS and

FAT32 filesystem. Windows 95, Windows 98 and Windows Me do not support the use of FCBs on FAT32 drives due to its 32-bit cluster numbers,[4] except to read the volume label. This caused some old DOS applications, including WordStar
, to fail under these versions of Windows.

The FCB interface does not work properly on Windows NT, 2000, etc. either – WordStar does not function properly on these operating systems. DOS emulators DOSEMU and DOSBox implement the FCB interface properly, thus they are a way to run older DOS programs that need FCBs on modern operating systems.

Disk Transfer Area

A companion data structure used together with the FCB was the Disk Transfer Area (DTA).[2] This is the name given to the buffer where file contents (records) would be read into/written from. File access functions in DOS that used the FCB assumed a fixed location for the DTA, initially pointing to a part of the PSP (see next section); this location could be changed by calling a DOS function, with subsequent file accesses implicitly using the new location.

With the deprecation of the FCB method, the new file access functions which used file handles also provided a means to specify a memory buffer for file contents with every function call, such that maintaining concurrent, independent buffers (either for different files or for the same file) became much more practical.

Program Segment Prefix & Program Initialisation

Every DOS executable started from the shell (COMMAND.COM) was provided with a pre-filled 256-byte long data structure called the Program Segment Prefix (PSP). Relevant fields within this structure include:[2]

Offset Byte
size
Contents
0x02 2 Available memory for the program in 16-byte chunks.
0x2C 2 Segment address containing the program's environment variables.
0x5C 16 Prepared FCB for first command line argument (unopened).
0x6C 20 Prepared FCB for second command line argument (unopened).
0x80 1 Length of command line.
0x81 127 Command line contents.

This data structure could be found at the beginning of the data segment whose address was provided by DOS at program start in the DS and ES segment registers. Besides providing the program's command line verbatim at address 0x81, DOS also tried to construct two FCB's corresponding to the first two words in the command line, the purpose being to save work for the programmer in the common case where these words were filenames to operate on. Since these FCB's remained unopened, no problem would ensue even if these command line words did not refer to files.

The initial address for the DTA was set to overlay the area in the PSP (at address 0x80) where the command line arguments were stored, such that a program needed to parse this area for command line arguments before invoking DOS functions that made use of the DTA (such as reading in a file record), unless the program took care to change the address of the DTA to some other memory region (or not use the DTA/FCB functions altogether, which soon became deprecated in favour of file handles).

See also

References

  1. ^ a b c "FCB - Standard DOS File Control Block".
  2. ^ .
  3. ^
    OCLC 16581341. (xix+1570 pages; 26 cm) (NB. This edition was published in 1988 after extensive rework of the withdrawn 1986 first edition by a different team of authors. [1]
    )
  4. ^
    ISBN 978-0-201-60835-9. (xxvi+738+iv pages, 3.5"-floppy [2][3]) Errata: [4][5][6]
  5. OpenDOS 7.01
    , including the description of many undocumented features and internals. It is part of the author's yet larger MPDOSTIP.ZIP collection maintained up to 2001 and distributed on many sites at the time. The provided link points to a HTML-converted older version of the NWDOSTIP.TXT file.)

Further reading