Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS 415 – Operating System (Lab) CIS 415 Lab 1 Slides based on Fred Kuhns, Kevin Butler and Joe Pletcher Dave Tian.

Similar presentations


Presentation on theme: "CIS 415 – Operating System (Lab) CIS 415 Lab 1 Slides based on Fred Kuhns, Kevin Butler and Joe Pletcher Dave Tian."— Presentation transcript:

1 CIS 415 – Operating System (Lab) CIS 415 Lab 1 Slides based on Fred Kuhns, Kevin Butler and Joe Pletcher Dave Tian

2 Lab/Office Hour Lab: 8:00 ~ 8:50 AM Wed/Thurs Klamath 026 Office: 9:00 ~ 10:00 AM Wed/Thurs DES 224 daveti@cs.uoregon.edu

3 Guideline C Programming Language UNIX/Linux commands Make Version Control

4 C Programming Language – Hello World gcc -o hello hello.c./hello /* you generally want to * include stdio.h and * stdlib.h * */ #include int main (void) { printf(“Hello World\n”); exit(0); }

5 C Programming Language – Standard Header Files stdio.h – file and console (also a file) IO: p error, printf, open, close, read, write, scanf, etc. stdlib.h - common utility functions: malloc, calloc, strtol, atoi, etc string.h - string and byte manipulation: strlen, strcpy, strcat, memcpy, memset, etc. ctype.h – character types: isalnum, isprint, isupport, tolower, etc. errno.h – defines errno used for reporting system errors math.h – math functions: ceil, exp, floor, sqrt, etc. signal.h – signal handling facility: raise, signal, etc stdint.h – standard integer: intN_t, uintN_t, etc time.h – time related facility: asctime, clock, time_t, etc.

6 C Programming Language – C Preprocessor The C preprocessor permits you to define simple macros that are evaluated and expanded prior to compilation. Commands begin with a ‘#’. Abbreviated list: #define : defines a macro #undef : removes a macro definition #include : insert text from file #if : conditional based on value of expression #ifdef : conditional based on whether macro defined #ifndef : conditional based on whether macro is not defined #else : alternative #elif : conditional alternative defined() : preprocessor function: 1 if name defined, else 0 #if defined(__NetBSD__)

7 C Programming Language – Types and Operators Basic data types Types: char, int, float and double Qualifiers: short, long, unsigned, signed, const Constant: 0x1234, 12, “Some string” Enumeration: Names in different enumerations must be distinct enum WeekDay_t {Mon, Tue, Wed, Thur, Fri}; enum WeekendDay_t {Sat = 0, Sun = 4}; Arithmetic: +, -, *, /, % prefix ++i or --i ; increment/decrement before value is used postfix i++, i--; increment/decrement after value is used Relational and logical:, =, ==, !=, &&, || Bitwise: &, |, ^ (xor), >, ~(ones complement)

8 C Programming Language – Operator Precedence TokensOperatorClass Pr ec ed en ce Associates names, literals simple tokensprimary 16 n/a a[k] subscriptingpostfixleft-to-right f(...) function callpostfixleft-to-right. direct selectionpostfixleft-to-right -> indirect selectionpostfixleft to right ++ -- increment, decrementpostfixleft-to-right (type){init} compound literalpostfixleft-to-right ++ -- increment, decrementprefixright-to-left sizeof sizeunaryright-to-left ~ bitwise notunaryright-to-left ! logical notunaryright-to-left - + negation, plusunaryright-to-left & address ofunaryright-to-left * indirection (dereference) unaryright-to-left TokensOperatorClass Pr ec ed en ce Associates names, literals simple tokensprimary 16 n/a a[k] subscriptingpostfixleft-to-right f(...) function callpostfixleft-to-right. direct sectionpostfixleft-to-right -> indirect selectionpostfixleft to right ++ -- increment, decrementpostfixleft-to-right (type){init} compound literalpostfixleft-to-right ++ -- increment, decrementprefixright-to-left sizeof sizeunaryright-to-left ~ bitwise notunaryright-to-left ! logical notunaryright-to-left - + negation, plusunaryright-to-left & address ofunaryright-to-left * indirection (dereference) unaryright-to-left TokensOperatorClass Pr ec ed en ce Associates (type) castsunary14right-to-left * / % multiplicativebinary13left-to-right + - additivebinary12left-to-right > left, right shiftbinary11left-to-right >= relationalbinary10left-to-right == != equality/ineq.binary9left-to-right & bitwise andbinary8left-to-right ^ bitwise xorbinary7left-to-right | bitwise orbinary6left-to-right && logical andbinary5left-to-right || logical orbinary4left-to-right ?: conditionalternary3right-to-left = += -= *= /= %= &= ^= |= >= assignmentbinary2right-to-left, sequential eval.binary1left-to-right

