什么是存根例程?

发布于 2024-09-29 01:58:49 字数 37 浏览 0 评论 0原文

对于 C 来说,什么是存根例程?另外,一个例子也将不胜感激。

In regards to C what is a stub routine? Also an example would be much appreciated as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瑶笙 2024-10-06 01:58:49

存根例程可以是(至少)两种情况之一。


首先,它可以是一个占位例程,您可以快速开发它来测试更高级别的例程,以便稍后替换真实版本。这通常用于自上而下的开发(首先编码较高级别,然后逐步深入到更详细的内容),并且可以简单如下:

int getCount (void) { return 7; } // just return fixed value for testing.

或稍微复杂一些:

// Cycle through values for some variety.
int getCount (void) {
    static int retvals[] = {2,7,1,8,2,8,1,8,2,8,4,5,9};
    static int pos = -1;
    pos = (pos + 1) % (sizeof (retvals) / sizeof (*retvals));
    return retvals[pos];
}

当然,一旦存根变得足够复杂,您可以以及实现真实的东西:-)


其次,它通常用于远程过程调用(RPC)环境。存根用于在一端编组数据并将其传送到另一端的服务器。

RPC 需要为客户端和服务器创建存根函数。它与 C 中的函数原型非常相似,但最终结果略有不同,例如:

+----------------+
| Client         |
|  +----------+  |                   +---------------+
|  |  caller  |  |                   | Server        |
|  |----------|  |                   |  +----------+ |
|  | stub_cli |---- (over the wire) --->| stub_svr | |
|  +----------+  |                   |  |----------| |
+----------------+                   |  | function | |
                                     |  +----------+ |
                                     +---------------+

在本示例中,调用者不是在同一程序中调用 function,而是调用客户端 stub< /em> 函数(与 function 具有相同的原型),它负责打包信息并将其通过网络传输到另一个进程。

这可以是同一台机器,也可以是不同的机器,这并不重要——RPC 的优点之一是能够随意移动服务器。

在服务器中,有一个“侦听器”进程将接收该信息并将其传递到服务器。服务器的存根接收信息,将其解包,并将其传递给实际函数。

然后,真正的函数执行所需的操作并返回到服务器存根,服务器存根可以打包返回信息并将其传递回客户端存根。

然后,客户端存根将其解压并将其传递回调用者。

A stub routine can be one of (at least) two things.


First, it can be a place-holder routine which you quickly develop to test a higher level routine, with the intention of substituting a real version later on. This is typically used with top-down development (coding the higher levels first then working your way down to the more detailed stuff) and can be as simple as:

int getCount (void) { return 7; } // just return fixed value for testing.

or slightly more complex:

// Cycle through values for some variety.
int getCount (void) {
    static int retvals[] = {2,7,1,8,2,8,1,8,2,8,4,5,9};
    static int pos = -1;
    pos = (pos + 1) % (sizeof (retvals) / sizeof (*retvals));
    return retvals[pos];
}

Of course, once the stub gets complex enough, you may as well just implement the real thing :-)


Secondly, it's commonly used in remote procedure call (RPC) environments. A stub there is used for marshalling data at one end and communicating it to a server at the other end.

RPC needs to create stub functions for the client and a server. It's very similar to a function prototype in C but the end result is slightly different, such as:

+----------------+
| Client         |
|  +----------+  |                   +---------------+
|  |  caller  |  |                   | Server        |
|  |----------|  |                   |  +----------+ |
|  | stub_cli |---- (over the wire) --->| stub_svr | |
|  +----------+  |                   |  |----------| |
+----------------+                   |  | function | |
                                     |  +----------+ |
                                     +---------------+

In this example, instead of calling function in the same program, the caller calls a client stub function (with the same prototype as function), which is responsible for packaging up the information and getting it across the wire to another process.

This can be the same machine or a different machine, it doesn't really matter - one of the advantages of RPC is to be able to move servers around at will.

In the server, there's a 'listener' process that will receive that information and pass it to the server. The server's stub receives the information, unpacks it, and passes it to the real function.

The real function then does what it needs to and returns to the server stub which can package up the return information and pass it back to the client stub.

The client stub then unpacks that and passes it back to the caller.

一梦等七年七年为一梦 2024-10-06 01:58:49

它是一个与真实函数具有相同签名的函数,但它不执行任何操作,并且可以像真实函数一样编译和执行。例如,

int MyStub(char * str)
{
    /* Stub - Does Nothing */

    return 0;
}

这些通常用作占位符,以便可以首先确定总体程序结构,然后再确定细节。

It is a function with the same signature as the real function but it does nothing, and can be compiled and executed just like the real thing. e.g.

int MyStub(char * str)
{
    /* Stub - Does Nothing */

    return 0;
}

These are often used as placeholders, so that overall program structure can be worked out first, and then the details.

撩心不撩汉 2024-10-06 01:58:49

在C语言中,存根代码实际上调用了main(),而不是操作系统或编译器。

In C language Stub Code actually call the main(), not the OS or Compiler.

好倦 2024-10-06 01:58:49

在 C\C++ 中,我们可以将其作为调用另一个函数时的安全机制。

void stub(void (*func)(int), int arg) {
   (*func)(arg);
   thread_exit(0);
}

不只是调用 func 的原因是为了确保一旦运行完成,它不会返回存储在函数中的随机值。尤其是在不调用 thread_exit(0) 函数的情况下。因此,我们可以返回存根,然后调用 thread_exit(0)func 本身不会调用 thread_exit(0) 让存根来做。

In C\C++ we can have it as a safe mechanism when calling another function

void stub(void (*func)(int), int arg) {
   (*func)(arg);
   thread_exit(0);
}

The reason to not just call func is to ensure that once it's done running it doesn't return to the random value stored at the top of the stack especially without calling the thread_exit(0) function. So instead we can return to stub, which then calls thread_exit(0). func itself won't call thread_exit(0) let stub do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文