Docsity

C Programming - Operating Systems - Project | CS 326, Study Guides, Projects, Research of Operating Systems

Toggle sidebar
bg1
usf CSCS
UNIVERSITY of SAN FRANCISCO
department of computer science
C Programming
CS 326 Operating Systems
Greg Benson
1
Why C for OS Implementation?
!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
Many C types map to CPU registers
int x = 3, y = 7;
x = x + y;
movl $3, %eax
movl $7, %ebx
addl %ebx, %eax
2
C Basics
!Comments
!/* comment here */
!// comment to end of line
!File structure
!Header files (.h) and C files (.c)
!Blocks enclosed in braces {}
!Statements terminated with semicolon “;”
!See lec01/src/helloloop.c
3
C DataTypes
!Native types
!char, unsigned char, int unsigned int, float, double
!bool, arrays, enum
!structs
!pointers
!Programmer-defined types
!typedef, structs, and pointers
4
The byte is the least common
denominator of all C types!
Different Architectures
Linux x86-64
sizeof(char) = 1
sizeof(int) = 4
sizeof(long) = 8
sizeof(long long) = 8
sizeof(float) = 4
sizeof(double) = 8
sizeof(int *) = 8
Mac OS X G5 PPC
sizeof(char) = 1
sizeof(int) = 4
sizeof(long) = 4
sizeof(long long) = 8
sizeof(float) = 4
sizeof(double) = 8
sizeof(int *) = 4
Mac OS X G5 PPC64
sizeof(char) = 1
sizeof(int) = 4
sizeof(long) = 8
sizeof(long long) = 8
sizeof(float) = 4
sizeof(double) = 8
sizeof(int *) = 8
#include<stdio.h>
int main(int argc, char **argv)
{
printf("sizeof(char) = %d\n", sizeof(char));
printf("sizeof(int) = %d\n", sizeof(int));
printf("sizeof(long) = %d\n", sizeof(long));
printf("sizeof(long long) = %d\n", sizeof(long long));
printf("sizeof(float) = %d\n", sizeof(float));
printf("sizeof(double) = %d\n", sizeof(double));
printf("sizeof(int *) = %d\n", sizeof(int *));
return 0;
}
Linxu x86-32
sizeof(char) = 1
sizeof(int) = 4
sizeof(long) = 4
sizeof(long long) = 8
sizeof(float) = 4
sizeof(double) = 8
sizeof(int *) = 4
5
See lec01/src/sizes.c
C Operators
!Bitwise operators (used for bitmask ops)
!&,|,~
!See lec01/src/mask.c
!Relational operators
! <,>,<=,>=,==,!=
!Use parenthesis to force evaluation order
! if ((~(a&b)) == c) then {x = 1;}
6
pf3
pf4

Partial preview of the text

Download C Programming - Operating Systems - Project | CS 326 and more Study Guides, Projects, Research Operating Systems in PDF only on Docsity!

usfCSCS

UNIVERSITY department of computer science of SAN FRANCISCO

C Programming

CS 326 Operating Systems Greg Benson 1

Why C for OS Implementation?

! 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

Many C types map to CPU registers

int x = xx = 3,+ y; y = 7;^ movl $3, %eax movl $7, %ebx addl %ebx, %eax 2

C Basics

! Comments

! /*^ comment^ here^ */

! //^ comment^ to^ end^ of line

! File structure

! Header files (.h) and C files (.c)

! Blocks enclosed in braces {}

! Statements terminated with semicolon “;”

! See^ lec01/src/helloloop.c

C DataTypes

! Native types

! char, unsigned char, int unsigned int, float, double

! bool, arrays, enum

! structs

! pointers

! Programmer-defined types

! typedef, structs, and pointers

The byte is the least common

denominator of all C types!

Different Architectures

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

C Operators

! Bitwise operators (used for bitmask ops)

! See lec01/src/mask.c

! Relational operators

! Use parenthesis to force evaluation order

! if^ ((~(a&b))^ ==^ c) then {x = 1;}

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

Memory is a bit more

complicated in the kernel. We

will learn more in Project 0

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/ ! Create a local cs326 project directory ! Create a prj-1 directory ! Use svn import to create the cs326 project