1 / 18

CS 416- Fall 2008 Session 02

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

lorin
Download Presentation

CS 416- Fall 2008 Session 02

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. What will be in recitations • Summary of lectures • Projects • Sample Questions / Home work • More technical stuff

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

  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…

  6. C Programming • Links: • http://paul.rutgers.edu/~tphan/cs416/2_IntroC.pdf • http://www.cs.cf.ac.uk/Dave/C/

  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

  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.

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

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

  12. What Happens When There Is More Than One Running Process? OS Code Globals P0 Stack Heap P1 P2

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

  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 parameters a Return value bookeeping Return Address Dynamic link w Local variables x y

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

  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 + i a[i] ~ *(a+i)

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

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

More Related