Lecture 9 InterProcess Communication IPC amp Remote Procedure Calls RPC Sept 26 2019 Instructor David Culler httpscs162eecsberkeleyedu Recall Lock Solution 3 rd cut mutex ID: 1001135
Download Presentation The PPT/PDF document "CS 162: Operating Systems and Systems Pr..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
1. CS 162: Operating Systems and Systems ProgrammingLecture 9: Inter-Process Communication (IPC) & Remote Procedure Calls (RPC)Sept 26, 2019Instructor: David Cullerhttps://cs162.eecs.berkeley.edu
2. Recall: Lock Solution – 3rd cutmutex buf_lock = <initially unlocked>CondVar buf_signal = <initially nobody>Producer(item) { lock buffer while (buffer full) {cond_wait(buf_signal, buf_lock) }; Enqueue(item); unlock buffer broadcast(buf_signal)}Consumer() { lock buffer while (buffer empty) {cond_wait(buf_signal, buf_lock) }; item = queue(); unlock buffer broadcast(buf_signal) return item}Release lock; signal others to run; reacquire on resumen.b. OS must do the reacquireWhy User must recheck?9/26/19cs162 Fa19 L92
3. MonitorsLock: protects access to shared dataAlways acquire lock when accessingQueue of threads waiting to enter the monitor9/26/19cs162 Fa19 L93
4. Monitors in practiceLocks for mutual exclusionCondition variables for waitingA monitor is a lock and zero or more condition variables with some associated data and operationsJava provides this nativelyPOSIX threads: Provides locks and condvars, have to build your own9/26/19cs162 Fa19 L94
5. MonitorsCondition Variables: queue of threads waiting for something to become true inside critical sect.Atomically release lock and start waitingAnother thread using the monitor will signal themThe condition: Some function of monitor's data9/26/19cs162 Fa19 L95
6. Why the while Loop?Can we "hand off" the lock directly to the signaled thread so no other thread "sneaks in?"Yes. Called Hoare-Style MonitorsMany textbooks describe this schemeMost OSs implement Mesa-Style MonitorsAllows other threads to sneak inMuch easier to implementEven easier if you allow "spurious wakeups"wait() can return when no signal occurred, in rare casesPOSIX allows spurious wakeups9/26/19cs162 Fa19 L96
7. Interlude: Concurrency Is HardEven for practicing engineers trying to write mission-critical, bulletproof code!Therac-25: Radiation Therapy Machine with Unintended Overdoses (reading on course site)Mars Pathfinder Priority Inversion (JPL Account)Toyota Uncontrolled Acceleration (CMU Talk)256.6K Lines of C Code, ~9-11K global variablesInconsistent mutual exclusion on reads/writes9/26/19cs162 Fa19 L97
8. Comparing SynchronizationSemaphores can implement locksAcquire() { semaphore.P(); }Release() { semaphore.V(); }and Condition VariablesMonitors combine locks and CVs in a structured fashionModern view: concurrent objects (e.g., Java)Are there other important common patterns?9/26/19cs162 Fa19 L98
9. IPC/RPC BackgroundCollections of threads interact through shared objects and signals in a common address spaceMultiple threads in a user processMultiple threads forming the kernelProcesses are isolated from each other – distinct address spaces – so they interact through external meansFiles, Pipes, SocketsFunction as communication channelsNarrow interfaces, limited interactionsOn the same machine or notThese are forms of message passingCan be utilized between threads in a process tooGO channels, MPI, CSP, …THE paradigm for large parallel machines and clustersAND across any network (of course)9/26/19cs162 Fa19 L99
10. Recall: Communication between processesCan we view files as communication channels?We have seen one example – pipesRoutinely used with the shell>>> grep list src/*/*.c | morewrite(wfd, wbuf, wlen); n = read(rfd,rbuf,rmax); 9/26/19cs162 Fa19 L910
11. Recall: Communication Across the world looks like file IO Connected queues over the InternetBut what’s the analog of open?What is the namespace?How are they connected in time?write(wfd, wbuf, wlen); n = read(rfd,rbuf,rmax); 9/26/19cs162 Fa19 L911
12. Socket APIBase level Network programming interfaceSocketAPITCPUDPIPApplicationTransportNetwork 9/26/19cs162 Fa19 L912
13. Recall: Sockets w/ Protection & ParallelismClientServerCreate Client SocketConnect it to server (host:port)Create Server SocketBind it to an Address (host:port)Listen for ConnectionAccept syscall()Connection SocketConnection Socketwrite requestread responseClose Client Socketread requestwrite responseClose Connection SocketClose Server SocketChildClose Connection SocketClose Listen SocketParentAside: why does the server need to close the connection socket?9/26/19cs162 Fa19 L913
14. Recall: What Is A Protocol?A protocol is an agreement on how to communicateIncludesSyntax: how a communication is specified & structuredFormat, order messages are sent and receivedSemantics: what a communication meansActions taken when transmitting, receiving, or when a timer expiresDescribed formally by a state machineOften represented as a message transaction diagram9/26/19cs162 Fa19 L914
15. IPC Issue: RepresentationYou have mostly experienced writing and reading stringsi.e., sequential stream of characters (formerly bytes)What about an int ? struct ? array ? list ?An object in memory has a machine-specific binary representation.Threads in a common process address space are all in the same machine and have the same view of what’s in memory.Offsets into fields, follow pointersWhen a object is externalized, it must become a sequential sequence of bytesAnd it must be possible to read it back and create an equivalent object9/26/19cs162 Fa19 L915
16. Endian-ness For a byte-address machine, which end of a machine-recognized object (e.g., int) does its byte-address refer to?BigEndian: address is the most-significant bitsLittleEndian: address is the least-significant bits9/26/19cs162 Fa19 L916
17. What endian is the Internet?Big EndianNetwork byte ordervs “host byte order”9/26/19cs162 Fa19 L917
18. Abstracting away representation9/26/19cs162 Fa19 L918
19. Aside: write_words / read_wordsAll the issues of data representation arise in non-text files as well.9/26/19cs162 Fa19 L919
20. What about richer objects?Consider the list of word_count_t of HW0/1 …Each element contains:An intA pointer to a string (of some length)A pointer to the next elementfprintf_words writes these as a sequence of lines (character strings with \n) to a file streamWhat if you wanted to write the whole list as a binary object (and read it back as one)?how do you represent the string?Does it make any sense to write the pointer?9/26/19cs162 Fa19 L920
21. SerializationConverting data structures into a canonical linear format so that they can be stored/retrieved or transmitted/received.Values, structs, lists & trees are easygraphs are hardMany choices with different pros/consJSON & XML common in webSun External Data Representation (XDR) std since 80’sBuilt in to languages like Java, Lisp, …Google Protocol Buffers provide simple description languageUse a tool that fits your needs9/26/19cs162 Fa19 L921
22. Data Serialization FormatsJSON and XML are commonly used in web applicationsLots of ad hoc formats9/26/19cs162 Fa19 L922
23. Data Serialization Formats9/26/19cs162 Fa19 L923
24. Inter-Process Communication (IPC)Mechanism to create a communication channel between distinct processesUser to User, Kernel to User, Same machine or different ones, Different programming languages, …Serialization format understand by bothCan have authentication and authorization mechanism associated with itFailure in one process isolated from the otherBut may have to take exceptional measuresMany uses and interaction patternsLogging process, Window management, …Moving some system functions out of kernel to user space9/26/19cs162 Fa19 L924
25. IPC to simplify / extend OSWhat if the file system is not local to the machine, but on the network?Is there a general mechanism for providing services to other processes?AppAppfile systemWindowingNetworkingVMThreadsAppMonolithic StructureAppFileSys ???Window MgraddressspacesthreadsPartitioning ???AppIPC9/26/19cs162 Fa19 L925
26. Recall: Request/Response Protocolwrite(rqfd, rqbuf, buflen); n = read(rfd,rbuf,rmax); Client (issues requests)Server (performs operations)requestsresponseswrite(wfd, respbuf, len); n = read(resfd,resbuf,resmax); service requestwaitSerialized Objects9/26/19cs162 Fa19 L926
27. Domain Name System (DNS)Another proto-RPC distributed systemPurpose: Convert a human readable name (www.google.com) to an IP Address (169.229.15.7)Why?Humans don't want to remember IP addressesBut IP routes traffic based on IP addressesOther benefitsService can change hosts (IP Address) but keep nameFancy things like sending Google users to different hosts for load balancing9/26/19cs162 Fa19 L927
28. Example: DNSTop-levelcomeduMit.edu169.229.131.81128.32.61.103128.32.139.48berkeley.eduwwwcalmaileecsberkeleyMITeecs.berkeley.eduwwwDNS ServerHostname Resolution RequestHostname Resolution Response (IP)9/26/19cs162 Fa19 L928
29. DNSA hierarchical system for namingNames are divided into labels, right to left:www.eecs.berkeley.eduEach domain owned by a particular organizationTop level handled by ICANNSubsequent levels owned by organizationsResolution by repeated queriesName server for each domain: <root>, edu, berkeley.edu, eecs.berkeley.edu9/26/19cs162 Fa19 L929
30. DNS – Root ServerHow do we know where to start?Hardcoded list of root servers and backups (updated rarely)Or use your ISP's serverMakes repeated queries on your behalfCalled a recursive resolver9/26/19cs162 Fa19 L930
31. Don’t libraries provide Services?result_t libr_fun(arg_t arg1, … ) { “access the arguments”; “do something useful for caller” “return results”}main( … ) { “…”; res = libr_fun(p1, p2,…); “do something with results”}Stack call framep1p2Return Resultsres9/26/19cs162 Fa19 L931
32. And aren’t system calls doing this ?User program call library functionLibrary function formats args for syscallIssues syscall exceptionSyscall handler unpacks the args and calls (dispatches to) the subsystem function that handles the callSubsystem performs the operationsSyscall rtn handlers puts result in reg and resumes user threadLibrary function gets syscall result and returns to the user program9/26/19cs162 Fa19 L932
33. Remote Procedure Call (RPC)Idea: Make communication look like an ordinary function callWrapper library like for system callsCalled stubsAlso wrappers at the receiving endRead messages from socket, dispatch to actual functionLook like local function calls9/26/19cs162 Fa19 L933
34. RPC ConceptClient (caller) r = f(v1, v2);Server (callee)res_t f(a1, a2)callreturnreceivereturncallClientStubbundleargsbundleret valsunbundleret valssendreceivesendServerStubunbundleargs9/26/19cs162 Fa19 L934
35. RPC Information FlowClient (caller) r = f(v1, v2);Server (callee)res_t f(a1, a2)callreturnreceivereturncallClientStubbundleargsbundleret valsunbundleret valssendreceiveMachine AMachine BPacketHandlerPacketHandlerNetworkNetworkServerStubunbundleargssendServerStubunbundleargs9/26/19cs162 Fa19 L935
36. Six stepsThe client calls the client stub. The call is a local procedure call, with parameters pushed on to the stack in the normal way.The client stub packs the parameters into a message and makes a system call to send the message. Packing the parameters is called marshaling.The client's local operating system sends the message from the client machine to the server machine.The local operating system on the server machine passes the incoming packets to the server stub.The server stub unpacks the parameters from the message. Unpacking the parameters is called unmarshaling.Finally, the server stub calls the server procedure. The reply traces the same steps in the reverse direction9/26/19cs162 Fa19 L936
37. StubsClient and server use “stubs” to glue pieces togetherClient stub is responsible for “marshaling” arguments and “unmarshaling” the return valuesServer-side stub is responsible for “unmarshaling” arguments and “marshaling” the return valuesRegular function call (x86 calling convention etc…) RPC function “name” is “resolved” to remote handler function at serverDispatch similar to syscalltyp open(name,mode) {… open the file}#FILE *ropen( name, mode) { ‘send <#, name, mode>’ ‘rcv result’}9/26/19cs162 Fa19 L937
38. Stub GenerationWe need to “discover” available methods Interface definition language (IDL)Contains, among other things, types of arguments/returnSent from server to client for stub generationIDL “compiler” generates stub functions9/26/19cs162 Fa19 L938
39. MarshalingMarshaling involves (depending on system) converting values to a canonical form, serializing objects, copying arguments passed by reference, etc.Needs to account for cross-language and cross-platform issues Eg. Big endian vs Little endian Overhead.Many many different formats9/26/19cs162 Fa19 L939
40. RPC BindingHow does client know which machine to send RPC?Binding: the process of converting a user-visible name into a network endpointStatic: fixed at compile timeDynamic: performed at runtimeDynamic BindingMost RPC systems use dynamic binding via name serviceWhy dynamic binding?Access control: check who is permitted to access serviceFail-over: If server fails, use a different oneRegistry at server binds to RPC server stubs9/26/19cs162 Fa19 L940
41. Break9/26/19cs162 Fa19 L941
42. Do I need to implement RPC to use it?No! (Usually)Lot of existing RPC librariesJSON RPCXML RPCJava RMIApache ThriftRESTgRPC9/26/19cs162 Fa19 L942
43. Interface Definition LanguagePseudocodeprotocol myprotocol { 1: int32 mkdir(string name); 2: int32 rmdir(string name);}Marshalling Example: mkdir("/directory/name")returns 0Client Sends: \001/directory/name\0Server Sends: \0\0\0\09/26/19cs162 Fa19 L943
44. Our Toy Examplechar *l_mkdir(char *s) { send(svr, #1, s) res = rcv(srv) return res}Local Process#include <sys/stat.h>RPC_server() { while (req = rcv) { p = funcode(req) args = getargs(rq) res = RPC_Funs[p](args) reply(req, res) }Char *r_rmdir(char * s) {char *r_mkdir(char *s) { stat = mkdir(s, mode); return stat}int main( . . .) { st = l_mkdir(“cs162”);}Remote Server Process9/26/19cs162 Fa19 L944
45. Interface Definition LanguageCompiled by a tool (e.g., gRPC) to generate stubs in various source languages (C, C++, Java, Python, …)Any client/server stubs can interact if they both adhere to the same protocolMust be able to unmarshall what the other side marshalledImplementation language doesn't matter if we send right bits "over the wire"And this is not specific to RPC9/26/19cs162 Fa19 L945
46. Network File System (NFS)Three Layers for NFS systemUNIX file-system interface: open, read, write, close calls + file descriptorsVFS layer: distinguishes local from remote filesCalls the NFS protocol procedures for remote requestsNFS service layer: bottom layer of the architectureImplements the NFS protocolNFS Protocol: RPC for file operations on serverReading/searching a directory manipulating links and directories accessing file attributes/reading and writing filesFile StorageFile ServerClientOS9/26/19cs162 Fa19 L946
47. RPC: Really like a function call?What if something fails?result = myprotocol_mkdir(ctx, "/directory/name");What should result be?Do we really know if the server made the directory on its side?Maybe error occurred with server's file system?Unrelated problem caused server to crash?If client doesn't hear back from server: did server crash or is it just taking a long time?9/26/19cs162 Fa19 L947
48. RPC: Really like a function call?What if we're doing remote file IO?remoteFile *rf = remoteio_open(ctx, "oski.txt");remoteio_puts(ctx, rf, "Bear\n");remoteio_close(ctx, rf);What if the client fails before it closes?Will the file be left open forever?Remember: local case is easy, OS cleans up after terminated processes9/26/19cs162 Fa19 L948
49. RPC: Really like a function call?PerformanceCost of Procedure call << same-machine RPC << network RPCMeans programmers must be aware they are using RPC (limits to transparency!) Caching can help, but may make failure handling even more complex9/26/19cs162 Fa19 L949
50. Welcome to Distributed SystemsThings get complicated when we have multiple machines in the picture!Each can fail independentlyEach has its own view of the worldServer: Client hasn't closed oski.txt, may still want to writeClient after crash: I need to open oski.txt againWe'll study these and many other problems later!9/26/19cs162 Fa19 L950
51. Interlude: HTTPApplication protocol for The WebRetrieve a specific object, upload data, etc.Runs on top of TCP (sockets)Like any protocol, stipulates:Syntax: Content sent over socket connectionSemantics: Meaning of a messageValid replies and actions taken upon message receiptArguably a primitive form of RPCParsing text, Hardcoded operationsNo registry of available functionsNo formal marshal/unmarshalREST calls get a lot closer …9/26/19cs162 Fa19 L951
52. HTTP "RPC"GET /search.html200 OK< HTML Content>9/26/19cs162 Fa19 L952
53. HTTP "RPC"POST /users/oksi/photos<image content>201 Created9/26/19cs162 Fa19 L953
54. HTTP MessagesText-based: We just send character strings over our TCP socket connectionTo make a request, browser might write something like the following on a socket:GET /hello.html HTTP/1.0\r\nHost: 128.32.4.8:8000\r\nAccept: text/html\r\nUser-Agent: Chrome/45.0.2454.93\r\nAccept-Language: en-US,en;q=0.8\r\n\r\n9/26/19cs162 Fa19 L954
55. HTTP MessagesText-based: We just send strings over our TCP socket connectionWe then read the following response from the web server:HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nContent-Length: 128\r\n\r\n<html>\n<body>\n <h1>Hello World</h1>\n <p>\n Hello, World!\n </p>\n</body>\n</html>\n9/26/19cs162 Fa19 L955
56. HTTP and StateRemember this situation?remoteFile *rf = remoteio_open(ctx, "oski.txt");remoteio_puts(ctx, rf, "Bear\n");remoteio_close(ctx, rf);Client fails: does file stay open forever?Server maintains state between requests from client9/26/19cs162 Fa19 L956
57. HTTP and StateHTTP avoids this issue – stateless protocolEach request is self-containedTreated independently of all other requestsEven previous requests from same client!So how do we get a session?Client stores a unique ID locally – a cookieClient adds this to each request so server can customize its response9/26/19cs162 Fa19 L957
58. REST calls over http?HTTP GET http://www.appdomain.com/dirs/mkdir?name=cs162&mode=tmp9/26/19cs162 Fa19 L958
59. RPC LocallyDoesn't need to be between different machinesCould just be different address spaces (processes)Gives location transparencyMove service implementation to wherever is convenientClient runs same old RPC codeMuch faster implementations available locally(Local) Inter-process communicationWe'll see several techniques later on9/26/19cs162 Fa19 L959
60. MicrokernelsSplit OS into separate processesExample: File System, Network Driver are external processesPass messages among these components (e.g., via RPC) instead of system callsAppAppfile systemWindowingNetworkingVMThreadsAppMonolithic StructureAppFilesyswindowsRPCaddressspacesthreadsMicrokernel Structure9/26/19cs162 Fa19 L960
61. MicrokernelsMicrokernel itself provides only essential servicesCommunicationAddress space managementThread schedulingAlmost-direct access to hardware devices (for driver processes)AppAppfile systemWindowingNetworkingVMThreadsAppMonolithic StructureAppFilesyswindowsRPCaddressspacesthreadsMicrokernel Structure9/26/19cs162 Fa19 L961
62. Why Microkernels?ProsFailure IsolationEasier to update/replace partsEasier to distribute – build one OS that encompasses multiple machinesConsMore communication overhead and context switchingHarder to implement?9/26/19cs162 Fa19 L962
63. Flashback: What is an OS?Always:Memory ManagementI/O ManagementCPU SchedulingCommunicationsMultitasking/multiprogrammingMaybe:File System?Multimedia Support?User Interface?Web Browser?Not provided in a strict microkernel9/26/19cs162 Fa19 L963
64. Influence of MicrokernelsMany operating systems provide some services externally, similar to a microkernelOS X and Linux: Windowing (graphics and UI)Some currently monolithic OSs started as microkernelsWindows family originally had microkernel designOS X: Hybrid of Mach microkernel and FreeBSD monolithic kernel9/26/19cs162 Fa19 L964
65. SummaryRemote Procedure Call: Invoke a procedure on a remote machineProvides same interface as normal function callAutomatic packing and unpacking of user argumentsMicrokernels: Run system services outside of kernelHTTP: Application Layer ProtocolLike RPC, but statelessDomain Name Service: Map names to IP addressesHierarchical organization among many servers9/26/19cs162 Fa19 L965