-
大小: 504KB文件類型: .rar金幣: 2下載: 0 次發(fā)布日期: 2021-05-21
- 語言: C/C++
- 標(biāo)簽: 操作系統(tǒng)??進(jìn)程管理??進(jìn)程調(diào)度??C語言??
資源簡介
操作系統(tǒng)課程實(shí)驗(yàn)-利用C語言編寫的進(jìn)程管理和進(jìn)程調(diào)度任務(wù)。包括源程序和實(shí)驗(yàn)報(bào)告。

代碼片段和文件信息
#include?“stdio.h“
#include??
#include??
#define?getpch(type)?(type*)malloc(sizeof(type))?
#define?NULL?0?
struct?pcb?{?/*?定義進(jìn)程控制塊PCB?*/?
char?name[10];?
char?state;?
int?super;?
int?ntime;?
int?rtime;?
struct?pcb*?link;?
}*ready=NULL*p;?
typedef?struct?pcb?PCB;?
void?sort()?/*?建立對(duì)進(jìn)程進(jìn)行優(yōu)先級(jí)排列函數(shù)*/?
{?
PCB?*first?*second;?
int?insert=0;?
if((ready==NULL)||((p->super)>(ready->super)))?/*優(yōu)先級(jí)最大者插入隊(duì)首*/?
{?
p->link=ready;?
ready=p;?
}?
else?/*?進(jìn)程比較優(yōu)先級(jí)插入適當(dāng)?shù)奈恢弥?/?
{?
first=ready;?
second=first->link;?
while(second!=NULL)?
{?
if((p->super)>(second->super))?/*若插入進(jìn)程比當(dāng)前進(jìn)程優(yōu)先數(shù)大*/?
{?/*插入到當(dāng)前進(jìn)程前面*/?
p->link=second;?
first->link=p;?
second=NULL;?
insert=1;?
}?
else?/*?插入進(jìn)程優(yōu)先數(shù)最低則插入到隊(duì)尾*/?
{?
first=first->link;?
second=second->link;?
}?
}?
if(insert==0)?first->link=p;?
}?
}??
void?input()?/*?建立進(jìn)程控制塊函數(shù)*/?
{?
int?inum;?
printf(“\n?input?process?number?“);
scanf(“%d“&num);?
for(i=0;i {?
printf(“\n?input?the?process?No.%d:\n“i);
p=getpch(PCB);?
printf(“\n?the?name?of?Process:“);
scanf(“%s“p->name);?
printf(“\n?the?super?of?Process:“);
scanf(“%d“&p->super);?
printf(“\n?the?time?need:“);
scanf(“%d“&p->ntime);?
printf(“\n“);?
p->rtime=0;p->state=‘w‘;?
p->link=NULL;?
sort();?/*?調(diào)用sort函數(shù)*/?
}?
}?
int?space()?
{?
int?l=0;?PCB*?pr=ready;?
while(pr!=NULL)?
{?
l++;?
pr=pr->link;?
}?
return(l);?
}?
void?disp(PCB?*?pr)?/*建立進(jìn)程顯示函數(shù)用于顯示當(dāng)前進(jìn)程*/?
{?
printf(“\n?qname?\t?state?\t?super?\t?ndtime?\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()?/*?建立進(jìn)程查看函數(shù)?*/?
{?
PCB*?pr;?
printf(“\n?****?now?the?process?runing?is:%s“p->name);?/*顯示當(dāng)前運(yùn)行進(jìn)程*/
disp(p);?
pr=ready;?
printf(“\n?****now?the?ready?quenue?is:\n“);?/*顯示就緒隊(duì)列狀態(tài)*/
while(pr!=NULL)?
{?
disp(pr);?
pr=pr->link;?
}?
}?
void?destroy()?/*建立進(jìn)程撤消函數(shù)(進(jìn)程運(yùn)行結(jié)束撤消進(jìn)程)*/?
{?
printf(“\n?process?[%s]?has?finished.\n“p->name);
free(p);?
}?
void?running()?/*?建立進(jìn)程就緒函數(shù)(進(jìn)程運(yùn)行時(shí)間到置就緒狀態(tài)*/?
{?
(p->rtime)++;?
if(p->rtime==p->ntime)?
destroy();?/*?調(diào)用destroy函數(shù)*/?
else?
{?
(p->super)--;?
p->state=‘w‘;?
sort();?/*調(diào)用sort函數(shù)*/?
}?
}?
void?main()?/*主函數(shù)*/?
{?
int?lenh=0;?
char?ch;?
input();?
len=space();?
while((len!=0)&&(ready!=NULL))?
{?
ch=getchar();?
h++;?
printf(“\n?The?execute?number:%d?\n“h);?
p=ready;?
ready=p->link;?
p->link=NULL;?
p->state=‘R‘;?
check();?
running();?
printf(“\n?press?any?key?to?continue.“);
ch=getchar();?
}?
printf(“\n\n?process?is?over.\n“);
ch=getchar();?
}
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????2808??2010-04-29?12:35??操作系統(tǒng)實(shí)驗(yàn)\os02.c
?????文件?????653824??2011-12-28?18:05??操作系統(tǒng)實(shí)驗(yàn)\操作系統(tǒng)實(shí)驗(yàn)報(bào)告?梅蘢.doc
?????目錄??????????0??2010-04-29?12:35??操作系統(tǒng)實(shí)驗(yàn)
-----------?---------??----------?-----??----
???????????????656632????????????????????3
評(píng)論
共有 條評(píng)論