資源簡介
進程調度模擬程序:假設有10個進程需要在CPU上執行,分別用:先進先出調度算法、基于優先數的調度算法、最短執行時間調度算法確定這10個進程在CPU上的執行過程。要求每次進程調度時在屏幕上顯示:當前執行進程、就緒隊列、等待隊列。
實現了三種方法,純自己開發,使用鏈表實現,無bug。
代碼片段和文件信息
#?include
#?include
#?include
#?define?N?10
#?define?num_name?10
/*
輸入:
Wechat?BaoFeng?Baidu?Sougou?Google?Taobao?WPS?QQ?Baiduyun?360
或輸入:
0?1?2?3?4?5?6?7?8?9
*/
typedef?struct?PCB//創建進程PCB塊
{
????char?name[num_name];//名稱
????int?stage;??????????//狀態
????int?priority;???????//優先級
????int?time;???????????//運行時間
}PCB;
typedef?struct?Node//創建隊列結點
{
????PCB?data;
????struct?Node?*?next;
}QNode;
typedef?struct?Queue
{
????QNode?*?front;//隊列頭結點
????QNode?*?rear;//隊列尾結點
}linkQueue;
int?InitQueue(linkQueue?*?q)//鏈表初始化
{
????q->front=q->rear=?(QNode?*)malloc(sizeof(QNode));
????if(!q->front)
????????exit(0);
????q->rear->next=NULL;
????return?1;
}
void?Pop(linkQueue?*?q)//隊頭元素出隊
{
????if(q->front->next==NULL)?exit(0);
????QNode?*?p;
????p?=?q->front->next;
????q->front->next?=?q->front->next->next;
}
void?Pop_Item(linkQueue?*?q?QNode?*?t)//特定元素出隊
{
????if(q->front->next==NULL)?exit(0);
????QNode?*?p?*f;
????f?=?q->front;
????for(p=q->front->next;p!=NULL;p=p->next)
????{
????????if(!strcmp(t->data.name?p->data.name))
????????{
????????????f->next=f->next->next;
????????????return;
????????}
????????f=f->next;
????}
}
int?Print(linkQueue?*?q1?linkQueue?*?q2?QNode?*?q)//輸出隊列
{
????printf(“當前執行進程是:\n“);
????//printf(“%s?“?q->data.name);
????printf(“{?名稱:%8s??狀態:%2d??優先數:%2d??時間:%3d}?\n“?q->data.nameq->data.stage?q->data.priority?q->data.time);
????putchar(‘\n‘);
????QNode?*?p;
????printf(“就緒隊列是:\n“);
????p?=?q1->front->next;
????while(p?!=?NULL)
????{
????????if(strcmp(p->data.name?q->data.name))
????????printf(“{?名稱:%8s??狀態:%2d??優先數:%2d??時間:%3d}?\n“?p->data.namep->data.stage?p->data.priority?p->data.time);
????????p?=?p->next;
????}
????putchar(‘\n‘);
????printf(“等待隊列是:\n“);
????p?=?q2->front->next;
????while(p?!=?NULL)
????{
????????//printf(“%s?“?p->data.name);
????????printf(“{?名稱:%8s??狀態:%2d??優先數:%2d??時間:%3d}?\n“?p->data.namep->data.stage?p->data.priority?p->data.time);
????????p?=?p->next;
????}
????putchar(‘\n‘);
????putchar(‘\n‘);
}
void?EnQueue(linkQueue?*?q?PCB?*?a)//元素入隊
{
????//printf(“%s?“a->name);
????QNode?*?p;
????p?=?(QNode?*)malloc(sizeof(QNode));
????strcpy(p->data.namea->name);
????p->data.stage?=?a->stage;
????p->data.priority?=?a->priority;
????p->data.time?=?a->time;
????p->next?=?NULL;
????if(q->front==NULL)//首結點為空,放入頭結點后
????{
????????q->front?=?p;
????????q->rear?=?q->front;
????}
????else//頭結點不為空,放入尾結點后
????{
????????q->rear->next?=?p;
????????q->rear?=?q->rear->next;
????}
????//printf(“%s?%d“?q.rear->data.name?q.rear->data.stage);
}
PCB?FIFO(linkQueue?*?q_ready)//先進先出調度算法
{
????PCB?p;
????p?=?q_ready->front->next->data;
????return?p;
}
PCB?Priority(linkQueue?*?q_ready)//基于優先數的調度算法
{
????QNode?*?p?*t;
????t?=?q_ready->front->next;
????for(p=q_ready->front->next;p!=NULL;p=p->next)
????{
????????if(p->data.priority?>?t->data.priority)//調用優先數最大的
?????
評論
共有 條評論