📄 601caitao cpu 完整版.txt
字号:
#include "stdio.h"
#include "malloc.h"
#include "dos.h"
union REGS regs; /*定义共同体类型*/
typedef struct PCB /*定义pcb的数据类型*/
{ char MM[5]; /*进程名称*/
int TT; /*到达时间*/
}PCB,*PcbPtr;
typedef struct QNode /*定义结点的数据类型*/
{ PcbPtr data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{ QueuePtr front;
QueuePtr rear;
}LinkQueue;
static InitQueue(LinkQueue *Q) /*构造一个空队列Q*/
{ QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
if(!p)
exit(0);
p->data=NULL; p->next=NULL; p=Q->rear=Q->front;
return 1;
}
static EnQueue(LinkQueue *Q,PcbPtr P ) /*创建队列*/
{ QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
if(!p)
exit(0);
p->data=P;p->next=NULL; Q->rear->next=p; Q->rear=p;
return 1;
}
void WriteElem(PcbPtr PP) /*输出结果函数*/
{ printf("%s %d",PP->MM,PP->TT);
}
void InputPro(LinkQueue* Q) /*给队列赋值*/
{ int ProNum,i;
PcbPtr p;
printf("============\n******SJFCPU=====\nThenumberofprocess:\n===========
\n***************\n\n\n");
scanf("%d",&ProNum);
sleep(3);
clrscr();
textbackground(4)
printf("=============\n*************\nPlease Input Process And Time:\n===============\n ***************\n")
for(i=0;i<ProNum;i++)
{ p=(PcbPtr)malloc(sizeof(PCB));
scanf("%s%d",p->MM,&(p->TT));
EnQueue(Q,p);
}
}
static Pick(LinkQueue* Q,PcbPtr PP) /*静态函数定义*/
{ QueuePtr head,p;
if(Q->front==Q->rear)
return 0;
p=Q->front->next;
for(head=p;head!=NULL;head=head->next)
{
if(head->data->TT<p->data->TT)
p=head;
}
*PP=*p->data;
return 1;
}
void DeQueue(LinkQueue *Q,PcbPtr PP)
{
QueuePtr p,q;
p=Q->front;
for(;p->next->data->TT!=PP->TT;p=p->next);
q=p->next;
if(q->next==NULL)
{
Q->rear=p; p->next=NULL;
}
else
{ p->next=p->next->next;
}
free(q);
}
void Work(int TT)
{ long int t;
do{
t++;
if(t%1000000==0)
printf(".*.");
}while(t<TT*1e6);
}
void set_cur_off(void)
{
regs.x.ax=0x0100;
regs.x.bx=0x0;
regs.h.ch=0x20;
int86(16,®s,®s);
}
void Run(LinkQueue *Q)
{
PcbPtr PP;
int TT;
while(Q->front->next!=NULL)
{
Pick(Q,PP);
set_cur_off();
WriteElem(PP);
TT=PP->TT;
Work(TT);
printf("\n");
DeQueue(Q,PP);
}
}
void main()/*主函数定义*/
{ LinkQueue Q;
clrscr();
textbackground(3);
InitQueue(&Q);
InputPro(&Q);
Run(&Q);
getch();
}
四.运行步骤及结果:
1) ============**********
The number of process: 3
============**********
2) ================**************
Please Input Process And Time:
================**************
p1 2 6
p2 5 5
p3 3 9
3) p2 5 5.......
p1 2 6..........
p3 3 9.................
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -