


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



CS 326 Operating Systems Greg Benson 1
int x = xx = 3,+ y; y = 7;^ movl $3, %eax movl $7, %ebx addl %ebx, %eax 2
Linux x86- sizeof(char) sizeof(int) == (^14) sizeof(long) sizeof(long long) == (^88) sizeof(float) sizeof(double) == (^48) sizeof(int *) = 8 Mac OS X G5 PPC sizeof(char) sizeof(int) = 1= 4 sizeof(long) sizeof(long long) = 8 = 4 sizeof(float) sizeof(double) = 4= 8 sizeof(int *) = 4 Mac OS X G5 PPC sizeof(char) sizeof(int) == (^14) sizeof(long) sizeof(long long) == (^88) sizeof(float) sizeof(double) == (^48) sizeof(int *) = 8 #include<stdio.h> int { main(int argc, char **argv) printf("sizeof(char) printf("sizeof(int) = %d\n", sizeof(char));= %d\n", sizeof(int)); printf("sizeof(long) printf("sizeof(long long) = %d\n", sizeof(long long)); = %d\n", sizeof(long)); printf("sizeof(float) printf("sizeof(double) = %d\n", sizeof(float));= %d\n", sizeof(double)); printf("sizeof(int *) = %d\n", sizeof(int *)); }^ return 0; Linxu x86- sizeof(char) sizeof(int) = 1= 4 sizeof(long) sizeof(long long) = 8 = 4 sizeof(float) sizeof(double) = 4= 8 sizeof(int *) = 4 See lec01/src/sizes.c
Control ! If / else / else if ! for^ (x^ = 1, x^ < N, x++) { /* code / } ! while (x >^ 1)^ { / code */^ } ! switch / case / default ! goto 7 C Functions ! Arguments, local variables, return value ! Basic execution ! Caller sets arguments (in regs or on stack) ! call/jump to address of function ! Function body creates activation frame for local vars ! Function body sets return value (in reg, e.g., eax) then returns (ret or jr instruction) 8 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 ! Use bool for true/false data and functions (^9)! New types with typedef
The C Library ! The standard C library (libc) ! Strings (strcpy, strlen, etc.) ! Pintos: strlcpy, strlcat, strnlen ! I/O (open, read, write, printf, etc.) ! Math (sin, cos, pow, etc.) ! Others ! Use “man 3 name” to get documentation 10 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) ! See lec01/src/args.c 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 lec01/src/printf.c
C Struct Offsets ! What is offsetof(struct point, y)? ! What is offsetof(struct point, o)? #define offsetof(TYPE, MEMBER)
(size_t) &((TYPE *) 0)->MEMBER) struct point { int x; int y; int z; char c; float o; }; 0: x (4 byte int) 4: y (4 byte int) 8: z (4 byte int) 12: c (1 byte char) 13: 3 byte padding 16: o (4 byte float) 19 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 20 Simple Linked Lists ! Basic Idea: link struct instance to another struct instance via a pointer in the struct: ! Single or double links (also more...) struct node { struct node *next; int x; int y; } struct node *head; next x y next x y NULL x y Head 21 Linked Lists in Pintos ! Use lib/kernel/list.{h,c} ! Basic idea: ! Embed list_elem struct into existing struct ! List operations work on list_elem struct ! You can determine enclosing struct pointer via list_entry() macro ! Other points: doubly linked, sentinels, walking, ordering (^22)! See prj0/src/listtest.c Some Subversion ! You need to use subversion for all of your projects ! You have a subversion repository: ! https://www.cs.usfca.edu/svn/