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

Slides:



Advertisements
Similar presentations
Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Advertisements

Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Programming Languages and Paradigms The C Programming Language.
Washington WASHINGTON UNIVERSITY IN ST LOUIS Brief Introduction to the C Programming Language Fred Kuhns Applied Research Laboratory,
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Command Line arguments Main function: int main(argc, char *argv[]) –argc is the count of command line arguments –argv is an array of strings argv[0] is.
. Compilation / Pointers Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Guide To UNIX Using Linux Third Edition
Introduction to C Language
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2007 Pearson Education, Inc. All rights reserved C Preprocessor.
CS 161 Introduction to Programming and Problem Solving Chapter 13 C++ Preprocessor Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
By Sidhant Garg.  C was developed between by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.  Unlike previously.
C Tutorial Session #2 Type conversions More on looping Common errors Control statements Pointers and Arrays C Pre-processor Makefile Debugging.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
1 Washington WASHINGTON UNIVERSITY IN ST LOUIS Brief Introduction to the C Programming Language Hanumantha Rao.N Ganesh Technology.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Introduction to C For the participants of Practical Course „Scientific Computing and Visualization“
C By Example 1 The assumption is that you know Java and need to extend that knowledge so you can program in C. 1. Hello world 2. declarations 3. pass by.
Fundamental Data Types, Operators and Expressions Kernighan/Ritchie: Kelley/Pohl: Chapter 2 Chapter 2, 3.
(language, compilation and debugging) David 09/16/2011.
CS 261 – Recitation 2 Fall 2013 Oregon State University School of Electrical Engineering and Computer Science.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Navigating the C++ Development Environment
Pointers *, &, array similarities, functions, sizeof.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
C LANGUAGE Characteristics of C · Small size
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
C Primer Session – 1/25/01 Outline Hello World Command Line Arguments Bit-wise Operators Dynamic Memory / Pointers Function Parameters Structures.
Unary, Binary, logical Operations, Explicit type conversion Lecture 6 Instructor: Haya Sammaneh.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
13 C Preprocessor.
C++ Lesson 1.
Beginning the Study of the C Programming Language
The Machine Model Memory
Function: Declaration
Brief Introduction to the C Programming Language
A bit of C programming Lecture 3 Uli Raich.
ECE Application Programming
Programming Languages and Paradigms
C Fundamentals & Pointers
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
C Short Overview Lembit Jürimägi.
Programming Paradigms
C Basics.
Introduction to C Programming Language
Introduction to C Programming
Computer Science 210 Computer Organization
פרטים נוספים בסילבוס של הקורס
Govt. Polytechnic,Dhangar
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
Assignment Operators Topics Increment and Decrement Operators
Programming in C Miscellaneous Topics.
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Assignment Operators Topics Increment and Decrement Operators
Programming in C Miscellaneous Topics.
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
C Programming Getting started Variables Basic C operators Conditionals
2. Second Step for Learning C++ Programming • Data Type • Char • Float
C By Example The assumption is that you know Java and need to extend that knowledge so you can program in C. 1. Hello world 2. declarations 3. pass.
Programming Languages and Paradigms
C Language B. DHIVYA 17PCA140 II MCA.
Assignment Operators Topics Increment and Decrement Operators
Review.
INTRODUCTION TO C.
Presentation transcript:

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

Lab/Office Hour Lab: 8:00 ~ 8:50 AM Wed/Thurs Klamath 026 Office: 9:00 ~ 10:00 AM Wed/Thurs DES 224

Guideline C Programming Language UNIX/Linux commands Make Version Control

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); }

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.

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__)

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)

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

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

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;

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?

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.

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) {... }

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) {... }

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 NA x y

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 0x3b NA 0x3d4 0x3d0 NA x y

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

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

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

UNIX/Linux Commands – Kernel Module lsmod insmod rmmod dmesg

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

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

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!

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

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

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

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

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

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 ware

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

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

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

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 git push -u origin master