fork and vfork

微信公众号 微信公众号 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>

int global=5;

int main(void)
{
pid_t pid;
int var=1,i;
printf("The difference between fork and vfork:\n");
pid=vfork();
switch(pid)
{
case 0:
i=3;
while(i-->0)
{
printf("Child process is running\n");
global++;
var++;
}
printf("Child End:global=%d,var=%d\n",global,var);
break;
case -1:
perror("fork error:");
exit(1);
default:
i=5;
printf("%10d\n",var);
while(i-->0)
{
printf("Parent process is running\n");
global++;
var++;
}
printf("Parent End:global=%d,var=%d\n",global,var);
exit(0);
}
}


The program running result as follows:

The difference between fork and vfork:
Child process is running
Child process is running
Child process is running
Child End:global=8,var=4
4359003
Parent process is running
Parent process is running
Parent process is running
Parent process is running
Parent process is running
Parent End:global=13,var=4359008


My question is: The value of 'var' is 4359003, why not 4 ?



...全文

相关推荐

评论 8
收藏 已收藏
分享
返回顶部
微信公众号
C语言 70,039 成员 243,237 内容