Top Banner
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 1: Introduction and Review of C Programming
41

CS252: Systems Programming

Feb 23, 2016

Download

Documents

urania

CS252: Systems Programming. Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 1: Introduction and Review of C Programming. General Information. Web Page: http://www.cs.purdue.edu/homes/cs252 Office: LWSN2142K E-mail: [email protected] Textbook: - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS252: Systems Programming

CS252: Systems Programming

Ninghui LiBased on Slides by Prof. Gustavo Rodriguez-Rivera

Topic 1: Introduction and Review of C Programming

Page 2: CS252: Systems Programming

General InformationWeb Page: http://www.cs.purdue.edu/homes/cs252Office: LWSN2142K E-mail: [email protected]:

No textbook. We will use my notes and selected material in the web

Recommended: Advanced Programming in the UNIX Environment by W. Richard

Stevens. (Useful for the shell programming lab. Good as a reference book.)

Page 3: CS252: Systems Programming

Course Communications

Use Piazza for questions/answers, announcements, etc.

Use Blackboard Learn for grade distribution

Page 4: CS252: Systems Programming

Labs

There is no lab the first week.The projects will be explained in the lab sessions.E-mail administrative questions to [email protected]

Post project questions on PiazzaTAs office hours will be posted in the web page.

Page 5: CS252: Systems Programming

Grading

Grade allocation Midterm 1 (in class): 12.5% Midterm 2 (in class): 12.5% Final: 25% Projects/labs:50%

Six labs, weighted by #of weeks

Exams also include questions about the labs.

Page 6: CS252: Systems Programming

Course Organization: Part 1: C Programming Review

• Review of pointers, arrays, pointers to functions

• Compiling, Assembling, Linking, Loading, Runtime Linker, Static and Shared Libraries; Address space. Structure of a Program.

• Debugging using GDB.

Lab 1: C/C++ Programming and GDB

Page 7: CS252: Systems Programming

Course Organization: Part 2: Unix Basics and Using Unix

• Unix history & file system fundamentals• Common Unix utilities• Unix Shell scripting

Lab 2: Unix Shell Scripting.

Page 8: CS252: Systems Programming

Course Organization: Part 3: Unix Systems Programming I

• Structure of a Unix Shell• Using Lex and YACC to create parsers• Useful system calls: file creation, read,

write, close, file mode, IO redirection, pipes, fork, wait, waitpid, signals, etc.

Lab 3: Writing your own shell.

Page 9: CS252: Systems Programming

Course Organization: Part 4: Unix Systems Programming II

• OS kernel fundamentals: user mode, kernel mode, interrupts, system calls

• Processes, scheduling, • Programming with threads, thread

creation. • Race Conditions, Mutex locks.

Lab 4: Introduction to Threads.

Page 10: CS252: Systems Programming

Course Organization: Part 5: Unix Network Programming

• Internet overview: ARP, IP, DNS, TCP, UDP, NAT

• Socket Programming. • Iterative and concurrent servers.

Lab 5: Implementing a Web Server

Page 11: CS252: Systems Programming

Course Organization: Part 6: Miscellaneous topics

14. Introduction to SQL15. Source Control Systems (CVS, SVN) and

distributed (GIT, Mercurial)16. Introduction to Software Engineering17. Design Patterns18. Execution Profiling.

Lab 6: Writing a mobile application (team)

Page 12: CS252: Systems Programming

POINTERS IN C

Page 13: CS252: Systems Programming

Memory and Pointers

A pointer is a variable that contains an address in memory.In a 32 bit architectures, the size of a pointer is 4 bytes independent on the type of the pointer.

0

(4GB-1) 232-1

Address space

p:20: 12Char c = ‘A’; //ascii 65

char * p = &c; c:12: 65

Page 14: CS252: Systems Programming

Ways to get a pointer value (1)

1. Assign a numerical value into a pointerChar * p = (char *) 0x1800;*p = 5; // Store a 5 in location 0x1800;

What is likely to happen for the above code?

Note: Assigning a numerical value to a pointer isn't recommended and only left to programmers of OS, kernels, or device drivers

Page 15: CS252: Systems Programming

What is Likely to Happen Using Absolute Value Pointers

When programmer doesn’t what he is doing• Immediate core dump.• Mysterious program crash/misbehaving in the

future

When programmer knows• Program function as expected, as this is part of

OS kernel or device driver• This is part of exploit code for breaking into a

computer

Page 16: CS252: Systems Programming

Ways to get a pointer value (2)

2. Get memory address from another variable:

int *p;int buff[ 30];p = &buff[1];*p =78; buff[0]:100:

