Download presentation
Presentation is loading. Please wait.
Published byRuby Richards Modified over 8 years ago
1
1 CS 416- Fall 2008 Session 02 TA: Tuan Phan Email: tphan@cs.rutgers.edutphan@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/http://paul.rutgers.edu/~tphan/cs416/ Slides for recitation, Useful Links
2
2 What will be in recitations Summary of lectures Projects Sample Questions / Home work More technical stuff
3
3 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 …
4
4 CEREAL cluster / iLab Link: http://ilab.rutgers.edu/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
5
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… 5
6
C Programming Links: –http://paul.rutgers.edu/~tphan/cs416/2_IntroC.pdfhttp://paul.rutgers.edu/~tphan/cs416/2_IntroC.pdf –http://www.cs.cf.ac.uk/Dave/C/http://www.cs.cf.ac.uk/Dave/C/ 6
7
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
8
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 8
9
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. 9
10
A Simple C Program #include #define STOP 0 /* Function: main */ /* Description: counts down from user input to STOP */ main() { /* variable declarations */ int counter; /* an integer to hold count values */ int startPoint; /* 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); } 10
11
Fred Kuhns (2/18/2016)CSE332– Object Oriented Programming Lab 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
12
What Happens When There Is More Than One Running Process? OS Code Globals Stack Heap P0 P1 P2
13
Run-Time Stack main() foo() Memory Before callDuring callAfter call Notes: Each box represents an activation record of a function.
14
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 a Return value Return Address Dynamic link w x y parameter s Local variables bookeepin g
15
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); } 15
16
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 + ia[i] ~ *(a+i) 16
17
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); } 17
18
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); 18
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.