1

是否可以编写 C 或 C++ 源文件并自动创建源文件中函数的客户端/服务器实现而不需要接口定义语言?考虑以下示例:

程序.c

int foo() {
  return 2;
}

我想foo.c分成两个文件:client.c如下server.c

客户端.c

int foo() {
  return server_foo();
}

服务器.c

int server_foo() {
  return 2;
}

我想server.c在另一台机器上运行而不是在运行的机器上运行client.c,所以我需要实现某种形式的 RPC。我研究过的所有东西(ONC RPC、XML RPC、Apache Thrift...)都需要在接口文件中手动定义函数原型。有什么方法可以提供prog.c给可以自动生成分布式应用程序源代码的程序?

注意:我希望这些程序在 Unix 系统上运行!

4

3 回答 3

2

It is not possible in general (in particular because there is no general way to serialize arbitrary C data (or C++ data), like e.g. some FILE* handle or some void* pointer -e.g. obtained by dlopen(3) ..., or some C++11 instance of std::thread).

In general it is not possible: a shared memory is not the same as a collection of agents with message passing.

But we could assume that your prog.c has only functions involving easily serializable types. This is a strong hypothesis which is usually not true. (for instance, if you represent a tree, or some directed acyclic graph, using some struct-s, the C code does not know that it is a tree or a DAG and how it should be serialized; and if your data structure represents a more general graph you should know much more than its C coded types to serialize it, since its naive serialization would be infinite... because we don't know what is the shareable data...).

Also, even if all your types are serializable, you don't want in practice to distribute every function. For example, doing a remote call when computing strlen of strings does not make sense in practice: computing the strlen locally is thousands -or millions- of time faster than making a remote procedure call (even to an infinitely fast remote server, given the current networking delays; an RPC takes several milliseconds to transmit and receive data).

So you have to cleverly choose, within your prog.c, which functions you want to distribute on a remote server.

For example, if prog.c contains the following function:

// return a heap-allocated string to be free-d by caller
char* make_name(int x) {
   char buf[24];
   snprintf(buf, sizeof(buf), "NAME_%d", x);
   return strdup(buf);
}

you don't want to distribute it. Making an RPC call from it does not make any practical sense.

However, you might for instance consider customizing GCC (assuming you have a recent version e.g. 4.7 or 4.8) with your MELT extension which handle some common cases automatically.

MELT is a lisp-y domain specific language, implemented as a GCC [meta-]plugin, to extend GCC

So you could code your extension in MELT for gcc (and g++) which would process prog.c (while GCC is compiling it) and dump appropriate glue code (e.g. someprog_generated.x file for ONC RPC...), at least for those functions whose signature involve serializable types.

The issue is to define what exactly are your serializable types and how you serialize them (and what are the routines you want to distribute remotely); then you'll spend a week -or more- to code that MELT extension. Please ask on gcc-melt@googlegroups.com list for help and advices. You could consider adding your own #pragma-s and/or your own __attribute__ to guide the serialization and the remote distribution...

See also application checkpointing, message-passing and MPI wikipages...

于 2013-11-12T00:34:55.963 回答
0

没有这样的工具来生成基于 c/c++ 源代码的客户端/服务器代码,只需使用 Apache Thrift,如您所知,它只需要您编写 DDL(数据定义语言)。然后它可以生成代码。

于 2013-11-12T02:32:03.430 回答
0

我在 OP 接受答案后回复...

我们开发了一个生成分布式应用程序的工具(互联网函数调用编译器,ifcc)。它使用 C 语言本身作为 IDL。函数调用参数被视为“请求”,函数返回值被视为“响应”。它还进行任意指针追逐。即它序列化/反序列化嵌套结构、指针、typedef 等。例如,指向树或双向链表的指针将由客户端存根序列化并由服务器存根反序列化。

非原始数据类型的响应通过指向 struct、typedef 或 char * 的指针返回。当不再需要响应时,客户端必须释放内存。

生成的 C 源代码在较低级别使用 ONC RPC/XDR 例程。

我们目前正在将基于浏览器的可见性集成到生成的代码中,以便可以在运行时查看分布式应用程序。这项工作是针对多核处理器的 C-Parallel 编程的一部分,其中问题类似于创建分布式应用程序。有关 C-Parallel 适合多核和分布式应用程序的更多信息,请参阅ac.accord.com/doc/上的幻灯片“在 C 中使用只有四个关键字的可扩展多核和分布式应用程序的简单并行编程c-parallel.pdf .

Ifcc 是一个原型编译器,供我们实验和学习使用 C-Parallel 轻松创建应用程序管理就绪且可扩展的分布式 + 多核应用程序所需的内容。

对于那些有兴趣尝试 ifcc 的人,我们可以将其提供给外部使用。当前工具将生成可与 TCP/UDP 一起使用的分布式应用程序,远程调用将是同步的。C-Parallel 将在完成后允许异步调用。

使 ifcc 可供外部使用可能需要几周左右的时间。我们得到的任何反馈都将有助于我们正确使用 C-Parallel。披露:我在 Accord Software, Inc. 工作。

如果有兴趣试驾ifcc,请告诉我。

于 2013-11-12T22:18:55.730 回答