操作系统实验(七):c实现SPOOLING假脱机技术

1、【实验目的】
理解和掌握SPOOLING假脱机技术
2、【实验内容】
通过SPOOLING技术可将一台物理I/O设备虚拟为多台逻辑I/O设备,同样允许多个用户共享一台物理I/O设备,从而使其成为虚拟设备。该技术广泛应用与各种计算机的I/O,通过采用预输入和缓输出的方法,使用共享设备的一部分空间来模拟独占设备,以提高独占设备的利用率。
3、【实验步骤】
数据结构

struct pcb//定义结构体
{
int status;//进程状态
int length; //输出长度
}*PCB[3];

struct reqname//请求进程名
{
int reqname;//定义名称
int length;//输出长度
int addr;//本次输出的首地址
}reqblock[10];

void request(int i)//定义请求函数
void spooling()//spooling进程

1.3实验代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct pcb//定义结构体 
{
 int status;//进程状态 
 int length; //输出长度 
}*PCB[3];
struct reqName//请求进程名 
{
 int reqName;//定义名称 
 int length;//输出长度 
 int addr;//本次输出的首地址 
}reqBlock[10];
int buffer[2][100];
int head = 0;
int tail = 0;
int t1 = 0;
int t2 = 0;
void request(int i);//i=1表示用户进程1;i=2表示用户进程2
void spooling();

void request(int i)//定义请求函数 
{
 int s = 0;
 int j = 0;
 int m = 0;
 int length = 0;
 struct reqName*run;
 if(1 == i)
  t1--;
 else
  t2--;
 printf("******************************************\n");
 printf("用户%d请求数据:\n",i);//输出标注 
 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;
  }
 }
  s = 0;
 while(1)
 {
  j = rand() % 10;
 
  if(0 == j)
  {
   run->length = length;
   break;
  }
  buffer[i-1][(run->addr+length)] = s;
  printf("%3d",s);
  s++;
  length++;
 }
 PCB[i-1]->length += length;
 printf("\n此时PCB[%d]中length的值为:%d\n",i-1,PCB[i-1]->length); 
 printf("******************************************\n");
 length = 0;
 if(2 == PCB[2]->status)
  PCB[2]->status = 0;
 tail++; 
}

void spooling()
{
 int i = 0;
 int j = 0;
 struct reqName*run;
 printf("***********************************************\n");
 printf("调用SPOOLING输出服务程序输出数据:\n");
 run = &reqBlock[head % 10];
 printf("用户%d请求的数据为\n",run->reqName);
 for(int i = 0; i < run->length; i++)
  printf("%3d",buffer[run->reqName - 1][run->addr + i]);
 printf("\n");
 head++;
 for(int j = 0; j < 2; j++)
 {
  if(1 == PCB[j]->status)//若没有可用请块时,调用进程进入"等待状态3
  PCB[j]->status = 0; 
 }
}
//主函数 
int 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;
 }
 printf("用户1的请求次数为:");
    scanf("%d",&t1);
    printf("用户2的请求次数为:");
 scanf("%d",&t2);
 printf("\n");
 //printf("两个用户进程的请求分别为5,5\n");
 srand((unsigned)time(NULL));
  while(1)
  {
   int k;
   k = rand() % 100;//用随机数模拟进程执行的概率 
   printf("当前k的值为%d\n",k);
   //printf("****************************\n");
   if(k <= 45 && (t1 > 0))//执行请求输出用户进程1 
   {
    if(0 == PCB[0]->status) 
     request(1);
    
   }
   else if((k <= 90) && (t2 > 0))//执行请求输出用户进程2 
   {
    if(0 == PCB[1]->status)
     request(2);
   }
   else
    spooling();//执行SPOOLING进程 
   if((0 == t1) && (0 == t2) && (head == tail))
    break;
  }
 for(int m = 0; m < 3; m++)
 {
  free(PCB[m]);
  PCB[m] = NULL;//PCB值为空 
 }
 getchar();
}
  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值