操作系统实验报告之处理机管理.docx
《操作系统实验报告之处理机管理.docx》由会员分享,可在线阅读,更多相关《操作系统实验报告之处理机管理.docx(26页珍藏版)》请在冰豆网上搜索。
操作系统实验报告之处理机管理
试验一:
时间:
2013-11-29
计算机实验机房2
朱蓉蓉
第一题:
先来先服务算法
#include
#include
#include
usingnamespacestd;
structPCB
{
stringname;//进程名
floatta;//进程到达时间
floatts;//进程估计运行的时间
floattb;//进程开始运行时间
floattm;//进程仍需运行的时间
floatto;//进程完成的时间
floatrn;//进程运行的次数
floattotalTime;//周转时间
doubleweightTotalTime;//带权周转时间(周转时间/估计运行时间)
PCB*next;//定义指向下一个进程的指针
};
#defineMAX_NUM15
intpronum;//定义进程数为pronum
floattotal;//记录所有进程的总时间
doubleweight;//记录所有进程的带权周转时间
PCB*create(PCB*head);//创建进程队列
voiddeal(PCB*head);//FCFS记录处理
voidsort(PCB*head);//将进程按到达的先后顺序排列
voidfcfsrun(PCB*head);//先来先服务算法
PCB*create(PCB*head)
{
PCB*p1,*p2;
p1=p2=newPCB;
head=p1;
cout<<"请输入进程数:
";
cin>>pronum;
for(inti=0;i{
p2=p1;
p1=newPCB;
p1->next=NULL;
cout<<"请依次输入第"<
";
cin>>p1->name>>p1->ta>>p1->ts;
p1->tm=p1->ts;
p1->rn=1;
p2->next=p1;
}
returnhead;
}
voidsort(PCB*head)//将进程按到达的先后顺序排列
{
PCB*p,*q,*r,*s;
if(head->next!
=NULL)
{
p=head->next->next;
head->next->next=NULL;
}
while(p)
{
q=p;
p=p->next;
r=head;
s=head->next;
while(s&&s->ta<=q->ta)
{
r=s;
s=s->next;
}
r->next=q;
q->next=s;
}
}
voiddeal(PCB*head)//FCFS记录处理
{
sort(head);
PCB*p,*q;
q=head->next;
q->tb=q->ta;
q->to=q->tb+q->ts;
q->totalTime=q->to-q->ta;
q->weightTotalTime=q->totalTime/(double)q->ts;
total+=q->totalTime;
weight+=q->weightTotalTime;
p=q->next;
while(p!
=NULL)
{
p->tb=q->to;
p->to=p->tb+p->ts;
p->totalTime=p->to-p->ta;
p->weightTotalTime=p->totalTime/(double)p->ts;
total+=p->totalTime;
weight+=p->weightTotalTime;
q=p;
p=p->next;
}
}
voidfcfsrun(PCB*head)//先来先服务算法
{
deal(head);
PCB*p,*q,*s;
p=head->next;
cout<<"进程执行顺序为:
";
while(p!
=NULL)
{
cout<<"--"<name;
p=p->next;
}
cout<cout<<"进程名提交时间开始时间结束时间周转时间带权周转时间\n";
s=head->next;
while(s!
=NULL)
{
cout<name<ta<tb<to<totalTime<weightTotalTime<s=s->next;
}
cout<cout<<"平均周转时间:
"<cout<<"平均带权周转时间:
"<cout<<"******************************************************"<total=0;
weight=0;
}
voidmain()
{
cout<<"*E01114336-朱蓉蓉-先来先服务调度算法*"<PCB*head=NULL;
head=create(head);
fcfsrun(head);
}
第二题:
//优先级调度算法
#include
#include
#include
#definegetpch(type)(type*)malloc(sizeof(type))
#defineNULL0
structpcb{/*定义进程控制块PCB*/
charname[10];
charstate;
intsuper;
intntime;
intrtime;
structpcb*link;
}*ready=NULL,*p;
typedefstructpcbPCB;
voidsort()/*建立对进程进行优先级排列函数*/
{
PCB*first,*second;
intinsert=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;
}
}
voidinput()/*建立进程控制块函数*/
{
inti,num;
system("cls");/*清屏*/
printf("\n请输入进程数:
");
scanf("%d",&num);
for(i=1;i<=num;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();/*调用sort函数*/
}
}
intspace()
{
intl=0;
PCB*pr=ready;
while(pr!
=NULL)
{
l++;
pr=pr->link;
}
return(l);
}
voiddisp(PCB*pr)/*建立进程显示函数,用于显示当前进程*/
{
printf("\n进程名\t状态\t优先数\t需要运行时间\t已经运行时间\n");
printf("|%s\t",pr->name);
printf("|%c\t",pr->state);
printf("|%d\t",pr->super);
printf("|%d\t\t",pr->ntime);
printf("|%d\t",pr->rtime);
printf("\n");
}
voidcheck()/*建立进程查看函数*/
{
PCB*pr;
printf("\n****当前正在运行的进程是:
\n");/*显示当前运行进程*/
disp(p);
pr=ready;
printf("\n****当前就绪队列状态为:
\n");/*显示就绪队列状态*/
while(pr!
=NULL)
{
disp(pr);
pr=pr->link;
}
}
voiddestroy()/*建立进程撤消函数(进程运行结束,撤消进程)*/
{
printf("\n进程[%s]已完成.\n",p->name);
free(p);
}
voidrunning()/*建立进程就绪函数(进程运行时间到,置就绪状态*/
{
(p->rtime)++;
if(p->rtime==p->ntime)
destroy();/*调用destroy函数*/
else
{
(p->super)--;
p->state='W';
sort();/*调用sort函数*/
}
}
voidmain()/*主函数*/
{
intlen,h=0;
charch;
input();
len=space();
while((len!
=0)&&(ready!
=NULL))
{
ch=getchar();
h++;
printf("-----------------------------------------------------");
printf("\n现在是第%d次运行:
\n",h);
p=ready;
ready=p->link;
p->link=NULL;
p->state='R';
check();
running();
printf("\n按任意键继续......\n");
}
printf("\n\n进程已经完成.\n");
}
第三题:
按时间片轮法调度
#defineN10
#include
#include
typedefstructpcb
{
charpname[N];
intruntime;
intarrivetime;
charstate;
structpcb*next;
}PCB;
PCBhead_input;
PCBhead_run;
PCB*pcb_input;
staticcharR='r',C='c';
unsignedlongcurrent;
voidinputprocess();
intreadydata();
intrunprocess();
intreadyprocess();
FILE*f;
/*检查就绪队列并准备运行进程的函数*/
intreadyprocess()
{
while
(1)
{
if(readydata()==0)
return1;
else
runprocess();
}
}
/*判断就绪队列是否为空的函数*/
intreadydata()
{
if(head_input.next==NULL)
{
if(head_run.next==NULL)
return0;
else
return1;
}
PCB*p1,*p2,*p3;
p1=head_run.next;
p2=&head_run;
while(p1!
=NULL)
{
p2=p1;
p1=p2->next;
}
p1=p2;
p3=head_input.next;
p2=&head_input;
while(p3!
=NULL)
{
if(((unsignedlong)p3->arrivetime<=current)&&(p3->state==R))
{
printf("Timeis%4d;Process%sstart,\n",\
(current+500)/1000,p3->pname);
fprintf(f,"Timeis%4d;Process%sstart,\n",\
(current+500)/1000,p3->pname);
p2->next=p3->next;
p3->next=p1->next;
p1->next=p3;
p3=p2;
}
p2=p3;
p3=p3->next;
}
return1;
}
intrunprocess()
{
PCB*p1,*p2;
if(head_run.next==NULL)
{
current++;
return1;
}
else
{
p1=head_run.next;
p2=&head_run;
while(p1!
=NULL)
{
p1->runtime--;
current++;
if(p1->runtime<=0)
{
printf("Timeis%4d;Process%send.\n",(current+500)/1000,p1->pname);
fprintf(f,"Timeis%4d;Process%send.\n",(current+500)/1000,p1->pname);
p1->state=C;
p2->next=p1->next;
deletep1;
p1=NULL;
}
else
{
p2=p1;
p1=p2->next;
}
}
return1;
}
}
voidinputprocess()
{
PCB*p1,*p2;
intnum;
printf("Howmanyprocessesdoyouwanttorun:
");
fprintf(f,"Howmanyprocessesdoyouwanttorun:
");
scanf("%d",&num);
fprintf(f,"%d\n",&num);
p1=&head_input;
p2=p1;
p1->next=newPCB;
p1=p1->next;
for(inti=0;i{
printf("InputNO.%3dprocesspname:
",i+1);
fprintf(f,"InputNO.%3dprocesspname:
",i+1);
scanf("%s",p1->pname);
fprintf(f,"%s\n",p1->pname);
printf("runtime:
");
fprintf(f,"runtime:
");
scanf("%d",&(p1->runtime));
fprintf(f,"%d\n",&(p1->runtime));
printf("arrivetime:
");
fprintf(f,"arrivetime:
");
scanf("%d",&(p1->arrivetime));
fprintf(f,"%d\n",&(p1->arrivetime));
p1->runtime=(p1->runtime)*1000;
p1->arrivetime=(p1->arrivetime)*1000;
p1->state=R;
p1->next=newPCB;
p2=p1;
p1=p1->next;
}
deletep1;
p1=NULL;
p2->next=NULL;
}
/*主函数*/
voidmain()
{
f=fopen("result.txt","w");
printf("\ntimel=1000timeslice\n");
fprintf(f,"\ntimel=1000timeslice\n");
current=0;
inputprocess();
readyprocess();
getch();
fclose(f);
}