linux c 使用vfork时产生的疑问

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int globvar=5;

int main(){
  pid_t pid;

 int var=1, i;
  printf("fork is diff with vfork\n");
 // pid=fork();
  pid=vfork();
  switch(pid){
  case 0:
    i=3;    
    while(i-->0){
     printf("child process is running\n");
     globvar++;
     var++;
     sleep(1);
    }
    printf("child‘s globvar=%d,var=%d\n",globvar,var);
   break;
  
  default:
    i=5;
    while(i-->0){
     printf("parent process is running\n");
     globvar++;
     var++;
     sleep(1);
    }
    printf("parent‘s globvar=%d,var=%d\n",globvar,var);


    exit(0);
  case -1:
    printf("process creation failed\n");
    exit(0);



}
 return 0;
}

运行结果为:

fork is diff with vfork
child process is running
child process is running
child process is running
child‘s globvar=8,var=4
parent process is running
parent process is running
parent process is running
parent process is running
parent process is running
parent‘s globvar=13,var=-1216646167


问题来了,为什么var值不是9????














郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。