单链表(进程表,就绪进程队列),结构体(结构化进程信息)
1.输入模块:接收进程信息,包括进程代号(简化了输入信息,只是int型),进程创建时间,进程运行时间,进程优先级,得到进程表
2.就绪进程模块:用Time记录系统当前运行的时间,得出就绪进程队列
3.对就绪进程队列按照优先序列排序
**4.**调度排序后就绪队列第一个执行
state用来记录当前进程是否存在于就绪队列中(可以考虑把state放入结构体中这样更有逻辑性)
排序算法采用的是交换结点值的冒泡算法,而不是直接交换结点
就绪队列应该随着系统时间Time的变化实时更新,并且改变优先序列(即一个进程
完成调度,系统时间发生变化,就绪队列及其优先顺序也发生变化)
Time默认值为进程最小开始时间,可以改进更普适一点
其他调度算法只需要调整排序条件即可,如短进程优先把进程运行时间改为比较条件
#include
#include
#include
#define ProcessNum 5 //进程数
//进程结构体
typedef struct pcb
{
int Pname; //进程代号
int CreateTime; //创建时间
int ProcessTime; //运行时间
int Priority; //优先级
struct pcb *next; //指向下一个进程
}PCB;
int Time = 0; //系统执行时间
int state[6] = {0,0,0,0,0,0}; //五个进程的到达状态
PCB *ready; // 就绪进程队列
//进程就绪队列
void Ready_Queue(PCB *head)
{
PCB *h = head;
PCB *current = NULL;
while(h){
if(h->CreateTime<=Time && state[h->Pname]==0){
state[h->Pname] = 1;//对进程序列中的进程状态进行标记
current = (PCB*)malloc(sizeof(PCB));
current->next = NULL;
current->Pname = h->Pname;
current->CreateTime = h->CreateTime;
current->ProcessTime = h->ProcessTime;
current->Priority = h->Priority;
if(!ready)
ready = current;
else{
current->next = ready;
ready = current;
}
}
h = h->next;
}
printf("当前就绪进程:");
h = ready;
while(h){
printf("p%d ",h->Pname);
h = h->next;
}
}
//进程优先级排序
void Priority_Sort()
{
int Pname; //进程代号
int CreateTime; //创建时间
int ProcessTime; //运行时间
int Priority; //优先级
PCB *h = ready;
PCB *current = NULL;
while(h){
current = h->next;
while(current){
if(current->Priority > h->Priority){//交换进程信息
Pname = current->Pname;
current->Pname = h->Pname;
h->Pname = Pname;
CreateTime = current->CreateTime;
current->CreateTime = h->CreateTime;
h->CreateTime = CreateTime;
ProcessTime = current->ProcessTime;
current->ProcessTime = h->ProcessTime;
h->ProcessTime = ProcessTime;
Priority = current->Priority;
current->Priority = h->Priority;
h->Priority = Priority;
}
current = current->next;
}
h = h->next;
}
printf("\n当前就绪进程优先级:");
h = ready;
while(h){
printf("p%d ",h->Pname);
h = h->next;
}
}
//读取进程信息队列(尾插法)
PCB* Process_Info()
{
int i;
PCB *current,*head,*tail;
tail = head = NULL;
printf("请输入各进程状态(代号,进程创建时间,进程执行时间,优先级)\n");
for(i = 0;i < ProcessNum; i++){
current = (PCB*)malloc(sizeof(PCB));
scanf("%d",¤t->Pname);
scanf("%d",¤t->CreateTime);
scanf("%d",¤t->ProcessTime);
scanf("%d",¤t->Priority);
if(!head)
tail = head = current;
else{
tail->next = current;
tail = current;
}
}
tail->next = NULL;
return head;
}
//打印进程信息
void Print_Info(PCB* head)
{
PCB *h = head;
printf("Pname CreateTime ProcessTime Priority\n");
while(h){
printf("P%d\t\t",h->Pname);
printf("%d\t\t",h->CreateTime);
printf("%d\t\t",h->ProcessTime);
printf("%d\n",h->Priority);
h = h->next;
}
}
void ProcessDispatch(PCB *head)
{
int finish=0;
PCB *del= NULL;
while(1){
Ready_Queue(head);
Priority_Sort();
Time += ready->ProcessTime;
printf("\n时间Time=%d时,进程P%d完成\n\n",Time,ready->Pname);
finish += 1;
//虽然程序很短运行结束就会自动释放,但malloc过的空间记得释放是个好习惯
del = ready;
ready = ready->next;
free(del);
//判断所有进程是否完成
if(finish == 5)
break;
}
}
int main()
{
PCB *head=NULL;
head = Process_Info();
Print_Info(head);
ProcessDispatch(head);
printf("进程调度完毕!");
return 0;
}
#include
#include
//进程结构体
typedef struct pcb
{
int Pname; //进程代号
int state; //进程就绪状态1就绪0未就绪
int CreateTime; //创建时间
int ProcessTime; //运行时间
struct pcb *next; //指向下一个进程
}PCB;
int Time = 0; //系统执行时间
PCB *ready = NULL; // 就绪进程队列
//进程就绪队列
void Ready_Queue(PCB *head)
{
PCB *h = head;
PCB *current = NULL;
while(h){
if(h->CreateTime<=Time && h->state==0){
h->state = 1;//对进程序列中的进程状态进行标记
current = (PCB*)malloc(sizeof(PCB));
current->next = NULL;
current->Pname = h->Pname;
current->CreateTime = h->CreateTime;
current->ProcessTime = h->ProcessTime;
if(!ready)
ready = current;
else{
current->next = ready;
ready = current;
}
}
h = h->next;
}
printf("当前就绪进程:");
h = ready;
while(h){
printf("p%d ",h->Pname);
h = h->next;
}
}
//进程优先级排序
void Priority_Sort()
{
int Pname; //进程代号
int CreateTime; //创建时间
int ProcessTime; //运行时间
PCB *h = ready;
PCB *current = NULL;
while(h){
current = h->next;
while(current){
if(current->ProcessTime < h->ProcessTime){//交换进程信息
Pname = current->Pname;
current->Pname = h->Pname;
h->Pname = Pname;
CreateTime = current->CreateTime;
current->CreateTime = h->CreateTime;
h->CreateTime = CreateTime;
ProcessTime = current->ProcessTime;
current->ProcessTime = h->ProcessTime;
h->ProcessTime = ProcessTime;
}
current = current->next;
}
h = h->next;
}
printf("\n当前就绪进程优先级:");
h = ready;
while(h){
printf("p%d ",h->Pname);
h = h->next;
}
}
//打印进程信息
void Print_Info(PCB* head)
{
PCB *h = head;
printf("Pname CreateTime ProcessTime\n");
while(h){
printf("P%d\t\t",h->Pname);
printf("%d\t\t",h->CreateTime);
printf("%d\n",h->ProcessTime);
h = h->next;
}
}
void ProcessDispatch(PCB *head,int ProcessNum)
{
int finish=0;
PCB *del= NULL;
while(1){
Ready_Queue(head);
Priority_Sort();
Time += ready->ProcessTime;
printf("\n时间Time=%d时,进程P%d完成\n\n",Time,ready->Pname);
finish += 1;
//释放已完成的就绪序列
del = ready;
ready = ready->next;
free(del);
//判断所有进程是否完成
if(finish == ProcessNum)
break;
}
}
int main()
{
int i,ProcessNum;
PCB *head=NULL;
PCB *current,*tail;
tail = head = NULL;
printf("请输入待调度进程数:\n");
scanf("%d",&ProcessNum);
printf("请输入各进程状态(代号,进程创建时间,进程执行时间)\n");
for(i = 0;i < ProcessNum; i++){
current = (PCB*)malloc(sizeof(PCB));
scanf("%d",¤t->Pname);
scanf("%d",¤t->CreateTime);
scanf("%d",¤t->ProcessTime);
current->state = 0;
if(!head)
tail = head = current;
else{
tail->next = current;
tail = current;
}
}
tail->next = NULL;
Print_Info(head);
ProcessDispatch(head,ProcessNum);
printf("进程调度完毕!");
return 0;
}