📄 spooling.c
字号:
/*SPOOLING 模拟实验*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct pcb
{
int id;/*进程标识*/
int status; /*进程状态*/
int firstaddr; /*信息块首地址*/
int length; /*输出长度*/
int outbufword; /*输出缓冲字*/
}*PCB[3];
/*进程状态status的可能取值:
*0:可执行
*1:输出井满等待
*2:输出井空等待
*3:结束
*/
FILE *f;
struct req/*输出请求块*/
{
int reqname; /*请求进程名*/
int length; /*本次输出长度*/
int addr; /*地址*/
}reqblock[10];
int buffer[2][100],C3=10; /* buffer为输出井*/
int l1=1, l2=1;
int head= 0, tail= 0; /*输出井的头尾指针*/
int t1, t2;
void request(int i) /*i为请求输出的进程标识“1或2”*/
{
int j, length= 0,m;
struct req *run;
/*
if( (tail-head) == 10)
{
PCB[i-1].status = 1;
return;
}
*/
if(i == 1)
{
t1--;
}
else
{
t2--;
}
run = &reqblock[tail%10];
run->reqname = i;
run->length = 0;
if (tail == 0)
{
run->addr= 0;
}
else
{
int index = (tail-1)%10;
run->addr = reqblock[index].addr + reqblock[index].length;
}
for(m=0; m < 100; m++)
{
if(buffer[i-1][m] == 0)
{
run->addr=m;
break;
}
}
while(1)
{
j = rand()%10;
if (j == 0)
{
run->length=length;
break;
}
buffer[i-1][ ( run->addr + length ) ] = j;
length++;
}
PCB[i-1]->length += length;
length = 0;
if(PCB[2]->status == 2)
{
PCB[2]->status=0;
}
tail++;
}
void spooling()
{
int i,j;
struct req * run;
run = &reqblock[head%10];
printf("%d ",run->reqname);
fprintf(f,"%d ",run->reqname);
for(i = 0; i < run->length; i++)
{
printf("%d ",buffer[run->reqname-1][run->addr+i]);
fprintf(f,"%d ",buffer[run->reqname-1][run->addr+i]);
}
printf("\n");
fprintf(f,"\n");
head++;
for( j = 0; j < 2; j++)
{
if(PCB[j]->status == 1)
{
PCB[j]->status=0;
}
}
}
int main()
{
int i, j, n;
f=fopen("result.txt","w");
for (i = 0; i < 2; i++)
for (n = 0; n < 100; n++)
buffer[i][n] = 0;
for( i = 0; i < 3; i++)
{
struct pcb *tmpPcb = (struct pcb*)malloc(sizeof(struct pcb));
tmpPcb->id= i;
tmpPcb->status= 0;
tmpPcb->firstaddr= 0;
tmpPcb->length= 0;
tmpPcb->outbufword= 1;
PCB[i] = tmpPcb;
}
printf("How many work do p1 want to do?");
fprintf(f,"How many work do p1 want to do?");
scanf("%d",&t1);
fprintf(f,"%d\n",t1);
printf("How many work do p2 want to do?");
fprintf(f,"How many work do p2 want to do?");
scanf("%d",&t2);
fprintf(f,"%d\n",t2);
srand((unsigned)time(NULL));
while(1)
{
i=rand()%100;
if(i <= 45)
{
if( (PCB[0]->status == 0) && (t1 > 0) )
{
request(1);
}
}
//else
else if( (i <= 90) && (t2>0) )
{
if(PCB[1]->status == 0 )
{
request(2);
}
}
else
{
spooling();
}
if( (t1 == 0) &&(t2 == 0)&& (head==tail) )
break;
}
for( i = 0; i < 3; i++)
{
free(PCB[i]);
PCB[i] = NULL;
}
fclose(f);
return 0;
}
/*
运行程序的结果如下:
How many work do P1 want to do?9
How many work do P2 want to do?9
2 3 3 8 9 3
2 8 9 3
2 7
2 8 6 5 5
2 9 3 7
1 3 8 6 8 8 6 1 4 9 8 1 9 9
2
2 5 8
2 1 8 3 5 5 6 5 1 2
2 1 9 7 7 6 4 1 3 9 8 9 4 6 3 3 7 5 6 9 5 7 2
1 7
1 7
1 1 6 7 6 4 7 3 8 9 2 7 2 6
1 1 7 2 4 3 9 6
1 7 2 8 4 4 7 6 5 8 3 8 6 9 8 5 7 5 8 6 3 4 4 4 6 1
1 8 6 2 2 1 8 7 1
1 1 4 4 6 1 6 7 3 1 1 8 5 5 5 6 6 9 8
1 3 2 6 4
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -