什么是存根例程?
对于 C 来说,什么是存根例程?另外,一个例子也将不胜感激。
In regards to C what is a stub routine? Also an example would be much appreciated as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
对于 C 来说,什么是存根例程?另外,一个例子也将不胜感激。
In regards to C what is a stub routine? Also an example would be much appreciated as well.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
存根例程可以是(至少)两种情况之一。
首先,它可以是一个占位例程,您可以快速开发它来测试更高级别的例程,以便稍后替换真实版本。这通常用于自上而下的开发(首先编码较高级别,然后逐步深入到更详细的内容),并且可以简单如下:
或稍微复杂一些:
当然,一旦存根变得足够复杂,您可以以及实现真实的东西:-)
其次,它通常用于远程过程调用(RPC)环境。存根用于在一端编组数据并将其传送到另一端的服务器。
RPC 需要为客户端和服务器创建存根函数。它与 C 中的函数原型非常相似,但最终结果略有不同,例如:
在本示例中,调用者不是在同一程序中调用
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:
or slightly more complex:
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:
In this example, instead of calling
function
in the same program, the caller calls a client stub function (with the same prototype asfunction
), 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.
它是一个与真实函数具有相同签名的函数,但它不执行任何操作,并且可以像真实函数一样编译和执行。例如,
这些通常用作占位符,以便可以首先确定总体程序结构,然后再确定细节。
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.
These are often used as placeholders, so that overall program structure can be worked out first, and then the details.
在C语言中,存根代码实际上调用了main(),而不是操作系统或编译器。
In C language Stub Code actually call the main(), not the OS or Compiler.
在 C\C++ 中,我们可以将其作为调用另一个函数时的安全机制。
不只是调用 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
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 thethread_exit(0)
function. So instead we can return to stub, which then callsthread_exit(0)
.func
itself won't callthread_exit(0)
let stub do it.