今天多云转晴吖 2015-10-11 06:26 采纳率: 0%
浏览 1385

linux vfork 进程的结果出现奇怪的问题

 #include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int globVar = 5;

int main (void)
{
    pid_t pid;
    int var = 1, i;

    printf("fork is different with vfork\n ");
//  pid = fork();
    pid = vfork();
    switch(pid){
        case 0:
            i = 3;
            while(i-- > 0)
            {
                printf("child process is running!\n");
                globVar++;
                var++;
            }
            printf("%d, %d\n",globVar,var);
        break;
        case 1:
            perror("process creation failed!\n");

        exit(0);
        default:
            i = 5;
            while(i-- > 0)
            {
                printf("parent process is running\n");
                globVar++;
                var++;
                sleep(1);
            }
            printf("%d, %d\n",globVar,var);
            exit(0);
    }   

}

结果

[root@localhost labtwo]# ./diffork 
fork is different with vfork
 child process is running!
child process is running!
child process is running!
8,      4
parent process is running
parent process is running
parent process is running
parent process is running
parent process is running
13,     11345274

最后的父进程var的值不应该是9吗怎么会是11345274 谁能帮忙解一下疑惑?谢谢了

  • 写回答

2条回答 默认 最新

  • 今天多云转晴吖 2015-10-11 06:46
    关注
        T.T你们都不理我 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#DMA固件#的问题,请各位专家解答!
  • ¥15 matlab生成的x1图不趋于稳定,之后的图像是稳定的水平线
  • ¥15 请问华为OD岗位的内部职业发展通道都有哪些,以及各个级别晋升的要求
  • ¥20 微信小程序 canvas 问题
  • ¥15 系统 24h2 专业工作站版,浏览文件夹的图库,视频,图片之类的怎样删除?
  • ¥15 怎么把512还原为520格式
  • ¥15 MATLAB的动态模态分解出现错误,以CFX非定常模拟结果为快照
  • ¥15 求高通平台Softsim调试经验
  • ¥15 canal如何实现将mysql多张表(月表)采集入库到目标表中(一张表)?
  • ¥15 wpf ScrollViewer实现冻结左侧宽度w范围内的视图