
处理器同一个时间只能处理一个任务。处理器在处理多任务的时候,就要看请求的时间顺序,如果时间一致,就要进行预测。挑到一个任务后,需要若干步骤才能做完,这些步骤中有些需要处理器参与,有些不需要(如磁盘控制器的存储过程)。不需要处理器处理的时候,这部分时间就要分配给其他的进程。原来的进程就要处于等待的时间段上。经过周密分配时间,宏观上就象是多个任务一起运行一样,但微观上是有先后的,就是时间片轮换。
//参考一下
#include <stdioh>
#include <stdlibh>
#define CPU_TIME 50 //CPU时间片
struct mission //单个任务的结构体
{
char name[20]; //任务名称
int finished; //任务是否已完成,完成为1,未完成为0
int need_time; //任务总共需要的CPU时间
int finished_time;//任务已经执行的CPU时间
};
int work_mission=5; //未完成任务计数
struct mission s[5]={{"one",0,100,0}, //假设有5个任务
{"two",0,380,0},
{"three",0,200,0},
{"four",0,440,0},
{"five",0,230,0}
};
int move_mission(int flag) //任务排序
{
if(work_mission==0)
{
return 1;
}
struct mission temp;
temp=s[0];
int i;
if(flag==1)
{
for(i=1;i<work_mission+1;i++)
{
s[i-1]=s[i];
}
s[work_mission]=temp;
}
else
{
for(i=1;i<work_mission;i++)
{
s[i-1]=s[i];
}
s[work_mission-1]=temp;
}
return 0;
}
int main(int argc, char argv[]) {
struct mission temp;
int finished=0;
int cpu_time_count=0;
while(finished==0)
{
printf("\n第%d个CPU时间片即将执行\n",cpu_time_count/CPU_TIME+1);
printf("\n当前任务队列(即将执行下面第一行任务,执行后将任务移到队列末尾): CPU已用时:%d\n",cpu_time_count);
int i;
for(i=0;i<5;i++)
{
printf("任务名称:%5s 是否已完成:%d 需时间:%03d 已执行时间:%03d 已完成:%032f%%\n",s[i]name,s[i]finished,s[i]need_time,s[i]finished_time,100((float)s[i]finished_time/(float)s[i]need_time));
}
if(s[0]finished==1)
{
finished=1;
break;
}
if((s[0]need_time-s[0]finished_time)>CPU_TIME)
{
s[0]finished_time+=CPU_TIME;
cpu_time_count+=CPU_TIME;
finished=move_mission(0);
}
else
{
cpu_time_count+=(s[0]need_time-s[0]finished_time);
s[0]finished_time=s[0]need_time;
s[0]finished=1;
work_mission-=1;
finished=move_mission(1);
}
}
printf("\n当前任务队列(全部执行完毕): CPU用时:%d\n",cpu_time_count);
int i;
for(i=0;i<5;i++)
{
printf("任务名称:%5s 是否已完成:%d 需时间:%03d 已执行时间:%03d 已完成:%032f%%\n",s[i]name,s[i]finished,s[i]need_time,s[i]finished_time,100((float)s[i]finished_time/(float)s[i]need_time));
}
printf("\n\n总共用了CPU时间: %d",cpu_time_count);
return 0;
}
试试这个,以前我编的时间片轮转
#include<iostreamh>
typedef struct node
{
char name [10];
struct node next;
int needtime;
int servetime;
char state;
};
void IniPCB(int n,node &head,node &finish)
{
node ready=new struct node;
ready=NULL;
for(int i=0;i<n;i++)
{
node p=new struct node;
cout<<"输入进程名:";
cin>>p->name;
cout<<"输入进程需要运行的时间:";
cin>>p->needtime;
p->servetime=0;
p->state='R';
if(ready==NULL)
{
ready=p;
head=p;
}
else
{
ready->next=p;
ready=p;
}
finish=p;
}
finish->next=head;
}
void DoPCB(int n,node &head,node &finish,node &run)
{
run=head;
bool end=0;
node check=new struct node;
cout<<"执行结果如下:"<<endl;
cout<<"进程名"<<" "<<"运行时间"<<" "<<"需要时间"<<" "<<"状态"<<endl;
for(int j=0;j<n;j++)
{
cout<<" "<<head->name<<" "<<head->servetime<<" "<<head->needtime<<" "<<head->state<<endl;
head=head->next;
}
cout<<endl;
int count=0;
while(end!=1)
{
if(run->state=='R')
{
(run->servetime)++;
if((run->servetime)==(run->needtime))
{
run->state='E';
}
count++;
cout<<"第"<<count<<"个时间片:"<<endl;
head=finish->next;
for(int k=0;k<n;k++)
{
cout<<" "<<head->name<<" "<<head->servetime<<" "<<head->needtime<<" "<<head->state<<endl;
head=head->next;
}
cout<<endl;
}
check=run;
for(int x=0;x<n;x++)
{
if(check->state=='E')
check=check->next;
}
if(check==run&&run->state=='E')
{
end=1;
}
run=run->next;
}
}
void main()
{
cout<<"输入进程个数:";
int n;
cin>>n;
node run=new struct node;
node head=new struct node;
node finish=new struct node;
run=NULL;
head=NULL;
finish=NULL;
IniPCB(n,head,finish);
DoPCB(n,head,finish,run);
}
cpu进程调度模拟四个队列是优先级队列(优先级高的在前面)可以使用结构体做队列的节点时间片轮转就是指定一个执行时间,时间一到就处理下一个进程,将当前进程进行状态转换(也就是换着状态需要向其放入相应的队列中,优先级队列)理清头绪的话程序不算太复杂但也很麻烦
在早期的时间片轮转法中,系统将所有的就绪进程按先来先服务的原则,排成一个队列,每次调度时,把CPU分配给队首进程,并令其执行一个时间片。时间片的大小从几ms到几百ms。当执行的时间片用完时,由一个计时器发出时钟中断请求,调度程序便据此信号来停止该进程的执行,并将它送往就绪队列的末尾;然后,再把处理机分配给就绪队列中新的队首进程,同时也让它执行一个时间片。这样就可以保证就绪队列中的所有进程,在一给定的时间内,均能获得一时间片的处理机执行时间。
如果在时间片结束时进程还在运行,则CPU将被剥夺并分配给另一个进程。如果进程在时间片结束前阻塞或结束,则CPU当即进行切换。调度程序所要做的就是维护一张就绪进程列表,当进程用完它的时间片后,它被移到队列的末尾。
好不容易才找到答案:
时间片轮转法主要是分时系统中使用的一种调度算法。时间片轮转法的基本思想是,将CPU 的处理
时间划分成一个个时间片,就绪队列中的诸进程轮流运行一个时间片。当时间片结束时,就强迫运行进程让出CPU,该进程进入就绪队列,等待下一次调度。同时,进程调度又去选择就绪队列中的一个进程,分配给它一个时间片,以投入运行。在轮转法中,时间片长度的选择非常重要,将直接影响系统开销和响应时间。如果时间片长度很小,则调度程序剥夺处理机的次数频繁,加重系统开销;反之,如果时间片长度选择过长,比方说一个时间片就能保证就绪队列中所有进程都执行完毕,则轮转法就退化成先进先出算法
影响时间片大小设置的主要因素有:系统响应时间、就绪进程数目(终端数目)
和计算机处理能力。
以上就是关于什么是“时间片轮转法”全部的内容,包括:什么是“时间片轮转法”、c语言,单处理机进程调度,时间片轮转、编写程序模拟处理机调度 时间片轮转 动态优先权调度 高响应比优先调度 c++代码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)