20
Follow the River and You Will Find the C Jae Woo Lee, Michael Kester and Henning Schulzrinne Columbia University SIGCSE 2011 C Java A systems programming course with a narraKve

Follow the River and You Will Find the Cjae/slides/3157-SIGCSE-2011.pdfLab2: Pointers and Arrays • The most important and difficult milestone! – Students need plenty of ... –

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Follow the River and You Will Find the Cjae/slides/3157-SIGCSE-2011.pdfLab2: Pointers and Arrays • The most important and difficult milestone! – Students need plenty of ... –

FollowtheRiverandYouWillFindtheC

JaeWooLee,MichaelKesterandHenningSchulzrinneColumbiaUniversity

SIGCSE2011

C

Java

AsystemsprogrammingcoursewithanarraKve

Page 2: Follow the River and You Will Find the Cjae/slides/3157-SIGCSE-2011.pdfLab2: Pointers and Arrays • The most important and difficult milestone! – Students need plenty of ... –

Objects‐first

•  Objects‐firstv.IteraKve‐firstv.FuncKonal‐first– Currenttrendisobject‐firstwithJavaorPython

•  Everyonehasanopinion–Ihaveonetoo!

Butnottoday.Ourcourseaddressesaconsequenceofchoosingobjects‐first.

Page 3: Follow the River and You Will Find the Cjae/slides/3157-SIGCSE-2011.pdfLab2: Pointers and Arrays • The most important and difficult milestone! – Students need plenty of ... –

TheGapproblem

CS1,CS1.5,CS2•  Java

•  Toyprograms•  Eclipse

•  NotePad

•  …

OS•  C

•  Linuxkernel•  make,svn,gdb

•  vi,emacs

•  …

TypicalhodgepodgetransiKoncoursesoffereither:

1.  Tooli_le–studentsareunderprepared2.  Toomuch–studentsrunaway

Page 4: Follow the River and You Will Find the Cjae/slides/3157-SIGCSE-2011.pdfLab2: Pointers and Arrays • The most important and difficult milestone! – Students need plenty of ... –

DesigninganeffecKvetransiKon

•  One‐semestercoursethatcovers:– ThewholeC– SomeessenKalC++– AlotofUNIXandnetworking

•  Withfourgoals:1.  Don’tforgodepth2.  Focusondoingitright3.  Layoutthebigpicture4.  Don’tbeboring

Page 5: Follow the River and You Will Find the Cjae/slides/3157-SIGCSE-2011.pdfLab2: Pointers and Arrays • The most important and difficult milestone! – Students need plenty of ... –

How?

•  Thebigproject:webserverfromscratch–  Seeminglyindependentlabsasmilestones–  Eachcontributescodeorconcept

•  Rigidstructure–  Eachlabbuildsonpreviousones

•  ProvidesoluKonadereachdeadline–  Super‐detailedinstrucKons

•  NotmuchroomforcreaKvity

•  MoKvaKngstudents–  Youwillwritearealwebserverfromscratch!–  Youwillgofromaprogrammingstudenttoaprogrammer

Page 6: Follow the River and You Will Find the Cjae/slides/3157-SIGCSE-2011.pdfLab2: Pointers and Arrays • The most important and difficult milestone! – Students need plenty of ... –

Thecourse,adrama

Exposi'on• Lab1:Tools• Lab2:Pointers• Lab3:LinkedList

Risingac'on• Lab4:I/O• Lab5:UNIX• Lab6:Sockets

Climax• Lab7:Webserver

Fallingac'on• Lab8:Apachemodule• MulK‐Kerarchitecture

Resolu'onC++essenKals• Lab9:ObjectlifeKme• Lab10:LinkedListII• SmartPtr

Page 7: Follow the River and You Will Find the Cjae/slides/3157-SIGCSE-2011.pdfLab2: Pointers and Arrays • The most important and difficult milestone! – Students need plenty of ... –

Lab1:Shellbasics,SVN,Make

•  LearnessenKalUNIXcommandlinetools•  LearnhowtocompileandlinkmulKplesourcefiles

•  LearnhowtouseSVNandMake

Page 8: Follow the River and You Will Find the Cjae/slides/3157-SIGCSE-2011.pdfLab2: Pointers and Arrays • The most important and difficult milestone! – Students need plenty of ... –

Lab2:PointersandArrays

