1 / 25

Understanding UNIX Kernel Source Code: Commentary and Procedures

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.

juangray
Download Presentation

Understanding UNIX Kernel Source Code: Commentary and Procedures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 제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.

  2. procedures procedures

  3. Part II Commentary

  4. user

  5. proc

  6. parameters

  7. 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

  8. 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

  9. 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

  10. 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()

  11. d_open() d_close() d_open()

  12. 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)

  13. 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)

  14. -= 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()

  15. d_close() d_read() d_write() d_open() d_close() d_read() d_write() d_open() d_close() d_read() d_write()

  16. &lpopen address_of (function name) i.e. pointer to function

  17. 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()

  18. 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(++)

  19. 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

More Related