实验四 假脱机技术
一、目的和要求
1、目的
假脱机(SPOOLING)技术是广泛应用于各种计算机系统的一种行之有效的输入输出手段。这种技术使用比较简单的方法,缓和了高速处理机与低速输入输出设备速度不匹配的矛盾,提高了设备的利用率。为了更好地掌握这种技术。本实习要求学生独立地用高级语言编写一个SPOOLING程序来模拟假脱机输入输出过程。
2、要求
可将SPOOLING输入输出程序编制成一个独立的进程,与其他要求输入输出的进程并发工作。
SPOOLING进程负责从键盘等设备读如信息送到磁盘输入井中,或是把磁盘输出井中的信息块送到打印机或CRT等设备输出。其余进程只要求编写输入输出部分的程序,可不考虑其他操作。
二、示例
1.题目
本实验编制一个SPOOLING输出进程,与另外两个要求输出的进程并发执行。要求输出进程每运行一次只输出一项信息到输出井,待输出到一个结束标志时,表示一批信息输出完成,在输出井中形成一输出信息块,再由SPOOLING进程把整个信息块实际输出到打印机或CRT。因此,进程运行必须考虑同步问题。
采用进程的随机调度法模拟SPOOLING输出是合适的,因为各进程的输出应该是随机的。
2、算法与框图
(1)进程调度采用随机调度法,假设两个要求输出进程的调度概率各为45%,SPOOLING进程的调度概率为10%。
(2)可为进程设置三种工作状态:可运行状态、不可运行状态和结束状态。为了区分要求输出的进程和SPOOLING进程处于不可运行状态的不同原因,又把不可运行状态分称不可运行状态1和2,分别叙述如下:
1)进程执行完毕后应设置成“结束状态”。
2)要求输出进程在输出信息时,如果发现输出井已满,应设置成“不可运行状态1”。
3)SPOOLING进程在输出井空时应设置成“不可运行状态2”。
4)SPOOLING进程输出一个信息块后,应释放该信息块所占的输出井位置,并将正在等待输出的进程置成“可运行状态”。
5)要求输出进程把信息输出到输出井并形成信息块后,应将SPOOLING进程置成“可运行状态”。
(3)程序中使用的数据结构有:
1)进程控制块(PCB)
其中,进程状态为:
0 可运行状态
1 不可运行状态1(输出井满)
2 不可运行状态2(无请求块)
3 结束状态
输出指针指向输出井的第一个空位置。
2)输出请求块:每构成一个输出信息块(即遇到结束标志)时,应形成一输出请求块,待SPOOLING进程运行时控制输出,结构如下:
3)输出井:要求输出的进程在输出井中应有自己的输出区域,不应与其他的输出进程混淆。
(4)程序框图
1)假脱机输出系统如图1,该进程由函数init和scheduler实现。
2)请求输出进程如图2,该进程由函数userproc实现。
3)SPOOLING进程,该进程由函数SPOOLING实现。
(5)程序说明
1)请求输出程序简单地设计成每运行一次,随机输出一个0~9之间的数字,为了方便,把“0”作为输出结束标志。当输出为“0”时,就可形成一个信息块输出。
2)程序中有关变量说明:
freeioblknum:空闲输出请求块计数器。初值为10,即系统最多可设置10个输出信息块。
pcb.start:进程输出请求块链链首指针,初值为1。
pcb.point:空闲输出请求块链链首指针,初值为1。
freepoollen(2):两个输出井的计数器,初值为100,即每个井能存放100个信息项。
count(2):用来控制两个请求输出进程的输出文件数,避免系统无限制循环下去。
3)程序运行过程。
程序启动后,屏幕上显示“input the times of user1’s output file”和“input the times of user2’s output file”,要求用户先后打入两个输出进程所要求的输出文件数,控制程序的整个运行时间。随后,系统自行调度SPOOLING进程输出,并在打印机上打印两个进程的假脱机输出过程,待完成各自输出文件数后停止。
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<iostream>
using namespace std;
struct REQ{int ioblock_id;//要求输出进程标识数int block_len;//本次输出长度int block_start;//本次输出的首地址REQ* next;
};
REQ *head=NULL,*tail=NULL;
struct PCB{int id;//进程标识数int state;//进程状态REQ* point;//输出指针(指向上次断点)int start;//信息块首地址int length;//输出长度int buffer;//输出缓冲字(-1表示没有断点)};
PCB pcb[2];
FILE *f;
int freeioblock=10;//空闲输出请求块计数器
int freepoollen[2]={100,100};//两个输出井的计数器
int count[2];//用来控制两个请求输出进程的输出文件数,避免系统无限制循环下去
int buffer[2][100];
/*
0 可运行状态
1 不可运行状态1(输出井满)
2 不可运行状态2(无请求块)
3 结束状态
*/void init();
void scheduler();
void userproc(int i);
void spooling();int main()
{init();scheduler();cout<<endl;cout<<"buffer[0] "<<endl;for(int m=0;m<100;m++){if(m%10==0)cout<<endl;cout<<buffer[0][m];}cout<<endl;cout<<"buffer[1] "<<endl;for(int m=0;m<100;m++){if(m%10==0)cout<<endl;cout<<buffer[1][m];}return 0;
}void init()
{int i,n;f=fopen("out.txt","w");for(i=0;i<2;i++)for(n=0;n<100;n++)buffer[i][n]=0;for(i=0;i<2;i++){pcb[i].id=i;pcb[i].state=0;pcb[i].start=0;pcb[i].buffer=-1;}printf("input the times of user1's output file ");fprintf(f,"input the times of user1's output file ");scanf("%d",&count[0]);fprintf(f,"%d\n",count[0]);printf("input the times of user1's output file ");fprintf(f,"input the times of user1's output file ");scanf("%d",&count[1]);fprintf(f,"%d\n",count[1]);
}
void scheduler()
{int i;srand((unsigned)time(NULL));while(1){i=rand()%100;cout<<endl;cout<<"选择随机数i的值: "<<i<<endl;if(i<=45){if((pcb[0].state==0)&&count[0]>0)userproc(0);}else if(i<=90&&count[1]>0){if(pcb[1].state==0)userproc(1);}elsespooling();if(count[0]==0)pcb[0].state=3;if(count[1]==0)pcb[1].state=3;if(pcb[0].state==3&&pcb[1].state==3)break;}fclose(f);
}void userproc(int i)
{cout<<"in userproc !"<<endl;cout<<" process "<<i<<": "<<endl;int j;if(freeioblock==0||freepoollen[i]==0)//输出井满或无空闲请求块{if(freepoollen[i]==0)pcb[i].state=1;//输出井满return;}int length=0;if(pcb[i].buffer==-1)//上次输入完成,无断点{cout<<"in first"<<endl;count[i]--;//进程i的工作量-1freeioblock--;//空闲请求块-1REQ* temp=new REQ;temp->ioblock_id=i;temp->next=NULL;for(int m=0;m<100;m++){if(buffer[i][m]==0){//在buffer中找到第一个空闲temp->block_start=m;break;}}if(head==NULL&&tail==NULL){head=temp;tail=temp;}else{//新的请求块插到队尾tail->next=temp;tail=temp;}while(1){j=rand()%10;cout<<j;if(j==0){//结束temp->block_len=length;pcb[i].length+=length;pcb[i].buffer=-1;break;}if(buffer[i][temp->block_start+length+1]==0){//输出井中空余连续buffer[i][temp->block_start+length]=j;freepoollen[i]--;length++;}else{if(freepoollen[i]==0){//输出井满,退出pcb[i].state=1;//状态1 输出井满buffer[i][temp->block_start+length-1]=-1;temp->block_len=length;pcb[i].buffer=j;//记录未完成的输入pcb[i].length+=length;pcb[i].point=temp;//指向断点break;}else{buffer[i][temp->block_start+length]=-1;//表示还未到0,后面没位置,挪位后还可写,不是指输出井满length++;freepoollen[i]--;temp->block_len=length;pcb[i].buffer=j;//记录未完成的输入pcb[i].length+=length;pcb[i].point=temp;//指向断点break;}}}}else{//前面未完成输入,有断点cout<<"in second"<<endl;length=0;freeioblock--;//空闲请求块-1,但count[i]不用减,还是属于上一个工作REQ* temp=new REQ;temp->ioblock_id=i;temp->next=NULL;for(int m=0;m<100;m++){if(buffer[i][m]==0){//寻找输出井中第一个空闲位temp->block_start=m;break;}}REQ *point=pcb[i].point;//恢复断点,将这个新建的请求块插入上次的断点后面if(point->next==NULL){tail->next=temp;tail=temp;}else{//将这个新建的请求块插入上次的断点后面temp->next=point->next;point->next=temp;}while(1){j=rand()%10;cout<<j;if(j==0){//结束temp->block_len=length;pcb[i].length+=length;pcb[i].buffer=-1;break;}if(buffer[i][temp->block_start+length+1]==0){buffer[i][temp->block_start+length]=j;freepoollen[i]--;length++;}else{if(freepoollen[i]==0){//输出井满,退出pcb[i].state=1;//状态1 输出井满buffer[i][temp->block_start+length-1]=-1;pcb[i].buffer=j;//记录未完成的输入temp->block_len=length;pcb[i].length+=length;pcb[i].point=temp;//指向断点break;}else{buffer[i][temp->block_start+length]=-1;//表示还未到0,后面没位置,挪位后还可写,不是指输出井满freepoollen[i]--;length++;pcb[i].buffer=j;//记录未完成的输入temp->block_len=length;pcb[i].length+=length;pcb[i].point=temp;//指向断点break;}}}}cout<<endl;}void spooling()
{cout<<"in spooling !"<<endl;if(freeioblock==10)//无请求块,即请求块全为空闲return;REQ *run=head;REQ *temp=head;int temp_id=head->ioblock_id;int temp_state=pcb[temp_id].state;printf("%d:",run->ioblock_id);fprintf(f,"%d:",run->ioblock_id);int x=buffer[temp_id][run->block_start+run->block_len-1];int flag=0;do{flag=0;for(int i=0;i<temp->block_len;i++){if((buffer[temp_id][temp->block_start+i])!=-1)//没有断{printf("%d",buffer[temp_id][temp->block_start+i]);fprintf(f,"%d",buffer[temp_id][temp->block_start+i]);buffer[temp_id][temp->block_start+i]=0;freepoollen[temp_id]++;}else{//遇到断点flag=1;break;}}REQ* y=temp;temp=temp->next;head=head->next;//头指针向后移y->next=NULL;x=buffer[temp_id][temp->block_start+temp->block_len-1];free(y);//对应的请求块在输出井中的全部内容输入后释放freeioblock++;}while(x==-1);//以当前请求块末尾不为-1为结束条件if(pcb[temp_id].state==1)pcb[temp_id].state=0;}