•  Themostimportantanddifficultmilestone!–  StudentsneedplentyofKmeandhelp

•  Givehardproblem:$ ./twecho one two three one ONE two TWO three THREE

•  Requirebug‐freecode–  UseValgrind–  Focusondoingitright

int main(int argc, char **argv) { if (argc <= 1) return 1;

char **copy = duplicateArgs(argc, argv);

char **p = copy;

argv++; p++; while (*argv) { printf("%s %s\n", *argv++, *p++); }

freeDuplicatedArgs(copy);

return 0; }

Page 9: Follow the River and You Will Find the Cjae/slides/3157-SIGCSE-2011.pdfLab2: Pointers and Arrays • The most important and difficult milestone! – Students need plenty of ... –

Lab3:LinkedList

•  Rigidstructure–headerfilegiven

•  Comprehensivetestdriveralsogiven–  Again,bug‐freecodeusingValgrind

•  PointersemanKcsandtypeunsafe–  WillberevisitedinLab10

struct Node { struct Node *next; void *data; }; struct List { struct Node *head; }; struct Node *addFront(struct List *lst, void *data); struct Node *findNode(struct List *lst, const void *dataSought, int (*compar)(const void *, const void *));

Page 10: Follow the River and You Will Find the Cjae/slides/3157-SIGCSE-2011.pdfLab2: Pointers and Arrays • The most important and difficult milestone! – Students need plenty of ... –

Lab4:StandardI/O

•  Mdb:flat‐filedatabaseofnameandmessages

•  ImplementMdbLookup–  Readsshareddatabasefileintolinkedlistonstart‐up– Uselab3’slinkedlistasalibrary–  Promptsforsearchstringandprintsmatchingrecords– MdbAddbinaryisprovidedfortesKng

struct MdbRec { char name[16]; char msg[24]; };

Page 11: Follow the River and You Will Find the Cjae/slides/3157-SIGCSE-2011.pdfLab2: Pointers and Arrays • The most important and difficult milestone! – Students need plenty of ... –

Lab5:TurningMdbLookupintoaserverwithoutsocketprogramming

•  EndofC;lectureshidstoUNIXandnetworking–  BriefoverviewofOSandTCP/IP–imparttheconceptoflayers–  ProcessmanagementinUNIX–forkandexec

•  TurnMdbLookupintoaserverusingNetcat

–  Theserver‐sidepipelineisgiven;studentsputitinashellscriptandwriteaCprogramtoforkandexecthescript

mkfifo mypipe cat mypipe | \ nc -l -p 40000 | \ mdb-lookup > mypipe

nc remote-host 40000 clientserver

Page 12: Follow the River and You Will Find the Cjae/slides/3157-SIGCSE-2011.pdfLab2: Pointers and Arrays • The most important and difficult milestone! – Students need plenty of ... –

Lab6:SocketsandHTTP

•  GothroughsampleTCPclientandservercode–  TCPEchoClient.c/TCPEchoServer.c

•  Lab6,part1:MdbLookupServer–  TCPEchoServer.c+MdbLookup.c(fromlab4)–  Fewerthan20linesofmodificaKon

•  ExplainHTTPprotocol–  ShowtheprotocolinacKonusingNetcat

•  Netcatclientposingasabrowser•  Netcatserverposingasawebserver

•  Lab6,part2:implementwgetlite– DownloadsasinglefileusingHTTP

Page 13: Follow the River and You Will Find the Cjae/slides/3157-SIGCSE-2011.pdfLab2: Pointers and Arrays • The most important and difficult milestone! – Students need plenty of ... –

Lab7:Webserverfromscratch!

•  Atthispoint,studentshavealltheyneedtoimplementasubsetofHTTP1.0:–  OnlyGETrequests–  Doesnotsendcontent‐typeheader

•  Part1:servestaKcHTMLpagewithimages•  Part2:servedynamicpagegeneratedbyMdbLookup

Browser h_p‐server MdbLookupServer

/mdb‐lookup?key=string string

searchresultssearchresultsforma>edinHTMLtable

“OMG,thisthingshowsupinmyFireFox!”

Page 14: Follow the River and You Will Find the Cjae/slides/3157-SIGCSE-2011.pdfLab2: Pointers and Arrays • The most important and difficult milestone! – Students need plenty of ... –

Lab8:Apachemodule

•  Rewritelab7asanApachemodule– Download,buildandconfigureApachewebserver– WriteaCmoduletoconnecttoMdbLookupServer

