代码编写
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ready 0
#define running 1
#define blocking 2
#define nProcess 5
#define done 3
int busTime = 0;
struct Process
{
int ID;
int ARRIVE_TIME;
int PRIORITY;
int CPU_TIME;
int ALL_TIME;
int START_BLOCK;
int BLOCK_TIME;
int STATE;
int NEXT;
};
void createProcess(int n, struct Process *p)
{
int i;
for (i = 0; i < n; i++)
{
p[i].ID = i;
p[i].ARRIVE_TIME = rand() % 100;
p[i].PRIORITY = rand() % 10;
p[i].CPU_TIME = 0;
p[i].ALL_TIME = rand() % 10 + 1;
p[i].START_BLOCK = rand() % 10;
p[i].BLOCK_TIME = rand() % 10 + 1;
p[i].STATE = ready;
p[i].NEXT = i + 1;
}
}
void scheduling(int id, struct Process *p)
{
int i;
int cpuTime = p[id].CPU_TIME;
int allTime = p[id].ALL_TIME;
int startBlock = p[id].START_BLOCK;
printf("进程%d开始执行!\n", id);
for (i = p[id].CPU_TIME; i < allTime; i++)
{
busTime++;
p[id].CPU_TIME++;
if (p[id].CPU_TIME == startBlock && p[id].ALL_TIME != startBlock)
{
p[id].STATE = blocking;
printf("进程%d已被阻塞!\n", id);
break;
}
}
if (p[id].CPU_TIME == p[id].ALL_TIME)
{
p[id].STATE = done;
printf("进程%d已经完成!\n", id);
}
}
void block2ready(int id, struct Process *p)
{
if (busTime >= 0 + p[id].START_BLOCK + p[id].BLOCK_TIME && p[id].STATE == blocking)
{
p[id].STATE = ready;
printf("进程%d已被释放!\n", id);
}
else
{
printf("无法解锁当前进程:%d!\n", id);
}
}
void FCFS(struct Process *p, struct Process *FCFCP)
{
int i, j;
for (i = 0; i < nProcess; i++)
{
for (j = 0; j < nProcess; j++)
{
if (p[i].ARRIVE_TIME < p[j].ARRIVE_TIME)
{
FCFCP[i] = p[j];
}
}
}
}
void SJF(struct Process *p, struct Process *SJFP)
{
int i, j;
for (i = 0; i < nProcess; i++)
{
for (j = 0; j < nProcess; j++)
{
if (p[i].ALL_TIME < p[j].ALL_TIME)
{
SJFP[i] = p[j];
}
}
}
}
void PRIORITY_FIRST(struct Process *p, struct Process *PRIORITYP)
{
int i, j;
for (i = 0; i < nProcess; i++)
{
for (j = 0; j < nProcess; j++)
{
if (p[i].PRIORITY < p[j].PRIORITY)
{
PRIORITYP[i] = p[j];
}
}
}
}
void runningFunc(struct Process *p)
{
for (int i = 0; i <= nProcess; i++)
{
if (p[i].STATE == ready)
{
scheduling(i, p);
}
}
for (int i = 0; i <= nProcess; i++)
{
if (p[i].STATE == blocking)
{
block2ready(i, p);
}
}
for (int i = 0; i <= nProcess; i++)
{
if (p[i].STATE == ready)
{
scheduling(i, p);
}
}
}
void main(void)
{
srand((unsigned)time(NULL));
struct Process *process = (struct Process *)malloc(sizeof(struct Process) * nProcess);
createProcess(nProcess, process);
struct Process *FCFSProcess = (struct Process *)malloc(sizeof(struct Process) * nProcess);
FCFS(process, FCFSProcess);
struct Process *SJFProcess = (struct Process *)malloc(sizeof(struct Process) * nProcess);
SJF(process, SJFProcess);
struct Process *PRIORITYProcess = (struct Process *)malloc(sizeof(struct Process) * nProcess);
PRIORITY_FIRST(process, PRIORITYProcess);
free(process);
printf("将以先来先服务原则执行进程!\n");
runningFunc(FCFSProcess);
for (int i = 0; i <= 10; i++)
{
printf("-");
}
printf("\n");
free(FCFSProcess);
busTime = 0;
printf("将以短作业优先原则执行进程!\n");
runningFunc(SJFProcess);
for (int i = 0; i <= 10; i++)
{
printf("-");
}
printf("\n");
free(SJFProcess);
busTime = 0;
printf("将以优先级原则执行进程!\n");
runningFunc(PRIORITYProcess);
for (int i = 0; i <= 10; i++)
{
printf("-");
}
printf("\n");
free(PRIORITYProcess);
busTime = 0;
printf("所有进程作业全部完成!\n");
}
运行结果
将以先来先服务原则执行进程!
进程0开始执行!
进程0已被阻塞!
进程1开始执行!
进程1已被阻塞!
进程2开始执行!
进程2已被阻塞!
进程3开始执行!
进程3已经完成!
进程4开始执行!
进程4已经完成!
进程0已被释放!
进程1已被释放!
进程2已被释放!
进程0开始执行!
进程0已经完成!
进程1开始执行!
进程1已经完成!
进程2开始执行!
进程2已经完成!
-----------
将以短作业优先原则执行进程!
进程0开始执行!
进程0已被阻塞!
进程1开始执行!
进程1已被阻塞!
进程2开始执行!
进程2已被阻塞!
进程3开始执行!
进程3已被阻塞!
进程4开始执行!
进程4已经完成!
进程0已被释放!
进程1已被释放!
进程2已被释放!
进程3已被释放!
进程0开始执行!
进程0已经完成!
进程1开始执行!
进程1已经完成!
进程2开始执行!
进程2已经完成!
进程3开始执行!
进程3已经完成!
-----------
将以优先级原则执行进程!
进程0开始执行!
进程0已经完成!
进程1开始执行!
进程1已经完成!
进程2开始执行!
进程2已被阻塞!
进程3开始执行!
进程3已经完成!
进程4开始执行!
进程4已经完成!
进程2已被释放!
进程2开始执行!
进程2已经完成!
-----------
所有进程作业全部完成!