标签:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <malloc.h> 5 #include <windows.h> 6 #define NUM 3 7 struct pcb 8 { 9 char name[5];//进程名 10 int value;//优先数 11 int m_time;//需要运行时间 12 int r_time;//已运行时间 13 char condition[100];//状态 14 struct pcb *next; 15 }; 16 struct pcb PCB[NUM],center; 17 struct pcb *headc; 18 int count = NUM; 19 void Initialize()/*初始化*/ 20 { 21 int i; 22 for(i=0;i<NUM;i++) 23 { 24 printf("请输入第%d个进程名:\n",i+1); 25 scanf("%s",&PCB[i].name); 26 printf("请输入第%d个进程的需要运行时间:\n",i+1); 27 scanf("%d",&PCB[i].m_time); 28 printf("请输入第%d个进程的优先数:\n",i+1); 29 scanf("%d",&PCB[i].value); 30 PCB[i].r_time=0; 31 strcpy(PCB[i].condition,"Ready"); 32 } 33 } 34 void valueChange(struct pcb *p) 35 { 36 int i = 0; 37 while(p!=NULL) 38 { 39 PCB[i] = *p; 40 p=p->next; 41 i++; 42 } 43 count = i; 44 } 45 struct pcb* Sort()/*按优先级排序(高到低)*/ 46 { 47 struct pcb *head,*q,*p1; 48 int i,j; 49 for(i=0;i<count;i++) 50 { 51 for(j=i+1;j<count;j++) 52 { 53 if(PCB[i].value<PCB[j].value) 54 { 55 center=PCB[i]; 56 PCB[i]=PCB[j]; 57 PCB[j]=center; 58 } 59 } 60 } 61 head=(struct pcb*)malloc(sizeof(struct pcb)); 62 p1=head; 63 for(i=0;i<count;i++) 64 { 65 q=(struct pcb*)malloc(sizeof(struct pcb)); 66 strcpy(q->name,PCB[i].name); 67 strcpy(q->condition,PCB[i].condition); 68 q->m_time=PCB[i].m_time; 69 q->r_time=PCB[i].r_time; 70 q->value=PCB[i].value; 71 q->next=NULL; 72 p1->next=q; 73 p1=q; 74 } 75 return head; 76 //p=head->next; 77 } 78 void PrintReady() 79 { 80 struct pcb* p; 81 p=headc->next; 82 if(p!=NULL) 83 { 84 printf("就绪队列信息:\n "); 85 printf("进程名\t优先数\t已运行时间\t还需运行时间\t进程状态\n"); 86 while(p!=NULL) 87 { 88 printf("%s\t%d\t%d\t\t%d \t\t%s\n",p->name,p->value,p->r_time,p->m_time,p->condition); 89 p=p->next; 90 } 91 } 92 } 93 94 95 int Rule() 96 { 97 struct pcb* p; 98 p=headc->next; 99 while(1) 100 { 101 if(p->r_time==p->m_time) 102 { 103 strcpy(p->condition,"Run"); 104 printf("正在运行的进程为:%s\n",p->name); 105 printf("进程名\t优先数\t已运行时间\t还需运行时间\t进程状态\n"); 106 printf("%s\t%d\t%d\t\t%d \t\t%s\n",p->name,p->value,p->r_time,p->m_time,p->condition); 107 strcpy(p->condition,"Finish"); 108 p=p->next; 109 if(p==NULL) 110 { 111 printf("所有进程结束!\n"); 112 return 0; 113 } 114 } 115 else 116 { 117 printf("正在运行的进程为:%s\n",p->name); 118 // printf("正在运行的进程的状态为:\n"); 119 strcpy(p->condition,"Run"); 120 printf("进程名\t优先数\t已运行时间\t还需运行时间\t进程状态\n"); 121 printf("%s\t%d\t%d\t\t%d \t\t%s\n",p->name,p->value,p->r_time,p->m_time,p->condition); 122 Sleep(2000); 123 if(p->next==NULL) 124 { 125 while(1) 126 { 127 p->value--; 128 p->r_time++; 129 if(p->r_time!=p->m_time+1) 130 { 131 strcpy(p->condition,"Run"); 132 printf("正在运行的进程为:%s\n",p->name); 133 printf("进程名\t优先数\t已运行时间\t还需运行时间\t进程状态\n"); 134 printf("%s\t%d\t%d\t\t%d \t\t%s\n",p->name,p->value,p->r_time,p->m_time,p->condition); 135 Sleep(2000); 136 } 137 else 138 { 139 printf("所有进程结束!\n"); 140 return 0; 141 } 142 143 144 } 145 } 146 else if(p->value<p->next->value&&p->next!=NULL) 147 { 148 valueChange(p); 149 p = Sort()->next; 150 } 151 } 152 p->value--; 153 p->r_time++; 154 } 155 return 0; 156 } 157 void main() 158 { 159 Initialize(); 160 // p2=(struct pcb *)malloc(sizeof(struct pcb)); 161 headc = Sort(); 162 PrintReady(); 163 Rule(); 164 }
标签:
原文地址:http://www.cnblogs.com/VernSean/p/4522009.html