140 likes | 255 Views
Lab1. TA: Yi Cui 01/22/2013. Parallel graph search. Communication between processes Graph search technics BFS DFS bFS A* Multiple threads. Part 1. Goals Learn to use Windows API CreateFile ReadFile WriteFile … Learn to communicate between processes using a pipe Create a pipe
E N D
Lab1 TA: Yi Cui 01/22/2013
Parallel graph search • Communication between processes • Graph search technics • BFS • DFS • bFS • A* • Multiple threads
Part 1 • Goals • Learn to use Windows API • CreateFile • ReadFile • WriteFile • … • Learn to communicate between processes using a pipe • Create a pipe • Send/receive message through a pipe • Terminate your program clearly
Part 1 Your process CC process Start CC process (CreateProcess) Build a pipe to CC (CreateFile) Send parameters to CC (WriteFile) Read response from CC (ReadFile) Build a pipe to robot (CreateFile) Connect to robot (WriteFile) Read response from robot (ReadFile) Print out initial room id Generate graph, create robots, send response back Send robot response back, including initial room id
Part 1 • Two steps for clear termination • Cloese robots • Send DISCONNECT message to each robot (WriteFile) • Wait for robots to terminate (WaitForSingleObject) (optional) • CloeseHandle • Close CC • Send DISCONNECT message to CC (WriteFile) • Wait for CC to terminate (WaitForSingleObject) (must) • CloseHandle
Part 1 • Read pipe message • Difficulty: size is unknown in ahead of time • Correct way: • Try to read BUF_SIZE bytes • If (bytesRead == BUF_SIZE) // possibly more bytes • { • Use PeekNamedPipe to check how many bytes left, say x bytes • Re-allocate a new buffer with size BUF_SIZE + x • Copy data from old buffer to new buffer • Read rest x bytes • } • Why not do PeekNamedPipe first? Check x != 0 Delete old buffer
C Pointers • strcpy(char* str1, char* str2) • copy str2 to str1 • For example, copy “abcdefgh” to a string char* ptr; strcpy(ptr, “abcdefgh”); char* ptr= new char[5]; strcpy(ptr, “abcdefgh”); char* ptr = new char[8]; strcpy(ptr, “abcdefgh”); char* ptr = new char[9]; strcpy(ptr, “abcdefgh”); char* ptr; ptr = “abcdefgh”; ptr[1] = ‘x’; (crash, static array, read only)
C Pointers • strcat(char* str1, char* str2) • append str2 to the end of str1 • For example, append “def” to “abc” char* ptr = new char[6]; strcpy(ptr, “abc”); strcat(ptr, “def”); char* ptr = new char[7]; strcpy(ptr, “abc”); strcat(ptr, “def”);
C Pointers • memcpy(char* str1, char* str2, int n) • copy n bytes from str2 to str1 • For example char* ptr = new char[4]; memcpy(ptr, “abcd”, 4); intlen = strlen(ptr); Incorrect!
C Pointer • Practice • Dynamic buffer class MyBuf { public: char* buf; intdataSize; intbufSize; void ReSize(intnewSize); }; MyBuf *mb = new MyBuf; mb->buf = new char[5]; mb->dataSize = 0; mb->bufSize = 5; memcpy(mb->buf, “abc”, 3); mb->dataSize = 3; void MyBuf::ReSize(intnewSize) {}
C Pointers class NodeTuple64 { public: uint64 node; float intensity; }; sizeof(NodeTuple64) ?
C Pointers char* ptr = new char[20]; NodeTuple64* tuple = (NodeTuple64*)ptr; tuple->node = 1; tuple->intensity = 0.5; ptr++; tuple++; sizeof(NodeTuple64) vs. sizeof(tuple) ptr ptr 1 0.5 tuple tuple
Windows API • Practice • Open Notepad, type 10 characters “abcdefghij”, save it • Use CreateFile & ReadFile to read 10 bytes from that file and display it on screen