我已经使用具有CLONE_VM和CLONE_FILES集的clone()系统调用编写了一个程序。 我无法理解输出显示分段错误的原因。有人可以纠正我的代码,并告诉我相同的原因。分段错误的原因
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sched.h>
#include<stdlib.h>
int variable, fd;
int do_something() {
// sleep(100);
variable = 42;
close(fd);
_exit(0);
}
int main(int argc, char *argv[]) {
void **child_stack;
char tempch;
variable = 9;
fd = open("test.file", O_RDONLY);
child_stack = (void **) malloc(16384);
printf("The variable was %d\n", variable);
clone(do_something, child_stack, CLONE_VM|CLONE_FILES, NULL);
// sleep(100);
printf("The variable is now %d\n", variable);
if (read(fd, &tempch, 1) < 1) {
perror("File Read Error");
exit(1);
}
printf("We could read from the file\n");
return 0;
}
你确认'malloc'不返回NULL吗? – littleadv 2011-03-10 04:15:18
我怀疑这是你的问题,但'do_something'应该带'void *'参数。我不相信它是必需的,但它仍然是很好的风格。 – 2011-03-10 06:16:46