看板 Grad-ProbAsk 關於我們 聯絡資訊
#include<pthread.h> #include<stdio.h> int value = 0; void *runner(void *param); /*the thread*/ int main(int argc, char *argv[]) { int pid; pthread_t tid; pthread_attr_t attr; pid=fork(); if(pid == 0) /*child process*/ { pthread_attr_init(&attr); pthread_creat(&tid,&attr,runner,NULL); pthread_join(tid,NULL); printf("CHILD value = %d",value); /*LINE C*/ } else if(pid > 0) { wait(NULL); printf("PARENT : value = %d",value); /*LINE P*/ } } void *runner(void *param) { value = 5; pthread_exit(0); } 求解P行跟C行的value值 答案 : P=0 , C=5 為什麼?? 能否告訴我程式大概的執行流程 感謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.127.208.156 ※ 編輯: yesa315 來自: 140.127.208.156 (08/18 19:36)
icrts:fork的child process和parent process共享code block 08/18 21:04
icrts:但data block是分開的 08/18 21:05