THE WORLD ACCORDING TO C
Operating systems are generally large C (or sometimes C++) programs consisting of many pieces written by many programmers. The environment used for developing operating systems is very different from what individuals (such as students) are used to when writing small Java programs. In this section we shall give a very brief introduction to the world of writing an operating system for small-time Java programmers.
The C Language
This is not a complete guide to C language, but a short summary of some of the key differences between C and Java. Java is based on C, so there are various similarities between the two. Both are essential languages with data types, variables, and control statements, for instance. The primitive data types in C are integers (including short and long ones), characters, and floating-point numbers. Composite data types can be constructed using arrays, structures, and unions. The control statements in C are similar to those in Java, including if, switch, for, and while statements. Functions and parameters are roughly the same in both languages.
One feature that C has that Java does not is explicit pointers. A pointer is a variable that points to (i.e., contains the address of) a variable or data structure.
Consider the statements
char c 1, c2, *p;
c1 = ‘x’;
p = &c1;
c2 = *p;
which declare c1 and c2 to be character variables and p to be a variable that points to (i.e., contains the address of) a character. The first assignment stores the ASCII code for the character “c” in the variable c1. The second one gives the address of c1 to the pointer variable p. The third one gives the contents of the variable pointed to by p to the variable c2, so after these statements are carried out, c2 also includes the ASCII code for “c”. In theory, pointers are typed, so you are not supposed to assign the address of a floating-point number to a character pointer, but in practice compilers accept such assignments, albeit sometimes with a warning. Pointers are a very powerful construct, but also a great source of errors when used carelessly.
Some things that C does not have include built-in strings, threads, packages, classes, objects, type safety, and garbage collection. The last one is a show stopper for operating systems. All storage in C is either static or explicitly assigned and released by the programmer, generally with the library function malloc and free. It is the latter property – total programmer control over memory – along with explicit pointers that makes C attractive for writing operating systems. Operating systems are mainly real-time systems to some extent, even general purpose ones. When an interrupt takes place, the operating system may have only a few microseconds to perform some action or lose critical information. Having the garbage collector kick in at an arbitrary moment is intolerable.