www.pudn.com > OS_Spooling_Simulate.rar > Spooling.cpp


/****************************************/ 
//模拟Spooling技术输出文件 
//davenathan bio.hit 
//2006-11-15 
/****************************************/ 
 
#include 
#include 
#define N 100 
using namespace std; 
 
//定义相关资源 
struct PCB 
{ 
	int ID;					//进程标识数 
	int* output;			//输出指针 
	int num;				//所占用输出井标号 
	int first_output;		//信息块首地址 
}out1,out2; 
 
int  output[10][N];					//输出井 
int output_finish[10]={0};			//输出井满或输出到输出井完成时的标志 
int  number1=0,number2=0;			//两个进程分别要输出的文件数 
int  free_process[10]={0};			//空闲输出请示链 
int  out_number=0;					//实际输出的文件个数 
 
//初始化 
void Init(); 
//Spooling进程调度 
void Spooling(); 
//进程调度 
void Schedule(); 
//输出进程 
void OutProcess1(); 
void OutProcess2(); 
 
void main(void) 
{ 
	Init(); 
	while(1) 
	{ 
		Schedule(); 
	} 
} 
 
void Init() 
{ 
	//初始化PCB 
	out1.ID=1; 
	out1.num=0; 
	out1.output=output[out1.num]; 
	out1.first_output=0; 
 
	out2.ID=2; 
	out2.num=1; 
	out2.output=output[out2.num]; 
	out2.first_output=0; 
	 
	//输出井中0和1两个位置已经使用 
	free_process[0]=1; 
	free_process[1]=1; 
 
	//初始化输出井及输出次数 
	for(int i=0; i<10; i++) 
		for(int j=0; j>number1; 
	cout<<"Input the times of user1's output file(1-5)"; 
	cin>>number2; 
	out_number=number1+number2; 
} 
 
void Schedule() 
{ 
	int rand_number; 
 
//	cout<<"请输入0-99的任意数,以决定将要运行的进程"; 
//	cin>>rand_number; 
	rand_number=rand()%100; 
 
	if(rand_number<46) 
	{ 
		cout<<"运行输出进程1  "<>outnumber;	 
	outnumber=rand()%10; 
 
	//防止输出井溢出 
	if(out1.first_output==N-1) 
		outnumber=0;	//强制结束 
	out1.output=output[out1.num]; 
	*(out1.output+out1.first_output)=outnumber;	//将结果输出到输出井中 
	out1.first_output++; 
	 
	if(outnumber==0)	//要输出结束标志 
	{ 
		number1--;				//已向输出井输出完成一个文件 
		output_finish[out1.num]=1;	//标记输出井接受全部文件标志 
 
		if(number1!=0)			//还有文件要输出 
			for(int i=0; i<10; i++)		//寻找空闲的输出井 
				if(free_process[i]==0) 
				{ 
					out1.num=i; 
					free_process[i]=1; 
					break; 
				} 
	} 
} 
 
void OutProcess2() 
{ 
	if(number2==0) 
	{ 
		cout<<"进程2所要输出的内容已全部输出到输出井中"<>outnumber;	 
	outnumber=rand()%10; 
 
	//防止输出井溢出 
	if(out1.first_output==N-1) 
		outnumber=0;	//强制结束 
	out2.output=output[out2.num]; 
	*(out2.output+out2.first_output)=outnumber;	//将结果输出到输出井中 
	out2.first_output++; 
	 
	if(outnumber==0)	//要输出结束标志 
	{ 
		number2--;				//已向输出井输出完成一个文件 
		output_finish[out2.num]=1;	//标记输出井接受全部文件标志 
 
		if(number2!=0)			//还有文件要输出 
			for(int i=0; i<10; i++)		//寻找空闲的输出井 
				if(free_process[i]==0) 
				{ 
					out2.num=i; 
					free_process[i]=1; 
					break; 
				} 
	} 
} 
 
void Spooling() 
{ 
	int kong=0; 
 
	for(int i=0; i<10; i++) 
	{ 
		if(output_finish[i]==0) 
			kong++; 
		else 
		{ 
			//将已经输出完成的输出井中信息输出 
			for(int j=0; j