fork and vfork
#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 ?
...全文
相关推荐
Unix编程常见问题解答 高清PDF含书签目录
**1.1.2 What's the difference between fork() and vfork()? fork函数与 vfork函数的区别在哪里?
详解linux中fork、vfork、clone函数的区别
**fork()**`fork()` 是最传统的进程创建方法。它创建一个与父进程完全独立的新进程,即所谓的子进程。
fork()函数与vfork()函数的区别
这个程序是为了分析fork()函数和vfork()函数的区别,使用的系统环境是linux
浅谈Linux vfork与fork简单对比分析
"Linux vfork与fork的简单对比分析"在Linux操作系统中,创建新进程的主要方式有两种:`fork`和`vfork`。两者都用于创建一个与父进程相似的新进程,但它们在实现机制和使用场
深入解读Linux进程函数fork(),vfork(),execX()
**1. fork() 函数**`fork()`是Linux中最常见的创建子进程的函数。


