資源簡介
進程調度模擬程序:假設有10個進程需要在CPU上執行,分別用:
? 先進先出調度算法;
? 基于優先數的調度算法;
? 最短執行時間調度算法
確定這10個進程在CPU上的執行過程。要求每次進程調度時在屏幕上顯示:
? 當前執行進程;
? 就緒隊列;
? 等待隊列

代碼片段和文件信息
#include?
#include?
#include?
#include?
struct?PCB //定義一個進程結點數據域
{
int?name; //進程名
int?run_time; //運行時間
int?level; //優先數
char?state; //運行狀態
struct?PCB?*next; //指向下一個結點的指針
struct?PCB?*pre;????????????????//指向前一個節點的指針
};
struct?PCB?*PcbPoint[10];//申明指針
struct?PCB?*ReadyHead?*ReadyTail?*WaitHead?*WaitTail;
int?classNum;
void?displayProcess() //用于輸出當前鏈表中各結點的狀態
{
struct?PCB?*temp;
temp?=?ReadyHead;
while?(temp?!=?0?)
{
printf(“??Ready?p?%d“?temp->name);???
temp?=?temp->next; //實現往后移
}
????temp?=?WaitHead;
printf(“?\n“);???
while?(temp?!=?0?)
{
printf(“??Wait?p?%d“?temp->name);???
temp?=?temp->next; //實現往后移
}
???printf(“?\n“);
}
struct?PCB?*?shedule()//根據優先級調度
{??
struct?PCB?*retValue;
struct?PCB?*temp;
????retValue?=?ReadyHead;
temp?=?ReadyHead;
while?(temp?!=?0)
{
if?(temp->level?>?retValue->level)
{
retValue=temp;
}
temp=temp->next;
}
if?((retValue?==?ReadyHead)?&&?(retValue==ReadyTail))
{
ReadyTail->next=ReadyHead->next=ReadyTail->pre=ReadyHead->pre?=?NULL;
return?0;
}
if?(retValue?==?ReadyTail?&&?(retValue?!=?ReadyHead))
{
ReadyTail?=?retValue->pre;//調整隊列尾部
ReadyTail->next?=?NULL;
}
else?if?(retValue?==?ReadyHead?&&?(retValue?!=?ReadyTail)?)
{
ReadyHead=?retValue->next;
ReadyHead->pre?=?NULL;
}
else
{
retValue->pre->next=retValue->next;
retValue->next->pre=retValue->pre;
}
return?retValue;
}
int?carryOut?(struct?PCB?*p)
{
int?temp;
if?(NULL?==?p)
return?0;
???
????????printf(“?\n“);???
printf(“??Runing?process?%d“?p->name);???
printf(“??????run?time??%d“?p->run_time);???
????????printf(“??????level??%d\n“?p->level);???
?
?????temp?=?rand()%2;
?if?(temp?==?1)??//該隨機數為時,將等待隊列中的第一個PCB加入就緒隊列的對尾;
?{
?if?(ReadyTail?!=NULL?&&?(WaitHead?!=?NULL))?
?{
???????????ReadyTail->next=WaitHead;
???if?(WaitHead?!=?NULL)
???WaitHead->pre?=?ReadyTail;
????????????ReadyTail=WaitHead;
?}
?if?(WaitHead?!=?NULL)
?????????WaitHead=WaitHead->next;
???????if?(ReadyTail?!=NULL)
?ReadyTail->next=NULL;
?};
?temp?=?rand()%20?+1;//表示執行進程已經執行的時間;約為50毫秒
?
?if?(temp?run_time)?//沒有執行完
?{
?????????????p->run_time=p->run_time?-?temp;
????????temp?=?rand()%2;
????????if?(0?==temp?)?//?加到就緒隊列隊尾
????????{
??????ReadyTail->next=p;
??p->pre?=ReadyTail;
??????ReadyTail=p;
????????}
????????else???//?加到等待隊列隊尾
?? ????????{
WaitTail->next=p;
WaitTail=p;
????????};
?}
??????else//已經執行完了
?????????printf(“??process?%d?is?run?over\n“?p->name);
?if?(0==temp)
?ReadyTail->next=NULL;
?else
?WaitTail->next=NULL;
?????displayProcess();
?return?1;
};
void?creat() //創建一個函數,用于返回一個鏈表
{
int?i?=?1;
for?(i?=?0;?i10;?i++)?
{
PcbPoint[i]?=?(struct?PCB?*)malloc(sizeof(struct?PCB)); //給PcbPoint指針分配內存
????PcbPoint[i]->name=?i;
P
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????4183??2012-11-23?17:15??lab1.c
-----------?---------??----------?-----??----
?????????????????4183????????????????????1
- 上一篇:擾動觀測器
- 下一篇:紅米3S基帶.qcn
評論
共有 條評論