⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fork_vfork.c

📁 主要测试fork和vfork区别的代码 比较简单
💻 C
字号:
/*fork.c*/
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int	global=22;
char	buf[]="the test content!\n";

int main(void)
{	int test=66;
	pid_t pid;
	if(write(STDOUT_FILENO,buf,sizeof(buf)-1)!=sizeof(buf)-1)
		perror("write error!");
	printf(" fork test!\n");
/* fork */
	pid = fork();   /*we should check the error*/
//	pid =v fork();   /*we should check the error*/
	if(pid == -1){
		perror("fork");
		exit;
	}
/*if the pid=0 then it is the child*/
	else if(pid == 0){
	printf("global=%d test%d Child,my PID is %d\n",global,test,getpid());
	global++;
	test++;
//	exit(0);  //如果没有显式退出 会发生什么?
}
/*else be the parent*/
	//else
	//{sleep(2);}
	sleep(2);
	printf("global=%d test%d Parent,my PID is %d\n",global,test,getpid());
	exit(0);

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -