如何在 Linux 中的类和命名空间内进行克隆()?
我正在学习操作系统简介课程,我们将使用 Linux 中的 clone() 调用来创建线程,然后用它们做一些事情。我似乎在使用clone()时遇到了麻烦。
我已将代码构建为一个类(称为“Homework”),该类位于该类(“Course”)的命名空间中。这可能是问题所在,因为这是我第一次真正使用名称空间关键字。我正在尝试利用我很少做的事情来变得更有经验,所以如果我犯了一个愚蠢的错误,那就这样吧。
我在网上找到了一些文章,但没有多大帮助。我已经阅读了手册页,但我想我的经验不足,无法理解问题所在。一天!感谢您的任何帮助:)
我想要有一种方法来捕获类内的克隆:
// -- Header -- //
namespace _Course_ {
class _Homework_ {
...
int threadCatch(void *);
...
};
}
// -- Source -- //
namespace _Course_ {
void _Homework_::threadTest(void) {
...
// From web article
void **childStack;
childStack = ( void **) malloc(KILOBYTE);
clone(threadCatch, childStack, CLONE_VM | CLONE_FILES, NULL);
...
}
int _Homework_::threadCatch(void * ){
cout << getpid() << " cloned." << endl;
exit(0);
}
}
这就是我目前所拥有的。我尝试了不同的方法(将捕手从类中取出,然后从命名空间中取出)。它编译了两次,但是当我尝试在 make clean 之后重新编译时,它告诉我函数(threadCreate)在多个位置声明。由于这些奇怪的错误,我确信我做错了什么,我不会去破解它,而是会听取一些意见。我应该做什么,或者接下来我应该读什么?谢谢!
I'm taking an intro to operating systems course and we're to use the clone() call in linux to create threads and then do some stuff with them. I seem to be having trouble just using clone() at all.
I've structured my code into a single class (called Homework) which is in the namespace for the class (Course). This may be the problem as this is the first time I've really used the namespace keyword. I'm trying to use the things I rarely do to become more experienced with it so if I have a dumb mistake, so be it.
I found some articles on the web but they didn't help much. I've read the man page but I guess I'm not experienced enough to understand what the problem is. One day! Thanks for any assistance :)
I want to have the method to catch the clones inside the class:
// -- Header -- //
namespace _Course_ {
class _Homework_ {
...
int threadCatch(void *);
...
};
}
// -- Source -- //
namespace _Course_ {
void _Homework_::threadTest(void) {
...
// From web article
void **childStack;
childStack = ( void **) malloc(KILOBYTE);
clone(threadCatch, childStack, CLONE_VM | CLONE_FILES, NULL);
...
}
int _Homework_::threadCatch(void * ){
cout << getpid() << " cloned." << endl;
exit(0);
}
}
Is what I currently have. I've tried different ways (taking the catcher out of the class, then namespace). It's compiled twice but when I try to recompiled after a make clean it tells me the function (threadCreate) is declared in multiple locations. Because of these weird errors I'm sure I'm doing something wrong and instead of hack at it I'll take some opinions. What should I do, or what should I read next? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 catch 函数定义为静态类函数。
另外(您可能不需要这个,但为了以防万一,我会在这里说)您可能还需要使用范围解析运算符将其发送到clone()。我不这么认为,因为你已经在家庭作业课中使用它了。但我这么说只是为了以防万一,它可能对你有帮助。
Define your catch function as a static class function.
Also (and you probably don't need this, but just in case, I'll say it here) you might also need to use the scope resolution operators to send it to clone(). I don't think so, since you're using it inside of the Homework class already. but I say it just in case, it might help you.
clone(2) 系统调用需要一个指向带有 C 链接。由于您使用的是 C++,我建议将
threadCatch()
函数移至全局命名空间中,并将其声明为extern "C"
函数。您还可以将类中的方法声明为static
,但我认为使其成为具有 C 链接的自由函数更符合函数作为参数传递的方式。如果您需要调用
threadCatch()
函数中存在于其作用域之外的 C++ 对象,您可以将指向这些对象的指针作为arg
参数传递给clone()
调用。然后,您的threadCatch()
函数会将arg
转换为适当的类型,以便您可以相应地访问您的 C++ 对象。The clone(2) system call expects a pointer to a function with C linkage. Since you're using C++ I'd recommend moving your
threadCatch()
function into the global namespace and declare it as anextern "C"
function. You could also declare the method in your class asstatic
but I feel that making it a free function with C linkage more closely matches how the function is to be passed as a parameter.If you need to make calls to C++ objects inside your
threadCatch()
function that exist outside of it's scope you can pass pointers to those objects as thearg
parameter to theclone()
call. YourthreadCatch()
function would then cast thearg
to the appropriate type so that you can access your C++ object(s) accordingly.