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

📄 spooling.cpp

📁 操作系统课程设计源码 SPOOLING系统模拟
💻 CPP
字号:
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
#include<stdio.h>

struct pcb{                /*进程控制块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;                  /*进程请求t1 t2*/

void request(int i);          /*进程请求*/
void spooling();              /*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));  //设置RAND的种子为系统时间
	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 + -