我必须为我的课堂作业编写一个线程示例。通过 copy() 创建子进程后,我不知何故遇到了 Segfault (不得不遗憾地使用它)。
void thread(void);
#define CLONE_VM 0x00000100
#define ANSWER_TRUE "t"
#define ANSWER_FALSE "f"
static char stack[2];
int main(void) {
void** childThreadBP = NULL;
void** childThread = NULL;
int pid = 0;
puts("create stack for child process...");
void ** new_stack = (void **) malloc(128);
//new_stack = (void **) ((char *)new_stack + 128);
puts("create child process...");
pid = clone(thread, new_stack, CLONE_VM, NULL);
puts("write PID to shared stack...");
stack[0] = (char) pid;
puts("child answered:");
while(1){}
if (stack[1] == ANSWER_TRUE) {
puts("PIDs are equal.");
}
else {
puts("PIDs are NOT equal.");
}
return EXIT_SUCCESS;
}
void thread(void) {
puts("[child]: I'm alive!");
int pidSelf;
pidSelf = getpid();
if (pidSelf == (int)stack[0]) {
puts("[child]: dad the PID you gave me is correct!");
stack[1] = ANSWER_TRUE;
}
else {
puts("[child]: dad the PID you gave me is NOT correct!");
stack[1] = ANSWER_FALSE;
}
}
也许你明白我的错误是什么...... - 代码格式有什么问题?!
我只需要帮助修复段错误 - 其余的应该没问题(我认为;))
问候!!