9 C Programming Language – Structures and Unions structures struct MyPoint {int x, int y}; typedef struct MyPoint MyPoint_t; MyPoint_t point, *ptr; point.x = 0;point.y = 10; ptr = &point; ptr->x = 12; ptr->y = 40; unions union MyUnion {int x; MyPoint_t pt; struct {int 3; char c[4]} S;}; union MyUnion x; Can only use one of the elements. Memory will be allocated for the largest element

10 C Programming Language – if/else if (a < 10) printf(“a is less than 10\n”); else if (a == 10) printf(“a is 10\n”); else printf(“a is greater than 10\n”); If you have compound statements then use brackets (blocks) if (a 10) { c = a * b; b = 0; printf(“a = %d, a\’s address = 0x%08x\n”, a, (uint32_t)&a); } else { c = a + b; b = a; } These two statements are equivalent: if (a) x = 3; else if (b) x = 2; else x = 0; if (a) x = 3; else {if (b) x = 2; else x = 0;} Is this correct? if (a) x = 3; else if (b) x = 2; else (z) x = 0; else x = -2;

11 C Programming Language - switch int c = 10; switch (c) { case 0: printf(“c is 0\n”); break;... default: printf(“Unknown value of c\n”); break; } What if we leave the break statement out? Do we need the final break statement on the default case?

12 C Programming Language - loops for (i = 0; i < MAXVALUE; i++) { dowork(); } while (c != 12) { dowork(); } do { dowork(); } while (c < 12); flow control break – exit innermost loop continue – perform next iteration of loop Note, all these forms permit one statement to be executed. By enclosing in brackets we create a block of statements.

13 C Programming Language - functions Always use function prototypes int myfunc (char *, int, struct MyStruct *); int myfunc_noargs (void); void myfunc_noreturn (int i); C and C++ are call by value, copy of parameter passed to function C++ permits you to specify pass by reference if you want to alter the parameter then pass a pointer to it (or use references in C++) If performance is an issue then use inline functions, generally better and safer than using a macro. Common convention define prototype and function in header or name.i file static inline int myinfunc (int i, int j); static inline int myinfunc (int i, int j) {... }

14 C Programming Language - functions Always use function prototypes int myfunc (char *, int, struct MyStruct *); int myfunc_noargs (void); void myfunc_noreturn (int i); C and C++ are call by value, copy of parameter passed to function C++ permits you to specify pass by reference if you want to alter the parameter then pass a pointer to it (or use references in C++) If performance is an issue then use inline functions, generally better and safer than using a macro. Common convention define prototype and function in header or name.i file static inline int myinfunc (int i, int j); static inline int myinfunc (int i, int j) {... }