buff[1]:104:

buff[29]:216:220:

P: 96: 104

78

Page 17: CS252: Systems Programming

Ways to get a pointer value (3)

3. Allocate memory from the heap int *p p = new int; int *q; q = (int*)malloc(sizeof(int));

Page 18: CS252: Systems Programming

Using pointers

You can pass a pointer as a parameter to a function if you want the function to modify the content of the parameters

void swap (int *a, int *b){int temp; temp=*a;*a=*b;*b=temp;

}In main: swap(&x, &y)

Page 19: CS252: Systems Programming

Understanding Call by Value and Call by Reference

• C Tutorial – Call by Value or Call by Reference• http://www.codingunit.com/c-tutorial-call-by-

value-or-call-by-reference• Java With Us: Call by Value and Call by

Reference• http://www.javawithus.com/tutorial/call-by-

value-and-call-by-reference

Page 20: CS252: Systems Programming

Common Problems with Pointers

When using pointers, make sure the pointer is pointing to valid memory before assigning or getting any value from the locationMany string utility functions do not allocate memory for you: char *s; strcpy(s, "hello"); --> SEGV(uninitialized pointer)The only string function that allocates memory is strdup (it calls malloc with the length of the string and copies it)

Page 21: CS252: Systems Programming

Printing Pointers

It is useful to print pointers for debuggingchar*i; char buff[10]; printf("ptr=%d\n", &buff[5])

Or In hexadecimal printf("ptr=0x%x\n", &buff[5])Instead of using printf, it is better to use fprintf(stderr, …) since stderr is unbuffered and it is guaranteed to be printed on the screen.

Page 22: CS252: Systems Programming

sizeof() operator in Pointers

The size of a pointer is always 4 bytes in a 32 bit architecture independent of the type of the pointer:

sizeof(int)==4 bytessizeof(char)==1 bytesizeof(int*)==4 bytessizeof(char*)==4 bytes

Page 23: CS252: Systems Programming

Using Pointers to Optimize Execution

