1

I need some help with the clone() system call. I'm trying to use it with the flag CLONE_CHILD_CLEARTID but I cant see any change in the value of the field I specify as argument. Here it is a simple code:

int the_child(){
   exit(0);
}
int main(int argc, char * argv[])
{
    pid_t child_id = 99;
    printf("child %p\n",child_id);

    clone((int (*)(void *))the_child,
          NULL,
          CLONE_VM | CLONE_CHILD_CLEARTID | SIGCHLD,
          NULL, NULL,NULL, child_id);

    sleep(1);
    printf("child %p\n",child_id);
 }

However when the two printf display always 99, What am I doing wrong?

Federico
  • 31
  • 5

2 Answers2

1

You are passing child_id through value. You should pass it as a pointer.

This is where the clearing of tid takes place inside the kernel

/*
 * We don't check the error code - if userspace has
 * not set up a proper pointer then tough luck.
 */
put_user(0, tsk->clear_child_tid); 

Can you see the comment warning of the same ? :)

See this blog where they are tracking the arguments to clone system call through strace

EDIT : Adding from our discussion in comments, child_stack can be zero for sys_clone system call but not for the library function clone()

Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
  • I edited the code and used `&child_id` in the `clone` but nothing changed :( – Federico Mar 24 '12 at 14:09
  • My bad. Seams that I can not `NULL` value for the child stack. – Federico Mar 24 '12 at 14:33
  • @Federico Actually for the `sys_clone` system call, on top of which is the library function `clone` is implemented, `child_stack` can be zero. May be not when you are using the library function. After you supplied the stack appropriately for the child, what happened to the `tid` clearing functionality? – Pavan Manjunath Mar 24 '12 at 15:49
0

Shouldn't you allocate memory (malloc()) before creating clone?

arrowd
  • 33,231
  • 8
  • 79
  • 110
Kimboo Rasta
  • 1
  • 1
  • 1