#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <time.h>
#define RUNNING 0
#define READY 1
#define WAIT 2
typedef struct PCB
{
int pid;//进程的标识号
int priority;//进程的优先级,数值越大,优先级越高
int pstatus;//进程的状态
struct PCB * next;//指向下一个进程的指针
}PCB;
PCB * Create(int n)
{
//head指向头节点的指针,cur表示指向当前结点的指针
PCB * head,* temp,* cur;
int i;
head=(PCB *)malloc(sizeof(PCB));//分配空间
if(head==NULL)//分配失败
{
printf("ERROR");
exit(1);
}
cur=head;head->next=NULL;//头结点
for(i=0;i<n;i++)
{
temp=(PCB *)malloc(sizeof(PCB));
if(!temp)
{
printf("ERROR");
exit(1);
}
temp->pid=i+1; //初始化进程的标识号
temp->pstatus=READY;//进程刚创建时,初始化其状态为"就绪态"
printf("\n请输入进程 %d 的优先级别 :",temp->pid);
scanf("%d",&temp->priority);//由用户输入进程的优先级并初始化,写入对应的PCB表
temp->next=NULL;
cur->next=temp;
cur=cur->next;
}
return head;//返回指向头结点的头指针
}
void Insert(PCB * head,PCB * cur)
{
if(cur==NULL)
return;
for(;head->next!=NULL;head=head->next);
cur->next=NULL;
head->next=cur;
}
PCB * readyTorun(PCB * head)
{
PCB * temp1=head,* temp=head->next;
if(head->next==NULL)
{
printf("The Ready is NUll!");
temp=NULL;
}
for(;head->next!=NULL;head=head->next)
{
if(temp->priority < head->next->priority)//当前优先级小于下一结点优先级
{
temp1=head;
temp=head->next;//temp记录选中的进程的PCB
}
}
if(temp!=NULL)
temp1->next=temp->next;//从就绪进程的PCB链表中去除选中进程的PCB
return temp;//返回该结点
}
void blockToready(PCB * ready,PCB * wait,int j)
{
PCB * temp1=wait,* temp=wait->next;
if(wait->next==NULL)
{
printf("The Wait is Null!");
}
for(;wait->next!=NULL;wait=wait->next)//寻找指定进程的PCB
{
if(wait->next->pid==j)
{
temp1=wait;
temp=wait->next;
}
}
if(temp!=NULL)
{
temp1->next=temp->next;//从等待PCB链表中去除选中的PCB
temp->pstatus=READY;//将选中进程的PCB状态改为就绪态
}
Insert(ready,temp);//向就绪PCB链表中插入选中的PCB
}
void print(PCB * head)
{
PCB * temp=head->next;
if(head->next==NULL)
{
printf("It is NULL");
return;
}
for(;temp!=NULL;temp=temp->next)
{
printf("p<%d> ",temp->pid);
}
}
void Display(PCB * r)
{
for(;r->next!=NULL;r=r->next)
{
printf("\np<%d> Priority: %d ",r->next->pid,r->next->priority);
}
}
int main()
{
int n,k=1,j;
struct PCB * ready_list=NULL,* wait_list=NULL,* run=NULL;
wait_list=(struct PCB *)malloc(sizeof(struct PCB));
wait_list->next=NULL;
printf("请输入准备创建的进程数 :");
scanf("%d",&n);
ready_list=Create(n);//根据用户输入,创建进程
run=readyTorun(ready_list);//从就绪PCB链表中选择优先级最高的进程,进入CPU运行
srand((unsigned)time(NULL));//产生一个随机种子
while(k!=0)
{
int i = rand() % 4 ;//在0-3中随机产生一个数,0表示退出
k=i;
printf("\n=================进程调度==================");
if(run!=NULL)
{
run->pstatus=RUNNING;
printf("\np<%d> is Running ......",run->pid);
}
printf("\nThe Ready : ");
print(ready_list);
printf("\nThe Wait : ");
print(wait_list);
switch(i)
{
case 1 :
run->pstatus=READY;
Insert(ready_list,run);//将当前运行态的进程转为就绪态
run=readyTorun(ready_list);//系统自动从就绪PCB链表选择高优先级进程投入运行态
break;
case 2 :
run->pstatus=WAIT;
Insert(wait_list,run);//将当前运行态的进程转为等待态
run=readyTorun(ready_list);//系统自动从就绪PCB链表选择高优先级进程投入运行态
break;
case 3 :
printf("\n输入等待态(Wait)转换为就绪态(Ready)的进程标识号 : ");//将指定的等待态进程转为就绪态
scanf("%d",&j);
blockToready(ready_list,wait_list,j);
break;
default :break;
}
}
//调度结束,输出创建进程的相关信息
printf("\n进程模拟调度结束\n");
Display(wait_list);
Display(ready_list);
printf("\np<%d> Priority: %d \n",run->pid,run->priority);
}