資源簡介
試驗名稱:進程調度模擬算法
試驗內容:設計一個有N個進程并行的進程調度程序。
調度算法:采用最高優先數優先的調度算法。每個進程由一個進程控制塊(PCB)表示。進程控制塊可以包含以下信息:進程名、優先數、到達時間、需要運行時間、已用CPU時間,進程狀態等。

代碼片段和文件信息
#include?“stdio.h“
#include?“stdlib.h“
#include?“conio.h“
#define?getpch(type)(type*)malloc(sizeof(type))
#define?NULL?0
struct??pcb
{
char?name[20];
char?state;
int?super;
int?ntime;
int?rtime;
struct?pcb?*link;
}*ready=NULL*p;
typedef?struct?pcb?PCB;
void?sort()?????/*??建立對進程進行優先級排列函數?*/
{
PCB?*first*second;
int?insert=0;
if?((ready==NULL)||(p->super)>(ready->super))
{
p->link=ready;
ready=p;
}
else
{
first=ready;
second=first->link;
while?(second!=NULL)
{
if?((p->super)>(second->super))
{
p->link=second;
first->link=p;
second=NULL;
insert=1;
}?
else
{
first=first->link;
second=second->link;
}
}
if?(insert==0)
{
first->link=p;
}
}
}
void?input()???/*??建立進程控制塊函數?*/
{
int??inum;
printf(“\n請輸入進程個數“);
scanf(“%d“&num);
for?(?i=0;i {
printf(“\n進程號NO.?%d:\n??“i);
p=getpch(PCB);
printf(“\n請輸入進程名“);
????scanf(“%s“p->name);
printf(“\n請輸入進程優先數“);
????scanf(“%d“&p->super);
printf(“\n請輸入進程運行時間“);
????scanf(“%d“&p->ntime);
printf(“\n“);
p->rtime=0;p->state=‘w‘;
p->link=NULL;
sort();
}
}
int?space()
{?
int?l=0;?PCB*?pr=ready;
while?(pr!=NULL)
{
l++;
pr=pr->link;
}
return(l);
}
void?disp(PCB?*?pr)???/*??建立進程顯示函數,用于顯示當前進程?*/
{
printf(“\n?qname?\t?state?\t?super?\t?ntime?\t?runtime?\n“);
printf(“|%s?\t“pr->name);
printf(“|%c?\t“pr->state);
printf(“|%d?\t“pr->super);
printf(“|%d?\t“pr->ntime);
printf(“|%d?\t“pr->rtime);
printf(“\n“);
}
void?check()??/*??建立進程查看函數?*/
{
PCB?*pr;
printf(“\n***當前正在進行的進程是:%s“p->name);
disp(p);
pr=ready;
printf(“\n***當前就緒隊列狀態為:\n“);
while?(pr!=NULL)
{
disp(pr);
pr=pr->link;
}
}
destroy()???/*??建立進程撤銷函數,進程運行結束,撤銷進程?*/
{
printf(““p->name);
free(p);
}
void?running()???/*??建立進程就緒函數?,進程運行時間到,置就緒狀態*/
{
(p->rtime)++;
if?(p->rtime==p->ntime)
{
destroy();
}?
else
{
(p->super)--;
p->state=‘w‘;
sort();
}
}
void??main()??????/*??主函數?*/
{
int?lenh=0;
char?ch;
input();
len=space();
while?((len!=0)&&(ready!=NULL))
{
ch=getchar();
h++;
printf(“\n?The?execute?number:%d“h);
p=ready;
ready=p->link;
p->link=NULL;
p->state=‘R‘;
check();
running();
printf(“\n請按任意鍵繼續。。。?!埃?
ch=getchar();
}
printf(“\n\n進程已經完成。\n“);
ch=getchar();
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????45056??2009-10-19?22:40??進程調度模擬算法\試驗名稱.進程調度模擬算法.doc
?????文件???????2592??2009-10-19?18:26??進程調度模擬算法\ProcessDisp.cpp
?????目錄??????????0??2009-10-27?19:10??進程調度模擬算法
-----------?---------??----------?-----??----
????????????????47648????????????????????3
- 上一篇:14芯和20芯的JTAG的原理圖設計
- 下一篇:gmapping論文
評論
共有 條評論