2012-03-24 239 views
0

我需要克隆()系統調用一些幫助。 我想使用它與標誌CLONE_CHILD_CLEARTID,但我不能看到我指定爲參數的字段的值的任何更改。 這是一個簡單的代碼:克隆和CLONE_CHILD_CLEARTID標誌

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); 
} 

然而,當兩個printf的顯示總是99,什麼是我做錯了什麼?

回答

0

您正在通過價值child_id。你應該將它作爲指針傳遞。

This is wheretid清算需要內核

/* 
* 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); 

你能看到相同的評論警告裏面的地方嗎? :)

See this blog他們通過strace

編輯跟蹤參數clone系統調用:從我們的意見的討論添加,child_stack可以是零爲sys_clone系統調用而不是庫函數clone()

+0

我編輯了代碼並在'clone'中使用了'&child_id',但沒有任何變化:( – Federico 2012-03-24 14:09:30

+0

我的不好。接縫,我不能'NULL'值爲子堆棧 – Federico 2012-03-24 14:33:13

+0

@Federico其實對於'sys_clone'系統調用,其中最重要的是實現了庫函數'clone','child_stack'可以爲零。當你使用庫函數時,可能不會。當你爲孩子提供適當的棧後,'tid清除功能? – 2012-03-24 15:49:18

0

在創建克隆之前不應該分配內存(malloc())嗎?