FCFS算法

原理: 按照作业进入系统的先后次序来挑选作业,先进入系统的作业优先被挑选。 用c语言写的,采用单链表来模拟PCB,实现CPU调用进程。
FcFs.h:

#define _CRT_SECURE_NO_WARNINGS #include #includetypedef struct PCB { char ID[15]; //进程名字 int arrive_time; //进程到达时间 int work_time; //进程服务时间 int start_time; //进程开始时间 int finish_time; //进程完成时间 int turnover_time; //周转时间 double Wturnover_time; //加权周转时间 int data; //执行顺序 char sign; //标记 N 表示已排序,不能进入。Y表示没排序,可以进入。 struct PCB *next; }PCB,*P_PCB; //接收所有进程,形成PCB链表 int Enter(P_PCB start) { int process_sum = 0; //进程的总数 printf("请输入进程的个数:"); scanf("%d", &process_sum); start->data = https://www.it610.com/article/process_sum; //头指针装入了进程总数 for (int i = 0; i < process_sum; i++) { //录入第i个进程的信息 P_PCB newHead; newHead = (P_PCB)malloc(sizeof(PCB)); //新的节点 printf("请输入进程%d的名字:", i+1); scanf("%s", newHead->ID); printf("请输入进程%d的到达时间;", i+1); scanf("%d", &newHead->arrive_time); printf("请输入进程%d的服务时间;", i+1); scanf("%d", &newHead->work_time); system("cls"); newHead->next = start->next; start->next = newHead; } return 0; }//根据进程到达时间对除ID和arrive_time和work_time外其他值赋值 int Rank(P_PCB start) { int temp, atime, ftime; P_PCB m= NULL,p,q; //p为了找一个sign是Y的节点把arrive_time给temp,q是和其他的arrive_time比较的跟踪节点,m是最小temp的跟踪 //对所有节点的sign赋值 p = start->next; for(int i=0; idata; i++) { p->sign = 'Y'; p = p->next; } //给每一个节点赋了 data值 for (int i=0; i< start->data ; i++) { //arrive_time找最小值 p = start->next; while (p->next != NULL) { if (p->sign == 'Y') { temp = p->arrive_time; m = p; break; } } q = p; while ( q->next != NULL) { q = q->next; if (q->sign == 'Y'&& temp > q->arrive_time) { temp = q->arrive_time; m = q; } } m->data = https://www.it610.com/article/i + 1; m->sign = 'N'; //根据data值对start_time和finish_time和sign赋值 if (m->data =https://www.it610.com/article/= 1) { m->start_time = m->arrive_time; //开始时间 m->finish_time = m->work_time; //结束时间 atime = m->start_time; ftime = m->finish_time; m->turnover_time = m->finish_time - m->arrive_time; //周转时间(完成-到达) m->Wturnover_time = (double)m->turnover_time / m->work_time; //加权周转时间 } else { if (m->arrive_time <= ftime) { m->start_time = ftime; //开始时间 m->finish_time = m->work_time+ m->start_time; //结束时间 atime = m->start_time; ftime = m->finish_time; m->turnover_time = m->finish_time - m->arrive_time; //周转时间(完成-到达) m->Wturnover_time = (double)m->turnover_time / m->work_time; //加权周转时间 } else { m->start_time= m->arrive_time; //开始时间 m->finish_time = m->work_time+ m->start_time; //结束时间 atime = m->start_time; ftime = m->finish_time; m->turnover_time = m->finish_time - m->arrive_time; //周转时间(完成-到达) m->Wturnover_time = (double)m->turnover_time / m->work_time; //加权周转时间 } } } return 0; }//输出所有进程信息 void A(P_PCB start) { P_PCB p ; for (int i = 0; i < start->data; i++) { p = start; while (p->next!= NULL) { p = p->next; if (p->data =https://www.it610.com/article/= i + 1) { printf("进程名字%s,到达时间:%d,服务时间:%d", p->ID, p->arrive_time, p->work_time); printf("开始时间:%d ,完成时间:%d ,周转时间:%d ,加权周转时间:%lf\n", p->start_time, p->finish_time, p->turnover_time, p->Wturnover_time); printf("+++++++++++++++++++++++++++++++\n"); } } } }//模拟cpu运行 void B(P_PCB start) { P_PCB q; for (int m = 0; m < start->data; m++) { q = start; for (int i = 0; i < start->data; i++) { q = q->next; if (q->data =https://www.it610.com/article/= m + 1) { printf("在%d时刻:%s进程开始执行。\n", q->start_time, q->ID); for (int j = q->start_time; j <= q->finish_time; j++) { printf("在%d时刻:%s进程正在执行。\n", j, q->ID); } printf("在%d时刻:%s进程结束执行。\n", q->finish_time, q->ID); break; } } } }//函数调用 int Show(P_PCB start) { int a = 0; P_PCB p = start; printf("请录入进程的信息:\n"); Enter(start); Rank(start); while (1) { printf("请选择你要展示的效果:\n"); printf("1.输出所有进程的信息。\n"); printf("2.模拟cpu工作状态。\n"); printf("3.退出.\n"); scanf("%d", &a); switch (a) { case 1: A(start); break; case 2: B(start); break; case 3: exit(0); break; default:break; } system("pause"); system("cls"); } return 0; }

FcFs_test.cpp:
#include"FcFs.h"int main() { P_PCB start; start = (P_PCB)malloc(sizeof(PCB)); //头结点 start->next = NULL; Show(start); return 0; }

【FCFS算法】

    推荐阅读