0
は、私は次のコードを持っている:Cのクローン関数はCLONE_NEWPIDで実行されていません。 SIGCHLDフラグ
#define _GNU_SOURCE
#include<sched.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>
#include<errno.h>
#include<string.h>
static char child_stack[2048];
int x = 10;
static int child_fn() {
printf("Pid: %ld\n", (long) getpid());
return 0;
}
int main() {
printf("before= %d\n", x);
errno = 0;
pid_t child_pid = clone(&child_fn, (void *) child_stack+1024, CLONE_NEWPID | SIGCHLD, NULL);
if(child_pid == -1) {
printf("%s\n", strerror(errno));
return 0;
} else {
printf("clone()= %ld\n", (long) child_pid);
printf("after= %d\n", x);
while(waitpid(-1, NULL, 0) < 0 && errno == EINTR) {
printf("waiting\n");
continue;
}
return 0;
}
}
を私が手出力は次のようになります。
before= 10
clone()= 16
after= 10
これはchild_fn
はchild_pid
あるPIDが、割り当てられますなかったことを意味します。しかしprintf
の中のchild_fn
は実行されなかったか、別のstdoutスコープで印刷されている可能性がありますか?いずれにせよ、これの理由とそれをデバッグする方法。私はちょうど名前空間から始まっているので、それらについてはあまり知らない。