Eclectic

About programming and maybe more…

Archive for February 20th, 2007

Variable arguments in C

Posted by tread on February 20, 2007

Another area that I never really looked into is how to accept variable arguments in C. This time, the tutorial I am using is here.

What it boils down to is:
#include <stdargs.h> // include stdargs to use the macros va_*

int sum(int n, …) // n is the first argument, and denotes number of arguments
{
va_list args; // args here will store the list of arguments
int sum = 0, i = 0;
va_start (args, n); // initialize args – pass args and n to va_start
for (i = 0; i < n; i++) // loop n times – number of arguments
sum += va_arg (args, int); // va_arg () will return the argument – give it the initialized args and the type (int)
va_end (args); // end processing the argument list.
return sum;
}

That’s about it.
You can call this as
x = sum (5, 1, 2, 3, 4, 5) ;
or
x = sum (2, 100, 200);

Posted in Programming | 4 Comments »

Remote Procedure Calls on Linux

Posted by tread on February 20, 2007

RPCs have been around for quite some time, and I never have got around to reading up about them. Then I read this article by Jeff Bezanson (glibc on Linux) which seemed a good place to start.

Basics:

RPCs will execute your code on a remote machine without your needing to make network calls.

Client -> makes the call.

Server -> processes the call and executes whatever it is asked to do.

Passing pointers to data etc. is handled by XDR, which serializes the data before sending it across the network. Thus, even link lists can be sent across without your explicit intervention! This is done using an RPC compiler (rpcgen)

Steps:

1. Create a file file.x which contains a description of the data, and the procedures that will be called remotely. Program_id (decided arbitrarily) and version_number must be specified – program_id will be used to setup port numbers, and version_number is used to check that clients are communicating with a server that understands the language (in case of backward/forward incompatibility)

2. Invoke rpcgen on file.x – this results in file.h, file_svc.c, file_xdr.c, file_clnt.c

file.h: contains declarations, function prototypes in C – read this!

file_svc.c to be linked with the server, file_clnt.c with the client, and file_xdr.c with both.

3. For the server, function prototypes ending in _svc are what we need to implement.

(Note: handle quirky result value – functions return pointer to return value)

4. In the client main(), call clnt_create() with hostname, program_id and version_number.

5. Then call your rpc, note: result will be a pointer.

6. Link everything together, fire the server and then the client!

Questions as yet unanswered:

1. If the result is a pointer, does this mean that the memory location where the result is stored will get overwritten by multiple rpc calls?

2. What about permissions? How are they handled? Can user A call a server running under user B? If User B has sudo access on the server, but user A does not, what happens. Basically, how are permissions handled?

Of course, this is not really a detailed description – it’s only meant to jog my memory. For a proper tutorial, refer to the original article.

Posted in Programming | Leave a Comment »

Hello, World!

Posted by tread on February 20, 2007

Seems apt to have “Hello, World!” as the title to the first post – as I mentioned in my profile, I’m a software engineer who has realized he needs to keep studying something new everyday. So this blog will follow me around in my forays around assorted computer related topics – and will serve as a tool to jog my memory about what I read.

Posted in Uncategorized | 1 Comment »