How do you clone() in linux inside a class and namespace?

759 views Asked by At

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!

2

There are 2 answers

3
Brian Onn On BEST ANSWER

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);
2
Void - Othman On

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.