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

📄 spooling.cpp

📁 SPOOLING假脱机输入输出技术广泛应用于各种计算机的I/O。该技术通过采用预输入和缓输出的方法
💻 CPP
字号:
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
#include<stdio.h>

struct pcb{
	int status;
	int length;
}*PCB[3];
struct req{
	int reqname;
	int length;
	int addr;
}reqblock[10];
int buffer[2][100];
int head=0,tail=0;
int t1=5,t2=5;

void request(int i);
void spooling();

void main()
{
	for(int l=0;l<2;l++)
		for(int j=0;j<100;j++)
			buffer[l][j]=0;
	for(int n=0;n<3;n++)
	{
		struct pcb*tmpPcb=(struct pcb*)malloc(sizeof(struct pcb));
		tmpPcb->status=0;
		tmpPcb->length=0;
		PCB[n]=tmpPcb;
	}
	cout<<"两个用户进程的请求分别为5,5"<<endl;
	srand((unsigned)time(NULL));
	while(1)
	{
		int k;
		k=rand()%100;
		if(k<=45)
		{
			if((0==PCB[0]->status)&&(t1>0))
				request(1);
		}
		else if((k<=90)&&(t2>0))
		{
			if(0==PCB[1]->status)
				request(2);
		}
		else
			spooling();
		if((0==t1)&&(0==t2)&&(head==tail))
			break;
	}
	for(int m=0;m<3;m++)
	{
		free(PCB[m]);
		PCB[m]=NULL;
	}
	getchar();
}

void request(int i)
{
	int j,length=0;
	struct req*run;
	if(1==i)
		t1--;
	else
		t2--;
	cout<<"用户"<<i<<"请求数据:\n";
	run=&reqblock[tail%10];
	run->reqname=i;
	run->length=0;
	if(0==tail)
		run->addr=0;
	else
	{
		int index=(tail-1)%10;
		run->addr=reqblock[index].addr+reqblock[index].length;
	}
	for(int m=0;m<100;m++)
	{
		if(0==buffer[i-1][m])
		{
			run->addr=m;
			break;
		}
	}
	int s=0;
	while(1)
	{
		j=rand()%10;
		if(0==j)
		{
			run->length=length;
			break;
		}
		buffer[i-1][(run->addr+length)]=s;
		cout<<s<<" ";
		s++;
		length++;
	}
	cout<<endl;
	PCB[i-1]->length+=length;
	length=0;
	if(2==PCB[2]->status)
		PCB[2]->status=0;
	tail++;
}
void spooling()
{
	struct req*run;
	cout<<"调用SPOOLING输出服务程序输出数据:"<<endl;
	run=&reqblock[head%10];
	cout<<run->reqname<<":\n";
	for(int i=0;i<run->length;i++)
		cout<<buffer[run->reqname-1][run->addr+i]<<" ";
	cout<<endl;
	head++;
	for(int j=0;j<2;j++)
	{
		if(1==PCB[j]->status)
			PCB[j]->status=0;
	}
}

⌨️ 快捷键说明

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