•  Oneoftheeasiestlabs!

Page 15: Follow the River and You Will Find the Cjae/slides/3157-SIGCSE-2011.pdfLab2: Pointers and Arrays • The most important and difficult milestone! – Students need plenty of ... –

SodwareArchitecture:TheBigPicture

•  RetracetheevoluKonofMdbLookup–  Lab4:commandline,accesslocaldatabase–  Lab5:server,puttogetherwithNetcatandpipes–  Lab6:server,codedusingthesocketsAPI–  Lab7:web‐basedserver,wri_enfromscratch

–  Lab8:web‐basedserver,wri_enasApachemodule

•  NowstudentsunderstandmulK‐Kerclient‐serverarchitecture– UnderlyingarchitectureforLAMP,J2EE,etc.

Page 16: Follow the River and You Will Find the Cjae/slides/3157-SIGCSE-2011.pdfLab2: Pointers and Arrays • The most important and difficult milestone! – Students need plenty of ... –

3weeksled–let’slearnC++

•  FocusonobjectlifeKmeandmemoryusage– Naturalextensiontoourfocussofar– OdenpoorlyunderstoodbymanywhouseC++

•  Coverage– ObjectconstrucKonanddestrucKon– TemplatesandSTLcontainers

Page 17: Follow the River and You Will Find the Cjae/slides/3157-SIGCSE-2011.pdfLab2: Pointers and Arrays • The most important and difficult milestone! – Students need plenty of ... –

Lab9:ObjectConstrucKonandDestrucKoninC++

•  DetailedstudyofMyStringclassimplementaKon

•  TracetheBasic4–  Insertprinvinconstructor,destructor,copyandop=()

–  Analyzetheoutputgeneratedbyadd()funcKon

–  Needtocompilewith”-fno-elide-constructors”

MyString add(MyString s1, MyString s2) { MyString temp(" and "); return s1 + temp + s2; }

class MyString { public: // member functions ... // overloaded ops ... private: char *data; int len; };

Page 18: Follow the River and You Will Find the Cjae/slides/3157-SIGCSE-2011.pdfLab2: Pointers and Arrays • The most important and difficult milestone! – Students need plenty of ... –

Lab10:Workingwithlegacycode–LinkedListRevisited

•  Part1:Newfacetothelegacycode–  ImplementStrList,linkedlistofMyString,usinglab3linkedlistasunderlyingengine

–  Thisishard!•  NeedtoswitchfrompointersemanKcstovaluesemanKcs•  Comprehensivetestdriverprovided

•  Part2:Nowupgradetheengine–  TurnStrListintoatemplateclassTList

•  Fortheengine,switchfromlab3linkedlisttoSTLlist–  Part1testdriveworkswithoutmodificaKonwithtypedefs

void StrList::addFront(const MyString& str) calls: struct Node *addFront(struct List *list, void *data)

typedef string MyString; typedef TList<string> StrList;

Page 19: Follow the River and You Will Find the Cjae/slides/3157-SIGCSE-2011.pdfLab2: Pointers and Arrays • The most important and difficult milestone! – Students need plenty of ... –

Comefullcircle–Java‐styleobjectreferenceinC++

•  “ImissJava…”1.  NiceJavacode

Foo b = a.createFoo(); b.doSomething(); return;

2.  SameexactcodeinC++(orisit?) Foo b = a.createFoo(); b.doSomething(); return;

3.  Wecandothis,but… Foo *b = a.createFoo(); b->doSomething(); return;

4.  Nowthiscomepre_ydarnclose SmartPtr<Foo> b = a.createFoo(); b->doSomething(); return;

•  SmartPtr–  Reference‐counted,socanbefreelycopied–  IniKalizedwithpointertoheap‐allocatedobject–  Overloadsoperator‐>()andoperator*()

Page 20: Follow the River and You Will Find the Cjae/slides/3157-SIGCSE-2011.pdfLab2: Pointers and Arrays • The most important and difficult milestone! – Students need plenty of ... –

Conclusion

•  Studentslovedthecourse– GreatevaluaKonsandreviews

•  Theyliked:–  Singletracknatureofthecourse–  Rigidstructure

•  DetailedlabinstrucKons•  ImmediateverificaKonofcorrectness

–  Classmailinglist

•  Willsharecoursematerialswithotherinstructors