[商管] 計概 fork
#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
→
02/08 19:24, , 1F
02/08 19:24, 1F
→
02/08 19:25, , 2F
02/08 19:25, 2F
→
11/23 22:09, , 3F
11/23 22:09, 3F