Linux系统编程——进程中vfork函数

函数原型

pid_t vfork(void);//pid_t是无符号整型

所需头文件

#include 
#include 

功能

vfork() 函数和 fork() 函数一样都是在已有的进程中创建一个新的进程,但它们创建的子进程是有区别的。

返回值

成功 子进程中返回 0,父进程中返回子进程 ID
失败 返回 -1

vfork与fork的区别

关键区别一:

fork执行时无先后顺序,父进程与子进程会争夺执行 

vfork保证子进程先运行,当子进程调用exit退出后,父进程才执行

代码验证

#include 
#include 
#include 

int main()
{
	int fork_t = 0;

	fork_t = fork();

	if(fork_t > 0)
	{
		while(1)		
		{
			printf("This is father\n");
			sleep(1);
		}
	}
	else if(fork_t == 0)
	{
		while(1)
		{
			printf("This is child\n");
			sleep(1);
		}
				
	}
	return 0;
}

Linux系统编程——进程中vfork函数_第1张图片

#include 
#include 
#include 
#include 

int main()
{
	int vfork_t = 0;
	int count = 0;

	vfork_t = vfork();

	if(vfork_t > 0)
	{
		while(1)		
		{
			printf("This is father\n");
			sleep(1);
		}
	}
	else if(vfork_t == 0)
	{
		while(1)
		{
			printf("This is child\n");
			sleep(1);
			count++;
			if(count >= 3)
			{
				exit(-1);//输出三次子进程,之后退出
			}
		}
				
	}
	return 0;
}

Linux系统编程——进程中vfork函数_第2张图片

第一部分代码可见fork函数中的父进程和子进程会争夺输出,而第二部分的vfork函数会在子进程输出3次退出之后再执行父进程。


关键区别二:

fork中子进程会拷贝父进程的所有数据,子进程是父进程的地址空间

vfork中子进程共享父进程的地址空间

代码验证

#include 
#include 
#include 

int main()
{
	int fork_t = 0;
	int a = 10;

	fork_t = fork();
	
	if(fork_t != 0)
	{
		printf("This is father,a = %d\n",a);
	}
	else
	{
		printf("This is child,a = %d\n",a);
	}
	return 0;
}

#include 
#include 
#include 
#include 

int main()
{
	int vfork_t = 0;
	int count = 0;

	vfork_t = vfork();

	if(vfork_t > 0)
	{
		while(1)		
		{	
			printf("count = %d\n",count);
			printf("This is father\n");
			sleep(1);
		}
	}
	else if(vfork_t == 0)
	{
		while(1)
		{
			printf("This is child\n");
			sleep(1);
			count++;
			if(count >= 3)
			{
				exit(0);
			}
		}
				
	}
	return 0;
}

Linux系统编程——进程中vfork函数_第3张图片

第一部分代码可知,在父进程中定义a,调用fork函数时,父进程与子进程打印a的值一样,说明子进程会拷贝父进程的所有数据(父进程的只打印自己的值,不会收子进程影响);第二部分代码可知,在子进程结束之后,才会执行父进程,且子进程中数值发生改变,在父进程调用时会发生改变(一开始父进程a=0,调用后a=3),会受到子进程影响

你可能感兴趣的:(linux,运维,服务器)