?? spooling.cpp
字號(hào):
/****************************************/
//模擬Spooling技術(shù)輸出文件
//davenathan bio.hit
//2006-11-15
/****************************************/
#include<iostream>
#include<cstdlib>
#define N 100
using namespace std;
//定義相關(guān)資源
struct PCB
{
int ID; //進(jìn)程標(biāo)識(shí)數(shù)
int* output; //輸出指針
int num; //所占用輸出井標(biāo)號(hào)
int first_output; //信息塊首地址
}out1,out2;
int output[10][N]; //輸出井
int output_finish[10]={0}; //輸出井滿或輸出到輸出井完成時(shí)的標(biāo)志
int number1=0,number2=0; //兩個(gè)進(jìn)程分別要輸出的文件數(shù)
int free_process[10]={0}; //空閑輸出請(qǐng)示鏈
int out_number=0; //實(shí)際輸出的文件個(gè)數(shù)
//初始化
void Init();
//Spooling進(jìn)程調(diào)度
void Spooling();
//進(jìn)程調(diào)度
void Schedule();
//輸出進(jìn)程
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兩個(gè)位置已經(jīng)使用
free_process[0]=1;
free_process[1]=1;
//初始化輸出井及輸出次數(shù)
for(int i=0; i<10; i++)
for(int j=0; j<N; j++)
output[i][j]=' ';
cout<<"Input the times of user1's output file(1-5)";
cin>>number1;
cout<<"Input the times of user1's output file(1-5)";
cin>>number2;
out_number=number1+number2;
}
void Schedule()
{
int rand_number;
// cout<<"請(qǐng)輸入0-99的任意數(shù),以決定將要運(yùn)行的進(jìn)程";
// cin>>rand_number;
rand_number=rand()%100;
if(rand_number<46)
{
cout<<"運(yùn)行輸出進(jìn)程1 "<<endl;
OutProcess1();
}
else if(rand_number<91)
{
cout<<"運(yùn)行輸出進(jìn)程2 "<<endl;
OutProcess2();
}
else
{
cout<<"運(yùn)行Spooling進(jìn)程 ";
Spooling();
}
}
void OutProcess1()
{
if(number1==0)
{
cout<<"進(jìn)程1所要輸出的內(nèi)容已全部輸出到輸出井中"<<endl;
return;
}
int outnumber;
// cout<<"請(qǐng)輸入要向輸出井中輸出的數(shù)字(0-9),0為文件結(jié)尾標(biāo)志";
// cin>>outnumber;
outnumber=rand()%10;
//防止輸出井溢出
if(out1.first_output==N-1)
outnumber=0; //強(qiáng)制結(jié)束
out1.output=output[out1.num];
*(out1.output+out1.first_output)=outnumber; //將結(jié)果輸出到輸出井中
out1.first_output++;
if(outnumber==0) //要輸出結(jié)束標(biāo)志
{
number1--; //已向輸出井輸出完成一個(gè)文件
output_finish[out1.num]=1; //標(biāo)記輸出井接受全部文件標(biāo)志
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<<"進(jìn)程2所要輸出的內(nèi)容已全部輸出到輸出井中"<<endl;
return;
}
int outnumber;
// cout<<"請(qǐng)輸入要向輸出井中輸出的數(shù)字(0-9),0為文件結(jié)尾標(biāo)志";
// cin>>outnumber;
outnumber=rand()%10;
//防止輸出井溢出
if(out1.first_output==N-1)
outnumber=0; //強(qiáng)制結(jié)束
out2.output=output[out2.num];
*(out2.output+out2.first_output)=outnumber; //將結(jié)果輸出到輸出井中
out2.first_output++;
if(outnumber==0) //要輸出結(jié)束標(biāo)志
{
number2--; //已向輸出井輸出完成一個(gè)文件
output_finish[out2.num]=1; //標(biāo)記輸出井接受全部文件標(biāo)志
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
{
//將已經(jīng)輸出完成的輸出井中信息輸出
for(int j=0; j<N; j++)
{
if(output[i][j]!=' ')
{
cout<<output[i][j];
output[i][j]=' ';
}
}
output_finish[i]=0;
free_process[i]=0;
out_number--;
cout<<endl;
if(out_number==0)
{
cout<<"兩個(gè)進(jìn)程所要輸出內(nèi)容全部完成"<<endl;
exit(0);
}
return;
}
}
if(kong==10)
{
cout<<endl<<"輸出井無(wú)完整輸出信息,無(wú)法向I/O設(shè)備輸出."<<endl;
return;
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -