c语言 进程控制---创建进程 vfork()函数

#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
int gvar=2;
int main()
{
	pid_t pid;
	int var = 5;
	printf("process id:%ld\n",(long)getpid());
	printf("gvar=%d var=%d\n",gvar,var);
	pid=vfork();
	if (pid<0)
	{
		perror("error");
		return 1;
	}
	else if(pid==0)
	{
		gvar--;
		var++;
		printf("the child  process id:%ld gvar=%d var=%d\n",(long)getpid(),gvar,var);
		_exit(0);
	}
	else
	{
		printf("the parent process id:%ld gvar=%d var=%d\n",(long)getpid(),gvar,var);
		return 0;
	}
}

你可能感兴趣的:(C)