Implementing Modules in C

You will be implementing the OS 502 in the C programming language.   Unfortunately, C has no direct language support for modules.  Howeve, some of the important aspects of modules can be approximated using the separate compilation mechanisms of C.  In this chapter, we briefly describe how to achieve this.   Some other conventions in organizing your files will also be introduced.

Among the set of files that make up a C program we distinguish three different varieties and give them distinguishable names.  Files having the extension ".c" (for C code) contain the definitions for a given module.   These include constants, macros, types, variables, and functions.  Of these definitions, those that may be accessed by other modules are declared in a ".h" file of with the same base filename as the definition file.  This constitutes the public export interface to the given module.  Constants, macros and types that are used by a number of modules and don't belong to any module in particular are collected in ".h" files that are named <name>_g.h (for global header file).

A definitions file should have its contents in the following order:

  1. Inclusion of header files.
  2. Inclusion of declarations from other modules that are being imported.
  3. Definitions of macros, types, and variables to be exported.
  4. Definitions of macros, types, and variables strictly local to the module.
  5. Definitions of functions to be exported.
  6. Definitions of functions strictly local to the module.

Variables and functions that are strictly local to the module should be defined with the static attribute so that they will not be visble from other modules (files).  The only exception to this rule is the function main() which should not be exported but slso should not be defined using static since it is the entry point to the program.   In our examples, we will use the word "HIDDEN" rather than static to better capture our intended use of the mechanism.

You will use the #include mechanism of the C preprocessor to include the header files and the declarations of other modules ("<name>.h") that are being imported by the current module.

A public export interface header file corresponding to a definitions file should have the following structure:

  1. Constants, macros, and types corresponding to item 3. above.
  2. Declarations of the variables corresponding to item 3. above.
  3. Declarations of the functions corresponding to item 5. above.

Finally, a global header file (<name>_g.h) contains:

  1. Literals introduced for program readability.  For example,

    #define TRUE     1
    #define FALSE    0
    #define PAGESIZE 512
    #define HIDDEN   static

  2. Utility macro definitions.  For example,

    #define ODD(x)   ((x) & 01)

  3. Parameters of the program or set of modules.  For example,

    #define MEMSIZE  (32 * PAGESIZE)
    #define MAXPROC   20

Note the use of capital letters for names of constants and macros in order to distinguish them from variables and functions.

You are expected to adhere strictly to these conventions in the code you produce.   A large percentage of your style grade will be based on the structuring of your files according to these conventions.