15 C Programming Language – Array and Pointer 0x3dc 0x3d8 0x3cc 0x3c8 0x3c4 0x3c0 Note: The compiler converts z[1] or *(z+1) to Value at address (Address of z + sizeof(int)); In C you would write the byte address as: (char *)z + sizeof(int); or letting the compiler do the work for you (int *)z + 1; Step 1: int main (int argc, argv) { int x = 4; int *y = &x; int *z[4] = {NULL, NULL, NULL, NULL}; int a[4] = {1, 2, 3, 4};... 0x3bc 0x3b8 0x3b4 0x3b0 0x3d4 0x3d0 z[3] z[2] z[1] z[0] a[3] a[2] a[1] a[0] 4 0x3dc 0 0 0 0 4 3 2 1 NA x y

16 C Programming Language – Array and Pointers (II) Step 1: int main (int argc, argv) { int x = 4; int *y = &x; int *z[4] = {NULL, NULL, NULL, NULL}; int a[4] = {1, 2, 3, 4}; Step 2: Assign addresses to array Z z[0] = a; // same as &a[0]; z[1] = a + 1; // same as &a[1]; z[2] = a + 2;// same as &a[2]; z[3] = a + 3;// same as &a[3]; 4 0x3dc 0x3d8 0x3bc 0x3b8 0x3b4 0x3b0 0x3cc 0x3c8 0x3c4 0x3c0 0x3bc 0x3b8 0x3b4 0x3b0 4 3 2 1 NA 0x3d4 0x3d0 NA x y

17 UNIX/Linux Commands – File Operation touch mkdir cp mv chmod ls cat/dog? rm

18 UNIX/Linux Commands – Process, Performance and Debug ps top netstat ifconfig uname ltrace/strace valgrind sysctl

19 UNIX/Linux Commands - Utilities date who grep find sed awk head/tail tar man

20 UNIX/Linux Commands – Kernel Module lsmod insmod rmmod dmesg

21 UNIX/Linux Commands - Others vi/vim emacs gcc gdb objdump make ssh/scp wget/curl

22 UNIX/Linux Commands – Build from source gunzip XXX.gz bunzip2 XXX.bz2 tar -xvf XXX.tar cd XXX./configure make make install...README

23 Make - Intro Manually compile gcc -Wall -g -o helloX hello1.c hello2.c hello3.c... g++ -Wall -g -o helloY hello1.cpp hello2.cpp hello3.cpp... When the project is NOT trivial or code change (bug fix) is frequent... YOU have to REPEAT the command for times and times!

24 Make – DRY! Write a Makefile Specify the source Specify the target Specify the dependency Specify the compiler options Specify the other actions...Use TAB! Run 'make'/'make hello'/'make -f myHelloMakeFile'/'make all'/'make clean'...

25 Make – Example 1 all: g++ main.cpp hello.cpp factorial.cpp -o hello

26 Make – Example 2 all: hello hello: main.o factorial.o hello.o g++ main.o factorial.o hello.o -o hello main.o: main.cpp g++ -c main.cpp factorial.o: factorial.cpp g++ -c factorial.cpp hello.o: hello.cpp g++ -c hello.cpp clean: rm -rf *o hello

27 Make – Example 2 all: hello hello: main.o factorial.o hello.o g++ main.o factorial.o hello.o -o hello main.o: main.cpp g++ -c main.cpp factorial.o: factorial.cpp g++ -c factorial.cpp hello.o: hello.cpp g++ -c hello.cpp clean: rm -rf *o hello

28 Make – Example 3 # I am a comment, and I want to say that the variable CC will be # the compiler to use. CC=g++ # Hey!, I am comment number 2. I want to say that CFLAGS will be the # options I'll pass to the compiler. CFLAGS=-c -Wall all: hello hello: main.o factorial.o hello.o $(CC) main.o factorial.o hello.o -o hello main.o: main.cpp $(CC) $(CFLAGS) main.cpp factorial.o: factorial.cpp $(CC) $(CFLAGS) factorial.cpp hello.o: hello.cpp $(CC) $(CFLAGS) hello.cpp clean: rm -rf *o hello

29 Version Control - Intro Version Control The way to control different version of software... The way to provide convenience for team work... The way to implement an online code repos service... CVS SVN Git http://en.wikipedia.org/wiki/Comparison_of_revision_control_soft ware

30 Version Control - SVN svnadmin create svn co - Checkout a repo svn add - Add a file to a repo svn ci –m “commit message” – Check into a repo svn diff – See changes svn log - See commit messages svn help - Display SVN help

31 Version Control - Git git init – initialize the current directory to be a git repo git checkout - checks out a repo, revision, or branch git add – add a file to repo git commit –m “commit message” – commits any changes git log – display commits git branch – create a new branch git clone - copy a repo

32 Version Control – Git (Server side, ix) ssh login@ix.cs.uoregon.edu mkdir my_project.git cd my_project.git git init --bare git update-server-info #If http is desired

33 Version Control – Git (Client side, local) Install git on UNIX/Linux/Windows/MacOS cd your_project git init git add hello.c # Or use * for all files git commit -m “Well, this is my first using git...” git remote add origin login@ix.cs.uoregon.edu:my_project.git login@ix.cs.uoregon.edu git push -u origin master


Download ppt "CIS 415 – Operating System (Lab) CIS 415 Lab 1 Slides based on Fred Kuhns, Kevin Butler and Joe Pletcher Dave Tian."

Similar presentations


Ads by Google