fork 的克隆等价物?
我想使用 clone
函数的命名空间功能。阅读手册页,似乎 clone
有很多我需要担心的复杂细节。
是否存在与良好的 fork()
等效的clone
调用?
我已经熟悉fork
,并且相信如果我在clone
中有一个起点,我可以从那里添加标志和选项。
I'd like to use the namespacing features of the clone
function. Reading the manpage, it seems like clone
has lots of intricate details I need to worry about.
Is there an equivalent clone
invocation to good ol' fork()
?
I'm already familiar with fork
, and believe that if I have a starting point in clone
, I can add flags and options from there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这会起作用,但我不完全确定某些指针参数。
在 flags 参数中,它的低字节用于指定发送哪个信号来通知父线程执行诸如死亡或停止之类的操作。我相信所有实际标志都会打开与
fork
不同的开关。查看内核代码表明情况确实如此。如果您确实想要获得类似于 fork 的东西,您可能需要调用 sys_clone ,它不接受函数指针,而是像 fork 一样返回两次。
I think that this will work, but I'm not entirely certain about some of the pointer arguments.
In the flags parameter the lower byte of it is used to specify which signal to send to notify the parent of the thread doing things like dying or stopping. I believe that all of the actual flags turn on switches which are different from
fork
. Looking at the kernel code suggests this is the case.If you really want to get something close to
fork
you may want to callsys_clone
which does not take function pointer and instead returns twice likefork
.您可以使用 fork() 分叉一个普通的子进程,然后使用 unshare() 创建一个新的命名空间。
命名空间有点奇怪,我看不到它们的很多用例。
You could fork a normal child process using fork(), then use unshare() to create a new namespace.
Namespaces are a bit weird, I can't see a lot of use-cases for them.
clone() 用于创建线程。 clone() 和 fork() 之间的最大区别在于,clone() 意味着从一个单独的入口点(一个函数)开始执行,而 fork() 只是从调用代码的同一点继续向下执行。手册页定义中的 int (*fn)(void *) 是一个函数,它在退出时返回一个 int,即退出状态。
最接近于clone的调用是pthread_create(),它本质上是clone()的包装。
这并不能让你获得 fork() 行为。
clone() is used to create a thread. The big difference between clone() and fork() is that clone() is meant to execute starting at a separate entry point - a function, whereas fork() just continues on down from the same point in the code from where was invoked. int (*fn)(void *) in the manpage definition is the function, which on exit returns an int, the exit status.
The closest call to clone is pthread_create() which is essentially a wrapper for clone().
This does not get you a way to get fork() behavior.