多级反馈队列FCFS(周转时间的计算方法可能是错误的)![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c9ee8ea2f3e9a52670c2bbd607d88217.png)
代码1
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#include<Windows.h>
typedef struct PCB
{
char name;
int ComeTime;
int ServerTime;
int FinishTime;
int NeedTime;
int TurnoverTime;
double WeightedTurnoverTime;
char state;
int round;
int count;
struct PCB* next;
}PCB;
typedef struct Queue
{
PCB* LinkPCB;
int prio;
int round;
struct Queue* next;
}ReadyQueue;
int cometime;
int ProcessNum = 1;
int timeslice =2;
PCB* run = NULL, * finish = NULL;
int TimNum=0;
int ReadyNum = 3;
char name = 'A';
ReadyQueue* Head = NULL;
void endOutput();
void InsertFinish(PCB* in);
void PrioCreate();
void GetFirst(ReadyQueue* queue);
void InsertLast(PCB* in, ReadyQueue* queue);
void InsertFinal(PCB* pcb);
void ProcessCreate();
void RoundRun(ReadyQueue* timechip);
void MultiDispatch();
void SortQueue(ReadyQueue* que);
void InsertPrio(ReadyQueue* in);
void InsertReady(PCB* pcb, ReadyQueue* queue);
void sortPCB(PCB* T);
int main(void)
{
PrioCreate();
ProcessCreate();
MultiDispatch();
endOutput();
return 0;
}
void MultiDispatch()
{
ReadyQueue* q;
q = Head;
printf("正在进行排序\n");
sortPCB(Head->LinkPCB);
printf("排序完成\n");
int flag=1;
printf("取队列头元素\n");
GetFirst(q);
while (run != NULL)
{
if (Head->LinkPCB != NULL)
q = Head;
while (flag)
{
run->count++;
run->ServerTime++;
run->NeedTime--;
TimNum++;
if (run->NeedTime == 0)
{
run->FinishTime = TimNum ;
run->TurnoverTime = run->FinishTime - run->ComeTime;
run->WeightedTurnoverTime = run->TurnoverTime / run->ServerTime;
run->state = 'F';
InsertFinish(run);
flag = 0;
}
else if (run->count == run->round)
{
run->state = 'w';
run->count = 0;
if (q->next != NULL)
{
run->round = q->next->round;
InsertLast(run, q->next);
flag = 0;
}
else
{
RoundRun(q);
}
}
}
flag = 1;
if (q->LinkPCB == NULL)
q = q->next;
if (q->next == NULL)
{
RoundRun(q);
break;
}
GetFirst(q);
}
}
void GetFirst(ReadyQueue* queue)
{
if (queue->LinkPCB != NULL)
{
run = queue->LinkPCB;
run->state = 'R';
queue->LinkPCB = queue->LinkPCB->next;
run->next = NULL;
}
else
{
run = NULL;
}
}
void PrioCreate()
{
ReadyQueue* tmp;
int i;
printf("输入就绪队列的个数,最多输入3个:\n");
scanf("%d", &ReadyNum);
for (i = 0; i < ReadyNum; i++)
{
int times = timeslice * pow(2, i);
if ((tmp = (ReadyQueue*)malloc(sizeof(ReadyQueue))) == NULL)
{
perror("malloc");
exit(1);
}
tmp->round = times;
tmp->prio = times;
tmp->LinkPCB = NULL;
tmp->next = NULL;
InsertPrio(tmp);
}
}
void ProcessCreate()
{
time_t ts;
PCB* tmp;
int i;
int Pronum;
printf("输入进程的个数:\n");
scanf("%d", &Pronum);
getchar();
cometime = 0;
printf("等待%d秒钟,正在生成随机数", Pronum);
for (i = 0; i < Pronum; i++)
{
Sleep(1000);
srand((unsigned)time(NULL));
int num1 = rand() % 5+1 ;
int num2 = rand() %9 + 1;
if ((tmp = (PCB*)malloc(sizeof(PCB))) == NULL)
{
perror("malloc");
exit(1);
}
tmp->name = name;
name = name + 1;
tmp->ComeTime= cometime;
cometime = num1;
tmp->ServerTime = 0;
tmp->NeedTime= num2;
tmp->state = 'W';
tmp->round = Head->round;
tmp->count = 0;
InsertLast(tmp, Head);
}
}
void InsertPrio(ReadyQueue* in)
{
ReadyQueue* fst, * nxt;
fst = nxt = Head;
if (Head == NULL)
{
in->next = Head;
Head = in;
}
else
{
if (in->prio <= fst->prio)
{
in->next = Head;
Head = in;
}
else
{
while (fst->next != NULL)
{
nxt = fst;
fst = fst->next;
}
if (fst->next == NULL)
{
in->next = fst->next;
fst->next = in;
}
else
{
nxt = in;
in->next = fst;
}
}
}
}
void InsertReady(PCB* pcb, ReadyQueue* queue)
{
PCB* p;
pcb->state = 'W';
pcb->round = queue->round;
p = queue->LinkPCB;
if (queue->LinkPCB == NULL)
{
pcb->next = queue->LinkPCB;
queue->LinkPCB = pcb;
}
else
{
while (p->next != NULL)
{
p = p->next;
}
pcb->next = p->next;
p->next = pcb;
}
}
void InsertFinal(PCB* pcb)
{
PCB* p;
pcb->state = 'F';
p = finish;
if (finish == NULL)
{
pcb->next = NULL;
finish = pcb;
}
else
{
while (p->next != NULL)
{
p = p->next;
}
pcb->next = p->next;
p->next = pcb;
}
}
void InsertLast(PCB* in, ReadyQueue* queue)
{
PCB* fst;
fst = queue->LinkPCB;
if (queue->LinkPCB == NULL)
{
in->next = queue->LinkPCB;
queue->LinkPCB = in;
}
else
{
while (fst->next != NULL)
{
fst = fst->next;
}
in->next = fst->next;
fst->next = in;
}
}
void InsertFinish(PCB* in)
{
PCB* fst;
fst = finish;
if (finish == NULL)
{
in->next = finish;
finish = in;
}
else
{
while (fst->next != NULL)
{
fst = fst->next;
}
in->next = fst->next;
fst->next = in;
}
}
void RoundRun(ReadyQueue* timechip)
{
int flag = 1;
GetFirst(timechip);
while (run != NULL)
{
while (flag)
{
run->count++;
run->ServerTime++;
run->NeedTime--;
TimNum++;
if (run->NeedTime == 0)
{
run->FinishTime = TimNum;
run->TurnoverTime = run->FinishTime - run->ComeTime;
run->WeightedTurnoverTime = run->TurnoverTime / run->ServerTime;
run->state = 'F';
InsertFinish(run);
flag = 0;
}
else if (run->count == timechip->round)
{
run->state = 'W';
run->count = 0;
InsertLast(run, timechip);
flag = 0;
break;
}
}
flag = 1;
if (timechip->LinkPCB != NULL)
{
GetFirst(timechip);
}
else
{
break;
}
}
}
void endOutput()
{
PCB* p;
printf("进程名\t到达时间\t已服务时间\t完成时间\t周转时间\t带权周转时间\n");
p = finish;
while (p != NULL)
{
printf("%c\t %d\t\t%d\t\t%d\t\t %d\t\t%.2f\n", p->name, p->ComeTime, p->ServerTime, p->FinishTime, p->TurnoverTime, p->WeightedTurnoverTime);
p = p->next;
}
}
void SortQueue(ReadyQueue* queue)
{
ReadyQueue* m, * n;
m = n = Head;
if (Head == NULL)
{
queue->next = NULL;
Head = queue;
}
else
{
if (queue->prio <= m->prio)
{
queue->next = m;
Head = queue;
}
else
{
while (m->next != NULL)
{
n = m;
m = m->next;
}
if (m->next == NULL)
{
queue->next = m->next;
m->next = queue;
}
else
{
n->next = queue;
queue->next = m;
}
}
}
}
void sortPCB(PCB* T)
{
PCB* X = (PCB*)malloc(sizeof(PCB));
PCB* p = (PCB*)malloc(sizeof(PCB));
PCB* Current = T->next;
PCB* PreCurrent = T;
PCB* TailX = X;
*X = *T;
while (T->next != NULL) {
int tem = 999998;
Current = T->next;
PreCurrent = T;
while (Current != NULL) {
if (Current->ComeTime<= tem) {
tem = Current->ComeTime;
p = PreCurrent;
}
Current = Current->next;
PreCurrent = PreCurrent->next;
}
TailX->next = p->next;
TailX = TailX->next;
if (p->next->next != NULL)
p->next = p->next->next;
else
p->next = NULL;
}
*T = *X;
}
代码2
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
typedef struct PCB
{
int Numbers;
char name;
int ComeTime;
int ServerTime;
int FinishTime;
int NeedTime;
int TurnoverTime;
double WeightedTurnoverTime;
char state;
int round;
int count;
struct PCB* next;
}PCB;
typedef struct Queue
{
PCB* LinkPCB;
int prio;
int round;
struct Queue* next;
}ReadyQueue;
int cometime;
int ProcessNum = 1;
int timeslice =1;
PCB* run = NULL, * finish = NULL;
int TimNum=0;
int ReadyNum = 3;
char name = 'A';
ReadyQueue* Head = NULL;
void Output();
void endOutput();
void InsertFinish(PCB* in);
void PrioCreate();
void GetFirst(ReadyQueue* queue);
void InsertLast(PCB* in, ReadyQueue* queue);
void InsertFinal(PCB* pcb);
void ProcessCreate();
void RoundRun(ReadyQueue* timechip);
void MultiDispatch();
void SortQueue(ReadyQueue* que);
void InsertPrio(ReadyQueue* in);
void InsertReady(PCB* pcb, ReadyQueue* queue);
int main(void)
{
PrioCreate();
ProcessCreate();
MultiDispatch();
endOutput();
return 0;
}
void MultiDispatch()
{
ReadyQueue* q;
q = Head;
int flag=1;
GetFirst(q);
while (run != NULL)
{
if (Head->LinkPCB != NULL)
q = Head;
while (flag)
{
run->count++;
run->ServerTime++;
run->NeedTime--;
TimNum++;
if (run->NeedTime == 0)
{
run->FinishTime = TimNum ;
run->TurnoverTime = run->FinishTime - run->ComeTime;
run->WeightedTurnoverTime = run->TurnoverTime / run->ServerTime;
run->state = 'F';
InsertFinish(run);
flag = 0;
}
else if (run->count == run->round)
{
run->state = 'w';
run->count = 0;
if (q->next != NULL)
{
run->round = q->next->round;
InsertLast(run, q->next);
flag = 0;
}
else
{
RoundRun(q);
}
}
}
flag = 1;
if (q->LinkPCB == NULL)
q = q->next;
if (q->next == NULL)
{
RoundRun(q);
break;
}
GetFirst(q);
}
}
void GetFirst(ReadyQueue* queue)
{
if (queue->LinkPCB != NULL)
{
run = queue->LinkPCB;
run->state = 'R';
queue->LinkPCB = queue->LinkPCB->next;
run->next = NULL;
}
else
{
run = NULL;
}
}
void PrioCreate()
{
ReadyQueue* tmp;
int i;
for (i = 0; i < ReadyNum; i++)
{
int times = timeslice * pow(2, i);
if ((tmp = (ReadyQueue*)malloc(sizeof(ReadyQueue))) == NULL)
{
perror("malloc");
exit(1);
}
tmp->round = times;
tmp->prio = times;
tmp->LinkPCB = NULL;
tmp->next = NULL;
InsertPrio(tmp);
}
}
void ProcessCreate()
{
srand(time(NULL));
PCB* tmp;
int i;
int num;
printf("输入进程的个数:\n");
scanf("%d", &num);
getchar();
cometime = 0;
for (i = 0; i < num; i++)
{
if ((tmp = (PCB*)malloc(sizeof(PCB))) == NULL)
{
perror("malloc");
exit(1);
}
printf("输入进程到达时间和进程所需要时间\n");
scanf("%d%d", &tmp->ComeTime,&tmp->NeedTime);
tmp->name = name;
name = name + 1;
tmp->ServerTime = 0;
tmp->state = 'W';
tmp->round = Head->round;
tmp->count = 0;
InsertLast(tmp, Head);
}
}
void InsertPrio(ReadyQueue* in)
{
ReadyQueue* fst, * nxt;
fst = nxt = Head;
if (Head == NULL)
{
in->next = Head;
Head = in;
}
else
{
if (in->prio <= fst->prio)
{
in->next = Head;
Head = in;
}
else
{
while (fst->next != NULL)
{
nxt = fst;
fst = fst->next;
}
if (fst->next == NULL)
{
in->next = fst->next;
fst->next = in;
}
else
{
nxt = in;
in->next = fst;
}
}
}
}
void InsertReady(PCB* pcb, ReadyQueue* queue)
{
PCB* p;
pcb->state = 'W';
pcb->round = queue->round;
p = queue->LinkPCB;
if (queue->LinkPCB == NULL)
{
pcb->next = queue->LinkPCB;
queue->LinkPCB = pcb;
}
else
{
while (p->next != NULL)
{
p = p->next;
}
pcb->next = p->next;
p->next = pcb;
}
}
void InsertFinal(PCB* pcb)
{
PCB* p;
pcb->state = 'F';
p = finish;
if (finish == NULL)
{
pcb->next = NULL;
finish = pcb;
}
else
{
while (p->next != NULL)
{
p = p->next;
}
pcb->next = p->next;
p->next = pcb;
}
}
void InsertLast(PCB* in, ReadyQueue* queue)
{
PCB* fst;
fst = queue->LinkPCB;
if (queue->LinkPCB == NULL)
{
in->next = queue->LinkPCB;
queue->LinkPCB = in;
}
else
{
while (fst->next != NULL)
{
fst = fst->next;
}
in->next = fst->next;
fst->next = in;
}
}
void InsertFinish(PCB* in)
{
PCB* fst;
fst = finish;
if (finish == NULL)
{
in->next = finish;
finish = in;
}
else
{
while (fst->next != NULL)
{
fst = fst->next;
}
in->next = fst->next;
fst->next = in;
}
}
void RoundRun(ReadyQueue* timechip)
{
int flag = 1;
GetFirst(timechip);
while (run != NULL)
{
while (flag)
{
run->count++;
run->ServerTime++;
run->NeedTime--;
TimNum++;
if (run->NeedTime == 0)
{
run->FinishTime = TimNum;
run->TurnoverTime = run->FinishTime - run->ComeTime;
run->WeightedTurnoverTime = run->TurnoverTime / run->ServerTime;
run->state = 'F';
InsertFinish(run);
flag = 0;
}
else if (run->count == timechip->round)
{
run->state = 'W';
run->count = 0;
InsertLast(run, timechip);
flag = 0;
break;
}
}
flag = 1;
if (timechip->LinkPCB != NULL)
{
GetFirst(timechip);
}
else
{
break;
}
}
}
void Output()
{
ReadyQueue* q = Head;
PCB* p;
printf("进程名\t 时间片\t 已服务时间\t所需时间\t状态\t\n");
p = run;
while (p != NULL)
{
printf( " %c\t %d\t %d \t%d\t%c\n",p->name , p->round ,p->ServerTime, p->NeedTime,p->state );
p = p->next;
}
while (q)
{
if (q->LinkPCB != NULL)
{
p = q->LinkPCB;
while (p)
{
printf(" %c\t %d\t %d \t%d\t%c\n", p->name, p->round, p->ServerTime, p->NeedTime, p->state);
p = p->next;
}
}
q = q->next;
}
p = finish;
while (p != NULL)
{
printf(" %c\t\t %d\t\t %d \t\t%d\t\t%c\n", p->name, p->round, p->ServerTime, p->NeedTime, p->state);
p = p->next;
}
}
void endOutput()
{
PCB* p;
printf("进程名\t到达时间\t已服务时间\t完成时间\t周转时间\t带权周转时间\n");
p = finish;
while (p != NULL)
{
printf("%c\t %d\t\t%d\t\t%d\t\t %d\t\t%.2f\n", p->name, p->ComeTime, p->ServerTime, p->FinishTime, p->TurnoverTime, p->WeightedTurnoverTime);
p = p->next;
}
}
void SortQueue(ReadyQueue* queue)
{
ReadyQueue* m, * n;
m = n = Head;
if (Head == NULL)
{
queue->next = NULL;
Head = queue;
}
else
{
if (queue->prio <= m->prio)
{
queue->next = m;
Head = queue;
}
else
{
while (m->next != NULL)
{
n = m;
m = m->next;
}
if (m->next == NULL)
{
queue->next = m->next;
m->next = queue;
}
else
{
n->next = queue;
queue->next = m;
}
}
}
}