0
如果我有以下代碼(來自Silberschatz,Operating Systems),爲什麼P行中的value = 0?我認爲父母的過程等待,直到孩子完成,孩子設置值= 5.fork()和wait()/由父母/子女更改的值
有人可以解釋這一點給我嗎?
int value = 0;
void *runner(void *param);
int main(int argc, char *argv[])
{
int pid;
pthread_t tid;
pthread_attr_t attr;
pid = fork();
if (pid == 0) {
pthread_attr_init(&attr);
pthread_create(&tid,&attr,runner,NULL);
pthread_join(tid,NULL);
printf("CHILD: value= %d \n",value); /* LINE C */
}
else if (pid > 0) {
wait(NULL);
printf("PARENT: value= %d \n",value); /* LINE P */
}
}
void *runner(void *param) {
value = 5;
pthread_exit (0);
}
我明白了。非常感謝你! – soet