静态优先级调度算法Word下载.docx
《静态优先级调度算法Word下载.docx》由会员分享,可在线阅读,更多相关《静态优先级调度算法Word下载.docx(16页珍藏版)》请在冰豆网上搜索。
由自己设计。
三、实验原理及步骤
根据现代操作系统的特征
1.并发性(concurrence);
2.共享性(sharing);
3.虚拟性(virtual);
4.异步性(asynchronism)。
模拟出进程在执行中的状态变化过程;
体会进程申请资源、使用资源、归还资源;
体会死锁。
步骤(参考框图)
4、算法和流程图
可强占优先调度算法实现过程流程图(如下图):
四、程序运行
1选择输入执行程序(如下图)
2可强占优先调度算法图(如下图)
五.设计总结:
通过该课程设计,加深了对系统进程调度机制的理解。
在抢占方式中实践了“抢占”必须遵循的原则:
优先权原则。
认识了几种进程调度算法的优缺点以及应用范围。
加强C++的编程能力,实现类的封
装。
附录:
程序及注释(用红色黑体标注自己设计的函数)
//进程PCB类和模拟cpu的进程类的声明
#include<
stdio.h>
stdlib.h>
windows.h>
conio.h>
time.h>
io.h>
string.h>
#defineMAX_PHILOSOPHERS3//待测试的哲学家数
#defineZERO48//数字0的ASCII码
#defineDELAYrand()%25
structPCB
{
charp_name[20];
intp_priority;
intp_needTime;
intp_runTime;
charp_state;
chardeadlock();
structPCB*next;
};
voidHighPriority();
voiddeadlock();
voidInformation();
//形参的改变映射给实参说白了就是实参传过去不用return返回就可以把实参改变
charChoice();
structPCB*SortList(PCB*HL);
intmain(intargc,char*argv[])
Information();
charchoice=Choice();
switch(choice)
{
case'
1'
:
system("
cls"
);
HighPriority();
break;
2'
voiddeadlock();
default:
}
pause"
return0;
}
voidInformation()
printf("
\n\n"
*********************************************\n"
模拟进程调度算法\n"
*********************************************\n\n\n"
静态优先级调度算法"
死锁问题"
按回车键进入演示程序"
getchar();
charChoice()
进程调度演示\n"
1.演示最高优先数优先算法。
\n"
2.演示死锁问题。
3.退出程序。
\n\n\n\n"
选择进程调度方法:
"
selectafunction(1~3):
charch=getchar();
returnch;
voidHighPriority()
structPCB*processes,*pt;
//pt作为临时节点来创建链表
processes=pt=(structPCB*)malloc(sizeof(structPCB));
for(inti=1;
i!
=4;
++i)
structPCB*p=(structPCB*)malloc(sizeof(structPCB));
进程号No.%d:
i);
输入进程名:
scanf("
%s"
p->
p_name);
输入进程优先数:
%d"
&
p->
p_priority);
输入进程运行时间:
p_needTime);
p->
p_runTime=0;
p_state='
W'
;
next=NULL;
pt->
next=p;
pt=p;
//接受回车
//processes作为头结点来存储链表
processes=processes->
next;
intcases=0;
structPCB*psorted=processes;
while
(1)
++cases;
pt=processes;
//对链表按照优先数排序
//psorted用来存放排序后的链表
psorted=SortList(psorted);
Theexecutenumber:
%d\n\n"
cases);
****当前正在运行的进程是:
%s\n"
psorted->
psorted->
R'
qnamestatesuperndtimeruntime\n"
%s\t%c\t%d\t%d\t%d\t\n\n"
p_name,psorted->
p_state,psorted->
p_priority,psorted->
p_needTime,psorted->
p_runTime);
p_runTime++;
p_priority--;
****当前就绪状态的队列为:
//pt指向已经排序的队列
pt=psorted->
while(pt!
=NULL)
pt->
p_name,pt->
p_state,pt->
p_priority,pt->
p_needTime,pt->
pt=pt->
//pt指向已经排序的链表,判断链表是否有已用时间啊等于需要时间的
pt=psorted;
structPCB*ap;
ap=NULL;
//ap指向pt的前一个节点
if(pt->
p_needTime==pt->
p_runTime)
if(ap==NULL)
psorted=pt;
else
ap->
next=pt->
ap=pt;
if(psorted->
next==NULL)
structPCB*SortList(PCB*HL)
structPCB*SL;
SL=(structPCB*)malloc(sizeof(structPCB));
SL=NULL;
structPCB*r=HL;
while(r!
structPCB*t=r->
structPCB*cp=SL;
structPCB*ap=NULL;
while(cp!
if(r->
p_priority>
cp->
p_priority)
ap=cp;
cp=cp->
r->
next=SL;
SL=r;
next=cp;
next=r;
r=t;
returnSL;
//
HANDLEh_mutex_chopsticks[MAX_PHILOSOPHERS];
//互斥体数组,每根筷子需要一个互斥体
intthread_number[MAX_PHILOSOPHERS]={1,2,3};
//定义死锁的个数
//会产生死锁的哲学家线程
intdeadlock_philosopher(LPVOIDdata){
intphilosopher_number=*(int*)(data);
//哲学家编号
for(;
)
srand((unsigned)time(NULL)*(philosopher_number+1));
Sleep(DELAY);
printf("
%s%c%s%c\n"
"
Philosopher"
ZERO+philosopher_number,"
iswaitingchopstick"
(ZERO+philosopher_number));
WaitForSingleObject(h_mutex_chopsticks[philosopher_number],INFINITE);
gotchopstick"
Sleep(DELAY/4);
printf("
(ZERO+(1+philosopher_number)%MAX_PHILOSOPHERS));
WaitForSingleObject(h_mutex_chopsticks[((1+philosopher_number)%MAX_PHILOSOPHERS)],INFINITE);
%s%c%s\n"
iseating."
ReleaseMutex(h_mutex_chopsticks[philosopher_number]);
releasedchopstick"
ZERO+philosopher_number);
ReleaseMutex(h_mutex_chopsticks[(1+philosopher_number)%MAX_PHILOSOPHERS]);
Sleep(DELAY);
}//endfor
//死锁时的初始化程序
voiddeadlock()
charchoice;
inti=0;
HANDLEh_thread[MAX_PHILOSOPHERS];
可能出现死锁的哲学家就餐问题\n"
for(i=0;
i<
MAX_PHILOSOPHERS;
i++)
h_mutex_chopsticks[i]=CreateMutex(NULL,FALSE,NULL);
};
h_thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(deadlock_philosopher),&
thread_number[i],0,NULL);
WaitForMultipleObjects(MAX_PHILOSOPHERS,h_thread,TRUE,-1);
do
choice=(char)getch();
}while(choice!
='
deadlock();
\nPressanykeytoreturntomainmenu."
getch();
//通过按序分配资源预防死锁的哲学家线程
intordered_allocation_philosopher(LPVOIDdata)
if(philosopher_number==MAX_PHILOSOPHERS-1){
printf("
WaitForSingleObject(h_mutex_chopsticks[(1+philosopher_number)%MAX_PHILOSOPHERS],INFINITE);
Sleep(DELAY/4);
}
else{
ZERO+(1+philosopher_number)%MAX_PHILOSOPHERS);
isreleasingchopstick"
//通过按序分配资源预防死锁的初始化程序
voidordered_allocation()
i++){
h_thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(ordered_allocation_philosopher),&