作者KuU (/|\ YU)
看板Grad-ProbAsk
標題[商管] 計概 fork
時間Wed Feb 8 18:19:01 2012
#include<pthread.h>
#include<stdio.h>
int value = 5;
void *runner(void *param);
int main(int argc,char *argv[])
{
int pid;
pthread_t tid;
pthread_attr_t attr;
value--;
pid=fork();
value--;
if(pid==0){
value--;
pthread_attr_init(&attr);
pthread_create(&tid,&attr,runner,NULL);
pthread_join(tid,NULL);
printf("%d",value);
value--;
}
else if(pid>0){
value--;
wait(NULL);
printf("%d",value);
value--;
}
}
void *runner(void *param)
{
value--;
printf("%d",value);
value--;
pthread_exit(0);
}
這題正確答案是該怎麼執行呢?
我自己算是1 0 2 但答案是 -1 -2 -3
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.112.116.9
1F:→ kyodaisuki:我也是認為1 0 2 ~ 02/08 19:24
2F:→ kyodaisuki:最先輸出的是下面的runner 再來子行程的 再來老爸的 02/08 19:25
3F:→ TokiyoHot:-1 -2 0 11/23 22:09