clone System Call Linux

hell-student

Lieutenant
Registriert
Nov. 2007
Beiträge
671
Hallo Zusammen,

Ich möchte statt fork() den clone syscall benutzen doch bekomms einfach nicht hin. Parameter sind ja:

int clone(int (*fn)(void *), void *child_stack, int flags, void *arg,...)

EDIT: Programmiersprache ist C:


mein Funktion die ich gerne Aufrüfen möchte heißt jetzt mal:

void CalcQuad(). Denke dass ich aber als Rückgabeparameter ja int brauche und daher in der Funktion retrun 0 zurückgebe, wenn diese fertig ist.
child_stack ist ja Stack, der vom "Parent" übergeben werden muss also mit malloc.
flags sind ja die flags in der MAN also man 2 clone.
und arg sind die args für die Funktion, die ich aufrufen möchte, also hier NULL.

Hat jemand ein Beispiel oder ein Tut wo das bissel besser erklärt ist.

Dane schonmal
 
Zuletzt bearbeitet:
Du solltest clone() von der Kernel funktion generell nicht benutzen, sondern lieber pthreads benutzen um threads zu erstellen. Die funktion bietet dir nicht die Sicherheit, wie pthreads, z.B. pass mit clone auf das producer/consumer problem auf. Da bin ich desoefteren mal reingerannt.

Aber trotzdem ein kleines beispiel:
Code:
int produce( void * arg ) {
    int i, j;
    for( i = 0; i < SIZE; i++ ) {
        /* Mini delay... */
        for( j = 0; j < 10000; j++ );

        ((char *)arg)[i] = 0x7E;
        offset = i;
    }
    return 0;
}
Code:
char *data;
char *stack;
stack = (char *)malloc( 10000 );
data = (char *)malloc( SIZE );
int retVal;
retVal = clone( produce, stack,
                    CLONE_VM | CLONE_THREAD | CLONE_SIGHAND | SIGCHLD,
                    (void *)data );

Ja, deine Funktion sollte nicht void, sondern int sein.
 
Zuletzt bearbeitet:
Hmm Danke schonmal aber ich bekomme immer nur Fehler wegen Segmentation Fault:

Hier mal mein Quellcode (oben natürlich die includes):
Code:
const uint64_t N = 10000000;
volatile uint64_t tally = 0;

int total ()
{
  for( uint64_t i=0; i<N; ++i )
    tally += 1;
  printf( "Tally is %lld\n", tally );
  return 0;
}


int main ()
{	
  tally = 0;
  int pid;
  void *child_stack;
  child_stack = (void *) malloc(sizeof(N));
  pid = clone(total, child_stack, CLONE_FS ,NULL );
  printf("PID %d\n", pid);
  printf( "Tally is %lld\n", tally );
  if(pid==0)
  {
    total();
  }
  return 0;
}

// Liegt es vielleicht daran, dass der Stack zu klein ist??? N und tally sind ja global deklariert und müssten mit den flags von clone CLONE_FS | CLONE_VM | CLONE_THREAD | CLONE_SIGHAND | SIGCHLD auch fürs Child gelten?

Danke
 
Dein code ist auch kaputt. Dein stack pointer wird falsch initialisiert und auch schon falsch uebergeben.
Code:
child_stack = (void *) malloc(sizeof(N));
Du willst das nicht mit sizeof initialisieren, du willst genau die groesse von N haben. Nicht das sizeof N. Und es sollte ein char* sein, nicht void*
Dann ist das CLONE_FS unsinn. Du brauchst nicht die Rechte vom Parent an das CHILD vererben. Benutz das stattdessen: CLONE_VM | CLONE_THREAD | CLONE_SIGHAND | SIGCHLD

Als naechstes willst du noch einen data pointer erstellen.
char *data;
data = (char*) malloc(10000000);

So sollte das ungefaehr aussehen.

Code:
int total ()
{
  for( uint64_t i=0; i<N; ++i )
    tally += 1;
  printf( "Tally is %lld\n", tally );
  return 0;
}


int main ()
{
  tally = 0;
  int pid;
  char *child_stack;
  char *data;
  child_stack = (char *) malloc(N);
  data = (char*) malloc(10000000);
  pid = clone(total, child_stack, CLONE_VM | CLONE_THREAD | CLONE_SIGHAND | SIGCHLD , data);
  printf("PID %d\n", pid);
  printf( "Tally is %lld\n", tally );
  if(pid==0)
  {
    total();
  }
//  free(data);
  //free(child_stack);
  return 0;
}
 

Ähnliche Themen

Zurück
Oben