CS 326 Operating Systems C Programming Greg Benson Department of Computer Science University of San Francisco
Why C? Fast (good optimizing compilers) Not too high-level (Java, Python, Lisp) Not too low-level (Assembly) Easy manipulation of bits, bytes, words, and pointers Explicit memory allocation Most OS kernels written in C with some assembly 2
C Basics Comments /* comment here */ // comment to end of line File structure Header files (.h) C files (.c) Blocks enclosed in braces {} Statements terminated with semicolon ; 3
C Types and Functions Types char, unsigned char, int unsigned int float, double arrays, structs, pointers, enum Functions Arguments Local variables Return value 4
C Operators Bitwise operators &,,~ Relational operators <,>,<=,>=,==,!= Use parenthesis to force evaluation order if ( (~(a&b)) == c) then { x = 1; } 5
Control If / else / else if for (x = 1, x < N, x++) { /* code */ } while (x > 1) { /* code */ } switch / case / default goto 6
C Practice All arguments to functions are passed by value Use pointers to pass by reference Explicit memory allocation with malloc() and free() Structs with pointers to build linked data structures Pointers to functions Macros for speed Globals variables are not always bad New types with typedef 7
The C Library The standard C library (libc) Strings (strcpy, strlen, etc.) Buffered I/O (fopen, fread, fwrite, etc.) Math (sin, cos, pow, etc.) Others Use man 3 name to get documentation 8
See lec01/src/args.c 9 Command Line Arguments C and UNIX define a way to pass arguments to a program int main(int argc, char *argv[]) argc is the number of args (including the command itself) argv is an array of strings (actually it is an array of pointers to strings)
Basic Output with printf printf is a sophisticated function for generating output printf can output the values of C variables printf can help format output See lec/01/printf.c 10
Working with Strings In C, strings are an array of characters with a null termination \0 character at the end The length of a string does not include the termination character The storage for a string must include space for the termination character Arrays and pointers can be used to access strings Be sure to know where the string is stored See lec01/src/strings.c 11
Buffered I/O There are two ways to read/write data Directly use system calls open(), close(), read(), write() Use buffered I/O fopen(), fclose(), fread(), fwrite(), printf() Buffered I/O is used for better performance See lec01/src/linenums.c 12
The C Preprocessor Before compilation, C source files are preprocessed (e.g., translated) The preprocessor (cpp) handles Include files (#include<foo.h>) Macro expansion (#define NMAX 3) Conditional compilation Most C programs use the preprocessor, especially OS kernels 13
Include Files System include files (where are they?) #include<stdio.h> Program local include files #include foo.h General rule: do not declare storage within and include file, only data types, function prototypes and macros Add an include directory to search path: gcc -o main main.c -I/home/benson/src Now you can use #include<foo.h> 14
Preprocessor Macros Simple #define MAXCOUNT 3 Parameterized #define ADD(x,y) x + y Complex #define insert(e, l) { \ e->next =l->head; \ l->head = e; \ } while(0) 15
Conditional Compilation Used for portability and testing #ifdef LINUX printf( Linux\n ); #else printf( Unknown\n ); #endif /* LINUX */ 16
Include File Management Conditional compilation can be used to make sure only one instance of an include file is preprocessed #ifndef _FOO_H_ #define _FOO_H_... header contents... #endif /* _FOO_H_ */ 17
Linked Lists and Structures Pointers and linked structures are used extensively inside an OS kernel Basic idea: allocate memory, create links between allocated memory blocks Used for lists, trees, hash tables, etc. Can be tricky to debug, so we need to have a good understanding to avoid common mistakes See lec01/src: Makefile, namelist.h namelist.c, nametest.c 18