是否可以使用Linux的clone()系统调用在同一地址空间中运行多个应用程序?

发布于 2024-08-09 20:56:43 字数 239 浏览 6 评论 0原文

如果不将 CLONE_VM 标志传递给clone(),则新进程将与原始进程共享内存。这可以用来使两个不同的应用程序(两个 main()'s)在同一进程中运行吗?理想情况下,这就像使用 CLONE_VM 调用 clone() 然后调用 exec() 一样简单,但我意识到这可能更复杂。至少,我假设生成的应用程序需要编译为可重定位(-fPIC)。我意识到我总是可以将应用程序重新编码为库,并创建一个主应用程序,将其他“应用程序”生成为线程,但我很好奇这种方法是否可行。

If you don't pass the CLONE_VM flag to clone(), then the new process shares memory with the original. Can this be used to make two distinct applications (two main()'s) run in the same process? Ideally this would be as simple as calling clone() with CLONE_VM and then calling exec(), but I realize it's probably more involved. At the very least, I assume that the spawned application would need to be compiled to be relocatable (-fPIC). I realize I could always recode applications to be libraries, and create a master app spawning the other 'apps' as threads, but I'm curious of this approach is possible.

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

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

发布评论

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

评论(3

打小就很酷 2024-08-16 20:56:43

嗯,是的,这就是线程,减去“两个不同的 main()/应用程序”部分。

事实上,clone(2) 存在的原因是为了实现线程。

Clone(2) 或多或少要求您声明一个单独的堆栈(如果您不这样做,它就会创建一个堆栈),因为如果没有它,子级将无法从当前调用级别返回而不破坏父级堆栈。

一旦开始为每个进程设置堆栈,那么您不妨只使用 posix 线程库。

至于加载两个不同应用程序的部分,调用 execve(2) 很可能不是这样做的方法。如今内核无论如何都不能精确地运行程序。更典型的是,映像被设置为运行 Elf 动态加载器,这就是内核真正运行的全部内容。然后加载器将进程及其库mmaps(2)加载到地址空间中。当然,可以这样做以获得“两个不同的应用程序”,并且线程调度程序很乐意通过克隆(2)将它们作为两个进程运行。

Well, yes, that's what threads are, minus the "two distinct main()/application" part.

In fact, the reason clone(2) is there is to implement threads.

Clone(2) more-or-less requires you to declare a separate stack (if you don't it makes one), because without it the child won't be able to return from its current call level without destroying the parent's stack.

Once you start setting up stacks for each process then you might as well just use the posix thread library.

As for the part where two different applications are loaded, calling execve(2) would most likely not be the way to do it. These days the kernel doesn't precisely run programs anyway. It's more typical that the image is set up to run the Elf dynamic loader, and that's all that the kernel really runs. The loader then mmaps(2)s the process and its libraries into the address space. Certainly that could be done to get the "two distinct applications", and the thread scheduler would be happy to run them as two processes via clone(2).

琉璃梦幻 2024-08-16 20:56:43

为什么不将应用程序编译成相同的可执行文件并在 main 中将它们作为线程启动?

无论如何,将它们作为单独的任务运行有什么问题?如果您确实愿意,您仍然可以共享内存。

Why not compile the applications into the same executable and just start them as threads in main?

What is the problem running them as separate tasks anyway? You can still share memory if you really want to.

半窗疏影 2024-08-16 20:56:43

简短的回答:这是不可能的。

好吧,如果您愿意编写自己的自定义 ELF 加载程序并模拟内核通常为进程执行的许多操作,这是可能的。

最好将每个应用程序编译到一个库中,该库仅公开一个函数 main(重命名为其他函数)。然后主存根程序应该链接两个库并调用每个库的导出函数。

Short answer: it's impossible.

Well, it's possible if you're willing to write your own custom ELF loader and simulate a lot of things that the kernel normally does for a process.

It's better to compile each of the apps into a library that exposes exactly one function, main (renamed to something else). Then the main stub program should link with the two libraries and call each one's exported function.

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