1 / 29

CS6223: Distributed Systems

CS6223: Distributed Systems. Remote Procedure Call (RPC). An Example of Local Procedure Call. /* save a string to file msg.dat */ savemsg (char *msg) { FILE *fp; fp = fopen(&quot;msg.dat&quot;, &quot;w+&quot;); fprintf(fp, &quot;%s<br>&quot;, msg); fclose (fp); } main() {

erik
Download Presentation

CS6223: Distributed Systems

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. CS6223: Distributed Systems Remote Procedure Call (RPC)

  2. An Example of Local Procedure Call /* save a string to file msg.dat */ savemsg(char *msg) { FILE *fp; fp = fopen("msg.dat", "w+"); fprintf(fp, "%s\n", msg); fclose (fp); } main() { char *str = "This is an example of LPC."; savemsg(str); }

  3. Moving the local procedure to a remote machine ………… savemsg_1(char **argp; struct svc_req *rqstp) { FILE *fp; fp = fopen("msg.dat", "w+"); fprintf(fp, "%s\n", *argp); fclose (fp); } server: main() { char *str = "This is an example of RPC."; char *remote_host = "sus1.cs.cityu.edu.hk"; CLIENT *clnt; clnt = clnt_create(remote_host, MSGPROG, MSGVER, "netpath"); if (clnt == (CLIENT *) NULL) { clnt_pcreateerror(host); exit(1); } savemsg_1(&str, clnt); } client:

  4. Remote Procedure Call 1984: Birrell & Nelson • Mechanism to call procedures on other machines • Process on machine A can call a procedure on machine B • A is suspended • Execution continues on B • When B returns, control passed back to A Goal: Make a remote procedure call looking the same as a local call to programmers.

  5. client server message passing server skeleton service routines client program client stub RPC implementation The RPC compiler auto-generates stub/skeleton routinesto make an RPC to the user as if it is a local call. What stub routines do: • marshalling / unmarshalling parameters and return values • handling different data formats between different machines • detecting and handling failures of client/server processes and the networks

  6. server executable client executable RPC compiler C compiler C compiler Compilation in SUN RPC service routines server skeleton (main) interface definition data conversion client stub client program (main)

  7. Demo of RPC Interface file msg.x: program MSGPROG { version MSGVER{ int SAVEMSG(string)= 1; string READMSG(int)= 2; } = 2; } = 345678; Generate stub routines by: rpcgen –a msg.x Compile program by: cc –o object xxx.c

  8. Steps in a RPC 1. Client calls stub (push parameters onto stack) client program (msg_client.c) service routines client stub (msg_clnt.c) server skeleton network routines network routines

  9. Steps in a RPC 2. Clnt_stub marshals parameters to message & makes an OS call (client blocked) client program service routines client stub (msg_clnt.c) server skeleton network routines network routines

  10. Steps in a RPC 3. Network message sent to server client program service routines server skeleton client stub network routines network routines

  11. Steps in a RPC 4. Deliver message to server stub & unblock server client program service routines server skeleton (msg_svr.c) client stub network routines network routines

  12. Steps in a RPC 5. Svr-stub unmarshals parameters & calls service routine (local call) service routines (msg_server.c) client program server skeleton (msg_svr.c) client stub network routines network routines

  13. Steps in a RPC 6. Return to the stub from service routine client program service routines server skeleton client stub network routines network routines

  14. Steps in a RPC 7. Svr_stub marshals return-value & requests OS to send out message client program service routines server skeleton client stub network routines network routines

  15. Steps in a RPC 8. Transfer message over network client program service routines server skeleton client stub network routines network routines

  16. Steps in a RPC 9. Deliver message to client (unblock client) client program service routines server skeleton client stub network routines network routines

  17. Steps in a RPC 10. Clnt_stub unmarshals return-value & returns to client program… client program service routines server skeleton client stub network routines network routines

  18. server executable client executable RPC compiler C compiler C compiler Compilation in SUN RPC service routines server skeleton (main) interface definition data conversion client stub client program (main)

  19. Writing the programs Programmers need to write two pieces of programs: • Client program • Specify server’s location • Parameters and return value of RPC are pointers • Service routines • Generally the same as local procedures, except the parameters and parameter types

  20. Interface definition (1) • Define machine/OS/language-independent data types and data structures • Specify interfaces: program # and version # • Define remote procedures: procedure #, parameters, return type constant and type definitions /*optional */ program IDENTIFIER{ version VERSION_ID{ procedure list } = value; … } = value; program MSGPROG { version MSGVER{ int SAVEMSG(string)= 1; string READMSG(int)= 2; } = 3; } = 345678;

  21. Interface definition (2) • one program block in an interface definition • one or more sets of versionsin the program block • one or more sets of proceduresin each version block • one argument in each procedure by default • If more than one parameter is required, use a struct to group them • “–N” option in “rpcgen” allows multiple arguments

  22. Incompatible data representation (1) • Different byte ordering • Big endian: Most significant byte in low memory (e.g. Sun Sparcs) • Little endian: Most significant byte in high memory (e.g. Pentiums) The problem was solved in the IP protocol suite by forcing everyone to use big endian byte ordering for all 16 and 32 bit fields by htons (host-to-network-short)and htonl functions. main() { unsigned int n; char *a = (char *) &n; n = 0x11223344; printf("%02x, %02x, %02x, %02x\n", a[0], a[1], a[2], a[3]); } Output on a Sparc: 11, 22, 33, 44 Output on a Pentium: 44, 33, 22, 11

  23. Incompatible data representation (2) • Different sizes of integers and other types • Different floating point representations • Different character sets • Alignment requirements Need standard data representation and associated encoding /decoding functions to enable communication between heterogeneous systems • e.g. Sun’s RPC uses XDR (eXternal Data Representation)

  24. Interface Definition Language (IDL) SUN XDR (eXternal Data Representation) • A subset of ASN.1 (ITU), a standard data representation for message exchanges between clients and servers. • Data types and structs are translated into C language by rpcgen (see XXX.h, generated by rpcgen). • A set of encode/decode routines (see XXX_xdr.c) to convert between XDR and the local representations.

  25. const MAXSUBJ = 64; typedef char id[8]; typedef char code[8]; struct registargs { code subj_code; id stud_id; }; struct status { code subj_code; int regist_stat; }; struct regist_status { int total_regist; struct status subjs_status<MAXSUBJ>; }; program REGISTPROG { version REGISTVER { int REGIST(registargs) = 1; int DEREGIST(registargs) = 2; regist_status VIEW_STATUS(id) = 3; } = 1; } = 100023; Student registration system: regist.x

  26. Important data types simple data types: similar to C array fixed-length: type-name identifier[n]; variable-length: type-name identifier<n>; type-name identifier<>; array of strings string identifier<n>; string identifier<>; different from array of char in representation (explained later)

  27. Important data types constant/enumeration/type defintion/struct: similar to C const MAXSIZE = 512; enum state { BUSY=1, IDLE=2, TRANSIT=3 }; typedef long counter; typedef char code[8]; struct status { code subj_code; int regist_stat; };

  28. Data representation/alignment (1) • The basic data item size is 4 bytes. All variables or data structures should be aligned in 4-bytes. • Variable-length objects, e.g., variable-length arrays, structures, and strings, have a length in front of them.

  29. Data representation/alignment (2) • simple data types • int, unsigned int, char, bool, float, etc.: 4 bytes • double, etc.: 8 bytes • array • sequence of representations of individual elements • variable-length array has a 4-byte length in the front • string • an integer of string length, followed by a sequence of chars, one for a byte • a residue of (n mod 4) bytes are stuffed to make the total byte count a multiple of 4, e.g. string “exam-paper_01” is represented as:

More Related