Linux 中共享对象和普通库的区别

发布于 2024-09-25 16:33:42 字数 72 浏览 1 评论 0原文

绑定到共享对象或绑定到普通对象之间的主要区别是什么?另外,如何在某些程序之间共享某些变量并知道我们的变量永远不会被另一个程序更改?

What are the main differences between binding to a shared object or to an ordinary object? Also how is this possible to share some variables between some programs and knowing that our variables are never changed by another program?

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

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

发布评论

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

评论(1

尛丟丟 2024-10-02 16:33:42

变量永远不会在程序之间共享。 (虽然可以共享专门分配的共享内存,但这是一个“对象”,而不是 C 术语中的“变量”。)您感到困惑的是,磁盘上的支持是共享的进程之间,无论是主程序(静态或动态链接)还是共享库文件都是相同的。当内容与磁盘上的内容相同时,操作系统的虚拟内存实现负责为多个进程使用同一物理内存页面,并在运行时制作页面的物理副本(如果它们被写入)。所有这些对您的应用程序都是透明的,您的应用程序会看到一个线性 32 位或 64 位地址空间,仅由其自己的代码和数据组成。

在实践中,动态链接系统进行了许多存储优化,将每个进程将更改的数据隔离到几个页面,从而允许绝大多数页面在使用相同可执行文件或相同库的进程之间共享。

Variables are not shared between programs, ever. (Although specially-allocated shared memory can be shared, this is an "object" and not a "variable" in C terminology.) Where you're confused is that the on-disk backing is what's shared between processes, and this is the same whether it's the main program (static or dynamic linked) or a shared library file. The operating system's virtual memory implementation takes care of using the same page of physical memory for multiple processes when the contents are unchanged from what's on-disk, and making physical duplicates of pages at runtime if they're written to. All of this is transparent to your application, which sees a linear 32- or 64-bit address space consisting of nothing but its own code and data.

In practice, the dynamic linking system makes a number of storage optimizations which isolate the data which will be changed per-process to a few pages, allowing the vast majority of pages to be shared between processes that use the same executable file or same libraries.

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