1 CS 416- Fall 2008 Session 02 TA: Tuan Phan 732-445-6450 (ext 9644) : Just leaving msg( prefer...

18
1 CS 416- Fall 2008 Session 02 TA: Tuan Phan Email: [email protected] 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/

description

3 Recommend Tools for CS416 PuTTy : ssh client without GUI XManager: ssh client with GUI WinSCP: to upload files to CEREAL clusters IDE: emacs, vi …

Transcript of 1 CS 416- Fall 2008 Session 02 TA: Tuan Phan 732-445-6450 (ext 9644) : Just leaving msg( prefer...

Page 1: 1 CS 416- Fall 2008 Session 02 TA: Tuan Phan   732-445-6450 (ext 9644) : Just leaving msg( prefer using  ,

1

CS 416- Fall 2008Session 02

TA: Tuan PhanEmail: [email protected]

732-445-6450 (ext 9644) : Just leaving msg( prefer using Email, with HEADER:

[CS416] …)

Recitation: TH :3:35pm – 4:30 PM @ SEC 202Office 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

Page 2: 1 CS 416- Fall 2008 Session 02 TA: Tuan Phan   732-445-6450 (ext 9644) : Just leaving msg( prefer using  ,

2

What will be in recitations

• Summary of lectures• Projects• Sample Questions / Home work• More technical stuff

Page 3: 1 CS 416- Fall 2008 Session 02 TA: Tuan Phan   732-445-6450 (ext 9644) : Just leaving msg( prefer using  ,

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 …

Page 4: 1 CS 416- Fall 2008 Session 02 TA: Tuan Phan   732-445-6450 (ext 9644) : Just leaving msg( prefer using  ,

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

Page 5: 1 CS 416- Fall 2008 Session 02 TA: Tuan Phan   732-445-6450 (ext 9644) : Just leaving msg( prefer using  ,

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

Page 6: 1 CS 416- Fall 2008 Session 02 TA: Tuan Phan   732-445-6450 (ext 9644) : Just leaving msg( prefer using  ,

C Programming

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

6

Page 7: 1 CS 416- Fall 2008 Session 02 TA: Tuan Phan   732-445-6450 (ext 9644) : Just leaving msg( prefer using  ,

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

Page 8: 1 CS 416- Fall 2008 Session 02 TA: Tuan Phan   732-445-6450 (ext 9644) : Just leaving msg( prefer using  ,

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

Page 9: 1 CS 416- Fall 2008 Session 02 TA: Tuan Phan   732-445-6450 (ext 9644) : Just leaving msg( prefer using  ,

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

Page 10: 1 CS 416- Fall 2008 Session 02 TA: Tuan Phan   732-445-6450 (ext 9644) : Just leaving msg( prefer using  ,

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 */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

Page 11: 1 CS 416- Fall 2008 Session 02 TA: Tuan Phan   732-445-6450 (ext 9644) : Just leaving msg( prefer using  ,

Fred Kuhns (05/04/23) CSE332– Object Oriented Programming Lab

Another Simple C Programint 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

Page 12: 1 CS 416- Fall 2008 Session 02 TA: Tuan Phan   732-445-6450 (ext 9644) : Just leaving msg( prefer using  ,

What Happens When There Is More Than One Running Process?

OS

CodeGlobals

Stack

Heap

P0

P1

P2

Page 13: 1 CS 416- Fall 2008 Session 02 TA: Tuan Phan   732-445-6450 (ext 9644) : Just leaving msg( prefer using  ,

Run-Time Stack

main() main() main()

foo()

Memory Memory Memory

Before call During call After call

Notes: Each box represents an activation record of a function.

Page 14: 1 CS 416- Fall 2008 Session 02 TA: Tuan Phan   732-445-6450 (ext 9644) : Just leaving msg( prefer using  ,

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 AddressDynamic link

w

x

y

parameters

Local variables

bookeeping

Page 15: 1 CS 416- Fall 2008 Session 02 TA: Tuan Phan   732-445-6450 (ext 9644) : Just leaving msg( prefer using  ,

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

Page 16: 1 CS 416- Fall 2008 Session 02 TA: Tuan Phan   732-445-6450 (ext 9644) : Just leaving msg( prefer using  ,

Pointers in Cint 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)

16

Page 17: 1 CS 416- Fall 2008 Session 02 TA: Tuan Phan   732-445-6450 (ext 9644) : Just leaving msg( prefer using  ,

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

Page 18: 1 CS 416- Fall 2008 Session 02 TA: Tuan Phan   732-445-6450 (ext 9644) : Just leaving msg( prefer using  ,

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