250 likes | 257 Views
Learn the intricacies of the UNIX Kernel Source Code, including procedures, functions, and code structure with examples from John Lions' Commentary on Unix 6th Edition. Explore essential programming concepts and complex declarations in C language.
E N D
제22강 : Introduction to UNIX Kernel Source Code Unix Kernel Source CodeJohn Lions, Lions' Commentary on Unix 6th Edition :with Source Code Peer-to-Peer Communications.
procedures procedures
Procedures (Functions) • Starts from main() • Some functions are exported (system calls) • C calls assembler procedures • eg spl3() spl4() • HW starts from assembler code • Assembler initialize then calls C main() • main() • interrupt() …. • Naming convention • Uppercase – #defined symbols • Fields within structs (one letter) p_ u_ i_ b_ s_ f_ d_ We minimize hardware (pdp-11) dependent part
Back to C: Complex Declarations [Notation] [Rule] 1. closer to id 2. [], () have higher priority than * 3. Parenthesis used to grouping – highest 4. .Is higher than * ( ) --- function [ ] --- array * --- pointer [Example] In Declarations char * a(); function returning pointer to char char (* b) (); pointer to function returning char char * c() [3]; function returning pointer to 3-element array of char char * d[4] (); array of pointers to function returning char char (* e) [4]; pointer to 4 element array of char char *f[2][4]; 2 element array of pointers to 4 element array of char char (*g)[2][4]; pointer to 2 element array of (4 element array of char) ** Evaluation order C language operator precedence rule
structure type variables At different places --- Set up structure template (or structre tag or type name) Define variable Just variable definition struct { int x; int y; } var0; typedef struct { int x; int y; } point; ---------------------------- struct pointvar2 struct point { int x; int y; } var1; Set up struct template (or structure tag or type name) Also define variable --- do both at the same place variable structure tag
Chapter Three • Back to C Language (3-5) Complex declaration: struct cdevsw { (4635) int (*d_open) (); int (*d_close) (); int (*d_read) (); int (*d_write) (); int (*d_sgtty) (); } cdevsw[]; Array instantiation & initial values (4669) (15-1-3) (15-2-1) d_open() d_close() d_open()
d_open() d_close() d_open()
Memory X 120: 3 ---- back to C ----Pointers int x, y; int *p, *q; /* pointer to int */ x=1; /* LHS is address of x */ y=x; /* RHS is value of x */ value (originally at RHS) p=&x; /*to get address of x */ y=*p; /* to get value stored in this address */ address (originally at RHS)
Back to C: Functions function name or pointer pointer (1) Pointerto function char (*a)(), open(); /*a is pointer to function returning char*/ a =open;/*Functionname alone==pointer to function*/ /*i.e. not followed by ( )*/ /*open is pointer to function returning char*/ (2)Calling a Function open(2);/* function name followed by ( ) */ open(2);/* function pointer followed by ( ) */ (*open)(2); /* dereferenced function pointer function itself*/ a(2); /* function name followed by ( ) */ (*a)(2);/* function pointer followed by ( ) */ (pp. 293-306, A Book on C, 4-th Ed, Addison Wesley) We can choose between the two: open is name of a function open is pointer to function Hence open == &open (p 306) --- (*f)(k) --- f pointer to function *f function itself (*f)(k) invoke function pointer address_of (function name)
-= d_close() d_read() d_write() d_open() d_open() d_close() d_close() d_read() d_read() d_write() d_write() d_open() d_close() d_read() d_write()
d_close() d_read() d_write() d_open() d_close() d_read() d_write() d_open() d_close() d_read() d_write()
&lpopen address_of (function name) i.e. pointer to function
priority of indirection is lower than struct’s component selection ** Evaluation order Follow C precedence rule Complicated expression 6234 (3-7) (*cdevsw[maj].d_open)(dev,rw); array struct field within struct (pointer to function) invoke the function (3-5) sleep(&buffer); address variable – used as bit pattern wakeup(&buffer); d_open() d_open()
If (a & B) (3-4) • Mask bits & test (a is variable, B is constant) • char a; /* 8 bits*/ (3-5) if (a & 0377) • When char a is loaded into register, which is 16 bit • Sign extension occur ( 11000000 1111111111000000 ) • Masking with 0377 (111111112) eliminates high order bits • if @ is a binary operator x =@ a; x = x @ a • *b++ = *a++; (3-2) • copy the word (*) C precedence rule … • increment pointers(++)
Precedence Symbols Operator Names Associativity highest a[j], f(..) a.c array subscript, function call struct’s component selection ++ -- Postfix inc & dec ++ -- ! - + & * Prefix inc & dec logical not, unary negation, plus, address of, indirection (pointer) right (type name) cast right * / % + - binary < > <= >= relational == != equality && logical and || logical or lowest = += -= *= /= %= assignment right Back to C: Operator Precedence left left left left left left left left