Beginning android-application-development-wrox-programmer-to-programmer.9781118017111.55067
Problems with Send and Receive Low level –programmer is engaged in I/O –server often not modular...
-
Upload
beverley-newton -
Category
Documents
-
view
212 -
download
0
Transcript of Problems with Send and Receive Low level –programmer is engaged in I/O –server often not modular...
![Page 1: Problems with Send and Receive Low level –programmer is engaged in I/O –server often not modular –takes 2 calls to get what you want (send, followed by.](https://reader036.fdocuments.us/reader036/viewer/2022083009/56649ecf5503460f94bdccba/html5/thumbnails/1.jpg)
Problems with Send and Receive
• Low level– programmer is engaged in I/O– server often not modular– takes 2 calls to get what you want (send,
followed by receive) -- error prone
• Solution– use procedure calls -- familiar model
![Page 2: Problems with Send and Receive Low level –programmer is engaged in I/O –server often not modular –takes 2 calls to get what you want (send, followed by.](https://reader036.fdocuments.us/reader036/viewer/2022083009/56649ecf5503460f94bdccba/html5/thumbnails/2.jpg)
Remote Procedure Call (RPC)
• Allow procedure calls to other machines– servicing of procedure remote– caller blocks until procedure finished, as usual– simpler than explicit message passing
• Complications– caller and receiver in different address spaces– parameter passing– where is the server?– what about crashes?
![Page 3: Problems with Send and Receive Low level –programmer is engaged in I/O –server often not modular –takes 2 calls to get what you want (send, followed by.](https://reader036.fdocuments.us/reader036/viewer/2022083009/56649ecf5503460f94bdccba/html5/thumbnails/3.jpg)
Programming Client/Server Applications with RPC
Outline of Client code
while (1) {
build request
reply = foo(params)
do something
}
Note: Server can be
written as collection
of several procedures
Outline of Server code
foo(params) {
….
return reply;
}
bar (params) {
….
return reply;
}
![Page 4: Problems with Send and Receive Low level –programmer is engaged in I/O –server often not modular –takes 2 calls to get what you want (send, followed by.](https://reader036.fdocuments.us/reader036/viewer/2022083009/56649ecf5503460f94bdccba/html5/thumbnails/4.jpg)
Basics of RPC Implementation
• Goal: provide complete transparency– For RPC, replace a normal procedure call with:
• pack arguments (including func) into a message via “stub function”
– may need to worry about byte ordering, linked list, etc
• send message to server; block waiting for reply
– On receipt at server: unpack and push args, call func. (create new thread)• Implemented by forking a “stub” (sometimes called
“skeleton” function)
– On receipt of reply at client: put result where it belongs, unblock client
![Page 5: Problems with Send and Receive Low level –programmer is engaged in I/O –server often not modular –takes 2 calls to get what you want (send, followed by.](https://reader036.fdocuments.us/reader036/viewer/2022083009/56649ecf5503460f94bdccba/html5/thumbnails/5.jpg)
RPC Implementation Issues
• Weakly typed languages– E.g., C --- what to do if unbounded array passed to
RPC?
• Communication via global variables impossible• Binding
– How does client know where server is?• One solution: use a database
• Failures?– What if function is partially executed, or
executed twice, or executed never?
![Page 6: Problems with Send and Receive Low level –programmer is engaged in I/O –server often not modular –takes 2 calls to get what you want (send, followed by.](https://reader036.fdocuments.us/reader036/viewer/2022083009/56649ecf5503460f94bdccba/html5/thumbnails/6.jpg)
RPC Parameter Passing
• Client machine may be different than server– ignore this issue -- someone has to convert data
• Parameter issues– what to provide?– can be important performance issue– not as easy as it seems at first glance
![Page 7: Problems with Send and Receive Low level –programmer is engaged in I/O –server often not modular –takes 2 calls to get what you want (send, followed by.](https://reader036.fdocuments.us/reader036/viewer/2022083009/56649ecf5503460f94bdccba/html5/thumbnails/7.jpg)
Call by Value
• Simple semantics• Just package up the args, and sent them• Server uses these args
– doesn’t need to send them back
![Page 8: Problems with Send and Receive Low level –programmer is engaged in I/O –server often not modular –takes 2 calls to get what you want (send, followed by.](https://reader036.fdocuments.us/reader036/viewer/2022083009/56649ecf5503460f94bdccba/html5/thumbnails/8.jpg)
Call by Reference
• What do pointers mean across machines?– remember, they mean nothing across address
spaces
• Could do the following:– pass pointer– send back message to client on each reference
• SLOW!
![Page 9: Problems with Send and Receive Low level –programmer is engaged in I/O –server often not modular –takes 2 calls to get what you want (send, followed by.](https://reader036.fdocuments.us/reader036/viewer/2022083009/56649ecf5503460f94bdccba/html5/thumbnails/9.jpg)
Call by Copy/Restore
• Similar to call by reference– parameter copied in, same as call by value– but when procedure finished, copy back to
caller– not quite same as call by reference, but:
• can be problematic if pointer parameter points to a complex data type, e.g. graph or list
– method of choice for “reference parameters” when using RPC
![Page 10: Problems with Send and Receive Low level –programmer is engaged in I/O –server often not modular –takes 2 calls to get what you want (send, followed by.](https://reader036.fdocuments.us/reader036/viewer/2022083009/56649ecf5503460f94bdccba/html5/thumbnails/10.jpg)
(Contrived) example of how call by reference and call by copy-
restore can differ
int a;
unsafe(int x) {
x= 2; a= 0;
}
int main( ) {
a= 1; unsafe(a); print (a)
}
Call by reference outputs 0; callby copy-restore outputs 2
![Page 11: Problems with Send and Receive Low level –programmer is engaged in I/O –server often not modular –takes 2 calls to get what you want (send, followed by.](https://reader036.fdocuments.us/reader036/viewer/2022083009/56649ecf5503460f94bdccba/html5/thumbnails/11.jpg)
Rendezvous• Similar to RPC
– Key difference: no new process created on the server (but unlike RPC, there is synchronization)
– Caller side is the same as with RPC– Server looks roughly as follows:
in op1(…)
execute code for op1
[] op2(…)
execute code for op2
ni– Server blocks until >= 1 pending invocation on
any op (can be implemented via UNIX select)
![Page 12: Problems with Send and Receive Low level –programmer is engaged in I/O –server often not modular –takes 2 calls to get what you want (send, followed by.](https://reader036.fdocuments.us/reader036/viewer/2022083009/56649ecf5503460f94bdccba/html5/thumbnails/12.jpg)
Bank Account with RPCmodule Customer deposit(amt) or withdraw(amt)
module Bank void deposit(int) void withdraw(int) int balance = initbalance, cond try deposit(int amt) balance += amt broadcast(try) withdraw(int amt) while (amt > balance) wait(try) balance -= amt
Note: deposit and withdrawneed to be monitor functions
![Page 13: Problems with Send and Receive Low level –programmer is engaged in I/O –server often not modular –takes 2 calls to get what you want (send, followed by.](https://reader036.fdocuments.us/reader036/viewer/2022083009/56649ecf5503460f94bdccba/html5/thumbnails/13.jpg)
Bank Account with Rendezvousmodule Customer deposit(amt) or withdraw(amt)
module Bank void deposit(int) void withdraw(int) int balance = initbalance process Teller { while (true) in deposit(amt) balance += amt [] withdraw(amt) and balance >= amt balance -= amt ni }