#include
#include
#include
#include
#include
#include
#include

#define NULL 0

/*-----代码中要用到数据结构---------*/
typedef struct MYPCB{
char pname[5];//进程名
int cputime;//进程周转时间
int needtime;//进程占用cpu时间
int pp;//进程优先级
char flags[10];//进程状态(ready,working,finish)
struct MYPCB *next;//指向下一个进程结构体pcb
int pkey;//进程关键字
int cyclingtime;//周转时间
int waittime;//等待时间
int temp_needtime;
}PCB;

int CPUTIME = 0;

/*-------函数声明部分-------------*/
PCB *init();//初始化链表函数的声明,返回链表的头指针
void cpu_sechuling(PCB *head);//cpu调度函数
void pkey_sort(PCB *head);//按正整数排序如pkey(小-->大)
void pp_sort(PCB *head);//按正整数排序如pp(大-->小)
void print(PCB *p);//打印链表函数
void accout(PCB *p);//计算周转时间和等待时间


PCB *init()
{//指针函数,返回值是一个指针。本质上是一个函数
PCB *head,*p,*q;//head用于返回的,p,q是用于控制流程的
int i,n;
q=NULL;
if((head = (PCB*)malloc(sizeof(PCB)))==NULL){//带有头结点的链表
printf("calling malloc funtion fialed!\n");
exit(1);
}
p = head;
head->next = NULL;

printf("How many process do you want running n:\t");

scanf("%d",&n);//可以自己确定输入几个进程进行验证优先级调度算法的
printf("\n");
printf("pname\tneedtime\tpp\tpkey(1,2...n)\n");
for(i=0;i {
if((q = (PCB*)malloc(sizeof(PCB)))==NULL){
printf("calling malloc funtion fialed!\n");
abort();
}
p->next = q;
scanf("%s",&q->pname);
scanf("%d",&q->needtime);
scanf("%d",&q->pp);
scanf("%d",&q->pkey);
q->cputime = 0;
q->cyclingtime =0;
q->waittime = 0;
q->temp_needtime = q->needtime;
strcpy(q->flags,"ready");
p = p->next;
q->next = NULL;
q = NULL;
}
if(i==n){
p=NULL;
free(p);
free(q);
}
return head;
}

void cpu_sechuling(PCB *head)
{
PCB *p;
p = head->next;
while(p->next != NULL){
if(p->needtime > 1){
p->cputime++;
p->needtime--;
p->pp--;
strcpy(p->flags,"working");

if((strcmp(p->next->flags,"ready")==0)&&(p->next->cputime>0)){
p->next->cputime++;
}
if(strcmp(p->next->flags,"working")==0){
p->next->cputime++;
strcpy(p->next->flags,"ready");
}
else
if((p->next->cputime <= 0)||(strcmp(p->next->flags,"finish")==0))
;//do nothing
Sleep(1000);
CPUTIME++;
printf("\nCPUTIME:%d\n",CPUTIME);
pp_sort(head);
print(head);
p = head->next;
}
if(p->needtime == 1){
p->cputime++;
p->needtime--;
p->pp--;
strcpy(p->flags,"finish");
if((strcmp(p->next->flags,"ready")==0)&&(p->next->cputime>0)){
p->next->cputime++;
}
if(strcmp(p->next->flags,"working")==0){
p->next->cputime++;
strcpy(p->next->flags,"ready");
}
else
if((strcmp(p->next->flags,"finish")==0)||(strcmp(p->next->flags,"finish")==0))
;//do nothing
Sleep(1000);
CPUTIME++;
printf("\nCPUTIME:%d\n",CPUTIME);
p->cyclingtime = CPUTIME;
pp_sort(head);
print(head);
p= head->next;
}
if(p->needtime == 0)//if(strcmp(p->flags,"finish")==0)
p= p->next;
else if(p->needtime < 0)
{
printf("You input process was wrong(needtime < 0)!\n");
exit(1);
}
}
}

void pkey_sort(PCB *head)
{//按pkey由小到大排序
PCB *p,*p1,*p2,*p3;
PCB rear;//用于尾部节点
p = head;
while(p->next != NULL){//p置于链表尾部
p = p->next;
}
p->next = &rear;
p = p->next;
while(head->next != p){
p1 = head;
p2 = p1->next;
p3 = p2->next;
while(p3 != p){
if(p2->pkey > p3->pkey){
p2->next = p3->next;
p3->next = p2;//p2中存有max_pkey
p1->next = p3;
p1 = p3;
p3 = p2->next;//p3始终在p2后面
}
else{
p1 = p2;
p2 = p3;
p3 = p3->next;
}
}
p = p2;
}
while(p->next != &rear){
p = p->next;
}
p->next = NULL;
}

void pp_sort(PCB *head)
{//按pp由大到小排序
PCB *p,*p1,*p2,*p3;
PCB rear;//用于尾部节点
p = head;
while(p->next != NULL){//p置于链表尾部
p = p->next;
}
p->next = &rear;
p = p->next;
while(head->next != p){
p1 = head;
p2 = p1->next;
p3 = p2->next;
while(p3 != p){
if(p2->pp < p3->pp){//加等号的类似老师那种情况
p2->next = p3->next;
p3->next = p2;//p2中存有min_pp
p1->next = p3;
p1 = p3;
p3 = p2->next;//p3始终在p2后面
}
else{
p1 = p2;
p2 = p3;
p3 = p3->next;
}
}
p = p2;
}
while(p->next != &rear){
p = p->next;
}
p->next = NULL;
}

void print(PCB *head)
{
PCB *q;
q = head->next;
printf("pname\tcputime\tneedtime\tpp\tflags\tpkey\n");
while(q){
printf("%s\t%d\t%d\t\t%d\t%s\t%d\n",q->pname,q->cputime,q->needtime,q->pp,q->flags,q->pkey);
q = q->next;
}
free(q);
}

void accout(PCB *head){
PCB *p;
p = head->next;
printf("\npname\tRound Time\tWaiting Time\n");
while(p != NULL)
{
printf("%s\t%d\t\t%d\n",p->pname,p->cyclingtime,p->cyclingtime-p->temp_needtime);
p = p->next;
}
free(p);
}

int main(){
system("color 1b");
PCB *head=NULL;
head = init();
Sleep(1000);
pkey_sort(head);
printf("\nCPUTIME:%d\n",CPUTIME);
CPUTIME--;
print(head);

pp_sort(head);
cpu_sechuling(head);

pkey_sort(head);
printf("\n");
print(head);
accout(head);
return 0;
}