180 likes | 263 Views
CS 416- Fall 2008 Session 02. TA: Tuan Phan Email: tphan@cs.rutgers.edu 732-445-6450 (ext 9644) : Just leaving msg( prefer using Email, with HEADER: [CS416] …) Recitation: TH :3:35pm – 4:30 PM @ SEC 202 Office Hour: TH 5:00 – 6:00 PM @ Hill 367
E N D
CS 416- Fall 2008Session 02 TA: Tuan Phan Email: tphan@cs.rutgers.edu 732-445-6450 (ext 9644) : Just leaving msg( prefer using Email, with HEADER: [CS416] …) Recitation: TH :3:35pm – 4:30 PM @ SEC 202 Office Hour: TH 5:00 – 6:00 PM @ Hill 367 Another place to find me: PANIC LAB, CORE 340 Extra: Email to setup appointment on Monday afternoon. TA’s Web Site: http://paul.rutgers.edu/~tphan/cs416/ Slides for recitation, Useful Links
What will be in recitations • Summary of lectures • Projects • Sample Questions / Home work • More technical stuff
Recommend Tools for CS416 • PuTTy : ssh client without GUI http://www.chiark.greenend.org.uk/~sgtatham/putty/ • XManager: ssh client with GUI • WinSCP: to upload files to CEREAL clusters http://winscp.net/ • IDE: emacs, vi …
CEREAL cluster / iLab • Link: http://ilab.rutgers.edu/ • Use Linux machines; DO NOT use Sun machines (including cereal.rutgers.edu) • iLab: Hill 248 - 250 • Use Rutgers ID • Transfer students: use temporary ID • Problem: meet Robert Toth @ CoRE 232 Homework: Create/Activate an account on iLab
Today’s topics • Communication Channels with TA • Quick Introduction about C • Prepare for project 1 • General Information • Calling Stack • Pointers in C • Dynamic Memory Allocation • Fork() • Others…
C Programming • Links: • http://paul.rutgers.edu/~tphan/cs416/2_IntroC.pdf • http://www.cs.cf.ac.uk/Dave/C/
Example of Process Creation Using Fork • The UNIX shell is command-line interpreter whose basic purpose is for user to run applications on a UNIX system • cmd arg1 arg2 ... argn
Creating, Compiling and Running Your C Program • Create: myprog.c • Compile (1) cc myprog.c (2) cc -o myprog myprog.c • Running (1) ./a.out (2) ./myprog
Compiler Options -llibrary Link with object libraries. cc calc.c -o calc -lm -Ldirectory Add directory to the list of directories containing object-library routines. cc prog.c -L/home/myname/mylibs mylib.a -Ipathname Add pathname to the list of directories in which to search for #include files with relative filenames cc prog.c -I/home/myname/myheaders -g invoke debugging option.
A Simple C Program #include <stdio.h> #define STOP 0 /* Function: main */ /* Description: counts down from user input to STOP */ main() { /* variable declarations */ int counter; /* an integer to hold count values */ intstartPoint; /* starting point for countdown */ /* prompt user for input */ printf("Enter a positive number: "); scanf("%d", &startPoint); /* read into startPoint */ /* count down and print count */ for (counter=startPoint; counter >= STOP; counter--) printf("%d\n", counter); }
Another Simple C Program int main (int argc, char **argv) { int i; printf(“There are %d arguments\n”, argc); for (i = 0; i < argc; i++) printf(“Arg %d = %s\n”, i, argv[i]); return 0; } • Notice that the syntax is similar to Java • What’s new in the above simple program? • of course you will have to learn the new interfaces and utility functions defined by the C standard and UNIX • Pointers will give you the most trouble CSE332– Object Oriented Programming Lab
What Happens When There Is More Than One Running Process? OS Code Globals P0 Stack Heap P1 P2
Run-Time Stack Memory Memory Memory main() main() main() foo() Before call During call After call Notes: Each box represents an activation record of a function.
Activation Record • Activation Record: • information about each function • stored in run-time stack int foo(int a, int b) { int w, x, y; . . . return y; } b parameters a Return value bookeeping Return Address Dynamic link w Local variables x y
Passing pointers into a function (1) void Swap(int a, int b) { int tmp; tmp=a; a=b; b=tmp; printf(“Inside SWAP(): a=%d b=%d”, a, b); } main() { int x=2,y=3; Swap(x,y); printf(“x=%d y=%d”, x,y); }
Pointers in C int I; int *ptr; i=4; // Store the value 4 into the memory location associated with i. ptr= &i; // Store the address of I into the memory location associated with ptr *ptr = *ptr + 1; ----- int a[10], x; int *pa; pa = &a[0]; /* pa pointer to address of a[0] */ x = *pa; /* x = contents of pa (a[0] in this case) */ // &a[i] ~ a + i a[i] ~ *(a+i)
Passing pointers into a function (2) void Swap(int *a, int *b) { int tmp; tmp=*a; *a=*b; *b=tmp; printf(“Inside SWAP(): a=%d b=%d\n”, *a, *b); } main() { int x=2,y=3; Swap(&x,&y); printf(“x=%d y=%d”, x,y); }
Dynamic Memory Allocation • void *malloc(size_t number_of_bytes) Ex: char *cp; cp = malloc(100); int *ip; ip = (int *) malloc(100*sizeof(int)); • void *calloc(size_t num_elements, size_t element_size}; int *ip; ip = (int *) calloc(100, sizeof(int)); Malloc does not initialise memory (to zero) in any way. If you wish to initialise memory then use calloc. • free(pointer). • kmalloc() & kfree() • void * kmalloc (size_t size, int priority);void kfree (void * __ptr);