The following function adds the sum of integers in an array using array indexing.int sum(int * array, int n){int s=0;for(int i=0; i<n; i++) {

s+=array[i]; // Equivalent to //*(int*)((char*)array+i*sizeof(int))}return s;

}

Page 24: CS252: Systems Programming

Using Pointers to Optimize Execution

Now the equivalent code using pointersint sum(int* array, int n) {int s=0; int *p=&array[0];int *pend=&array[n];while (p < pend) {

s+=*p; p++;

}return s;

}

Page 25: CS252: Systems Programming

Using Pointers to Optimize Execution

When you increment a pointer to integer it will be incremented by 4 units because sizeof(int)==4.Using pointers is more efficient because no indexing is required and indexing require multiplication.Note: An optimizer may substitute the multiplication by a “<<“ operator if the size is a power of two. However, the array entries may not be a power of 2 and integer multiplication may be needed.

Page 26: CS252: Systems Programming

Array Operator Equivalence

We have the following equivalences:int a[20];a[i] - is equivalent to *(a+i) - is equivalent to *(&a[0]+i) – is equivalent to*((int*)((char*)&a[0]+i*sizeof(int)))

You may substitute array indexing a[i] by *((int*)((char*)&a[0]+i*sizeof(int))) and it will work!C was designed to be machine independent

Page 27: CS252: Systems Programming

2D Array. 1st Implementation

1st approachNormal 2D array.int a[4][3];

a[0][0]:100:a[0][1]:104:a[0][2]:108:a[1][0]:112:a[1][1]:116:a[1][2]:120:a[2][0]:124:a[2][1]:128:a[2][2]:132:a[3][0]:136:a[3][1]:140:a[3][2]:144:

a:

a[i][j] == *(int*)((char*)a + i*3*sizeof(int) + j*sizeof(int))

Page 28: CS252: Systems Programming

2D Array 2nd Implementation

2nd approachArray of pointers to rowsint* (a[4]); for(int i=0; i<4; i++){   a[i]=(int*)malloc(sizeof(int)*3);   assert(a[i]!=NULL);

}

Page 29: CS252: Systems Programming

2D Array 2nd Implementation

2nd approachArray of pointers to rows (cont)

a[0]:100:a[1]:104:a[2]:108:a[3]:112:

a[1][0]

a[0][0]

a[3][1]

a[2][0]

a[3][0]

a[2][1]

a[0][1]

a[1][1]

a[3][2]

a[2][2]

a[0][2]

a[1][2]

int*(a[4]);

a[3][2]=5

a:

Page 30: CS252: Systems Programming

2D Array 3rd Implementation

3rd approach. a is a pointer to an array of pointers to rows.

int **a;a=(int**)malloc(4*sizeof(int*));assert( a!= NULL)for(int i=0; i<4; i++){a[i]=(int*)malloc(3*sizeof(int));assert(a[i] != NULL)

}

Page 31: CS252: Systems Programming

2D Array 3rd Implementation

a is a pointer to an array of pointers to rows. (cont.)

a[0]:900:a[1]:904:a[2]:908:a[3]:912:

a[1][0]

a[0][0]

a[3][1]

a[2][0]

a[3][0]

a[2][1]

a[0][1]

a[1][1]

a[3][2]

a[2][2]

a[0][2]

a[1][2]

int **a; a[3][2]=5a:100: 900

Page 32: CS252: Systems Programming

Advantages of Pointer Based Arrays

You don’t need to know in advance the size of the array (dynamic memory allocation)You can define an array with different row sizesYou do not need to know how many rows there are when writing the program

Page 33: CS252: Systems Programming

Advantages of Pointer Based Arrays

Example: Triangular matrix

a[0]:900:a[1]:904:a[2]:908:a[3]:912:

a[1][0]

a[0][0]

a[2][0]

a[3][0]

a[2][1]

a[0][1]

a[1][1]

a[0][2]

a[1][2]

a[0][3]

int **a;a:100: 900:

Page 34: CS252: Systems Programming

Pointers to Functions

Pointers to functions are often used to implement Polymorphism in “C”.Polymorphism: Being able to use the same function with arguments of different types. Example of function pointer:typedef void (*FuncPtr)(int a);FuncPtr is a type of a pointer to a function that takes an “int” as an argument and returns “void”.

Page 35: CS252: Systems Programming

An Array Mappertypedef void (*FuncPtr)(int a);

void intArrayMapper( int *array, int n, FuncPtr func ) { for( int = 0; i < n; i++ ) {

(*func)( array[ i ] ); }

}int s = 0;void sumInt( int val ){

s += val;}void printInt( int val ) {

printf("val = %d \n", val);}

Page 36: CS252: Systems Programming

Using the Array Mapper

int a[ ] = {3,4,7,8};main( ){ // Print the values in the array

intArrayMapper(a, sizeof(a)/sizeof(int), printInt);

// Print the sum of the elements in the array s = 0; intArrayMapper(a, sizeof(a)/sizeof(int), sumInt); printf(“total=%d\”, s);}

Page 37: CS252: Systems Programming

A More Generic Mapper

typedef void (*GenFuncPtr)(void * a);void genericArrayMapper( void *array, int n, int entrySize, GenFuncPtr fun ){for( int i = 0; i < n; i++; ){

void *entry = (void*)( (char*)array + i*entrySize );

   (*fun)(entry);}

}

Page 38: CS252: Systems Programming

Using the Generic Mapper

void sumIntGen( void *pVal ){//pVal is pointing to an int

//Get the int val int *pInt = (int*)pVal; s += *pInt;}

void printIntGen( void *pVal ){int *pInt = (int*)pVal;printf("Val = %d \n", *pInt);

}

Page 39: CS252: Systems Programming

Using the Generic Mapperint a[ ] = {3,4,7,8}; main( ) {// Print integer values

s = 0; genericArrayMapper( a, sizeof(a)/sizeof(int),

sizeof(int), printIntGen);

// Compute sum the integer valuesgenericArrayMapper( a, sizeof(a)/sizeof(int),

sizeof(int), sumIntGen);printf(“s=%d\n”, s);

}

Page 40: CS252: Systems Programming

Swapping two Memory RangesIn the lab1 you will implement a sort function that will sort any kind of array. Use the array mapper as model.When swapping two entries of the array, you will have pointers to the elements (void *a, *b) and the size of the entry entrySize.

void * tmp = (void *) malloc(entrySize); assert(tmp != NULL); memcpy(tmp, a, entrySize); memcpy(a,b , entrySize); memcpy(b,tmp , entrySize);

Note: You may allocate memory only once for tmp in the sort method and use it for all the sorting to save muliple calls to malloc. Free tmp at the end.

Page 41: CS252: Systems Programming

String Comparison in Sort Function

In lab1, in your sort function, when sorting strings, you will be sorting an array of pointers, that is, of "char* entries. The comparison function will be receiving a “pointer to char*” or a” char**” as argument.

int StrComFun( void *pa, void *pb) {char** stra = (char**)pa;char ** strb = (char**)pb;return strcmp( *stra, *strb);

}