The server-side procedure in default mode is shown in the following code example.
Example 3-13 Default Mode Server Stub
#include "add.h" int * add_1(argp, rqstp) add_arg *argp; struct svc_req *rqstp; { static int result; result = argp->first + argp->second; return(&result); } |
Compile-Time MT-Safe Code
By default, the code generated by rpcgen is not MT safe. It uses unprotected global variables and returns results in the form of static variables. The -M flag generates MT-safe code that can be used in a multithreaded environment. This code can be used with the C-style flag, the ANSI C flag, or both.
An example of an MT-safe program with this interface follows. The rpcgen protocol file is msg.x, shown in the following code example.
Example 3-14 MT-Safe Program: msg
program MESSAGEPROG { version PRINTMESSAGE { int PRINTMESSAGE(string) = 1; } = 1; } = 0x4001; |
A string is passed to the remote procedure, which prints it and returns the length of the string to the client. The MT-safe stubs are generated with the rpcgen -M msg.x command.
Client-side code that could be used with this protocol file is shown in the following code example.
Example 3-15 MT-Safe Client Stub
A pointer to both the arguments and the results needs to be passed in to the rpcgen-generated code in order to preserve re-entrancy. The value returned by the stub function indicates whether this call is a success or a failure. The stub returns RPC_SUCCESS if the call is successful. Compare the MT-safe client stub, generated with the -M option, and the MT-unsafe client stub shown in Example 3-16. The client stub that is not MT-safe uses a static variable to store returned results and can use only one thread at a time.
Example 3-16 Client Stub (MT Unsafe)
The server side code is shown in the following example.
Note - When compiling a server that uses MT-safe mode, you must link in the threads library. To do so, specify the -lthread option in the compile command.
Example 3-17 MT-Safe Server Stub