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

📄 fork.c

📁 unix下用fork()创建process的例子
💻 C
字号:
// fork.c : Demo program for process creation in UNIX


#include "fork.h"

static int pid = 0;
int main(void)
{
	int i,in;
	int pidpar;
	pidpar=getpid();
	printf("\nI'm the progenitor 5%xh\n\n",pidpar);
	for (i=0;i<3;i++)
	{

		printf("\n####### %d.step in loop [0:2] ###### in process 5%xh ######\n",i, getpid());
		fflush(stdout);		// to avoid side effects
		in = fork();		// create new process: parent and child have the same program!!
		if(in < 0)	//fork failed
			{
				printf("\nfailure at fork process in step02%i\n",i);
				in = getchar();
				exit(1);
			}
		if(in)		//<in> = pid of child process

			{ // parent process
				printf("In the parent process 5%xh\twas created the child process 5%xh\n", getpid(),in);
				fflush(stdout);
			}
			else
			{ // child process
				
				printf("\t\t I'm the child process 5%xh\n", getpid());
				fflush(stdout);
			}
	} ///end for loop

	if(pidpar==getpid())sleep(5*SLEEP_TIME);
	sleep(SLEEP_TIME);
	printf("\n-------------- process 5%xh terminated---------------------------------------\n", getpid());
	fflush(stdout);
	exit(0);
	} ///end main

⌨️ 快捷键说明

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