如何在 Linux 中的类和命名空间内进行克隆()?

发布于 2024-10-16 16:32:44 字数 1096 浏览 2 评论 0原文

我正在学习操作系统简介课程,我们将使用 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 技术交流群。

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

发布评论

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

评论(2

稚然 2024-10-23 16:32:44

将 catch 函数定义为静态类函数。

static int threadCatch(void *);

另外(您可能不需要这个,但为了以防万一,我会在这里说)您可能还需要使用范围解析运算符将其发送到clone()。我不这么认为,因为你已经在家庭作业课中使用它了。但我这么说只是为了以防万一,它可能对你有帮助。

clone(Homework::threadCatch, childStack, CLONE_VM | CLONE_FILES, NULL);

Define your catch function as a static class function.

static int threadCatch(void *);

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(Homework::threadCatch, childStack, CLONE_VM | CLONE_FILES, NULL);
魔法唧唧 2024-10-23 16:32:44

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 an extern "C" function. You could also declare the method in your class as static 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 the arg parameter to the clone() call. Your threadCatch() function would then cast the arg to the appropriate type so that you can access your C++ object(s) accordingly.

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