问题描述
经过大量的在线研究和研究各种源代码。我想过尝试自己想出一种编写循环代码的方法。
我试过,但是在输出的某些部分出现错误。我无法生成Gaint图表,也请任何人解释一下我在计算我的等待时间算法时犯了错误。
任何人都可以特别解释我怎么能得到每个过程的等待时间,并善意纠正我的算法。请给我一个真诚的要求。我是一名自学者,除了在线文章或书籍之外,基本上没有人可以帮助我理解代码。所以,如果你纠正我的错误,请你解释我和纠正。我真的很想得到这个概念,因为我不想再犯同样的错误。谢谢。
这是我的代码: -
After lots of research online and studying various source codes. I thought of trying myself to come up with a way to write the round robin code.
I tried but i get errors in certain part of my output. I am unable to generate the Gaint chart and also please can anyone explain me where i made mistake in calculating my waiting time algorithm.
Can anyone explain me specially how can i get the waiting time of each process and kindly correct my algorithm .Please i have a sincere request. I am a self learner and there is basically no one other than online articles or books to help me understand a code. So, if you correct my mistake, please can you explain me along with the correction. I really want to get the concept because i dont want to do the same mistake again. Thanks.
here is my code:-
#include<iostream>
using namespace std;
int main()
{
int k,j,q,i,n,ts,temp;
int aw; float awt;
int bt[10],wt[10],te[10],rt[10],at[10];j=0; //te array stores the number of times a process comes to CPU before completion
//bt is my burst time store array
//wt is my waiting time array
//te keeps a count of the number of times a process enters the CPU before completion
//at is the arrival time
cout<<"Enter number of processes"<<endl;
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter burst time"<<endl;
cin>>bt[i];
cout<<"Enter arrival times"<<endl;
cin>>at[i];
te[i] = 0; wt[i] = 0;
}
for(i=0;i<n;i++)
{
for(j = i+1;j<n;j++)
{
if(at[j]<at[i])
{
temp = at[i];
at[i] = at[j];
at[j] = temp;
if(at[j] ==at[i])
temp = bt[i];
bt[i] = bt[j];
bt[j] = temp;
}
}
}
cout<<"Enter time slice"<<endl;
cin>>ts;
cout<<"process:"<<endl;
for(i=0;i<n;i++)
{
cout<<"\t"<<i+1<<endl;
}
cout<<"Burst time:"<<endl;
for(i=0;i<n;i++)
{
cout<<" "<<bt[i]<<endl;
rt[i] = bt[i];
}
cout<<"arrival time:"<<endl;
for(i=0;i<n;i++)
{
cout<<" "<<at[i]<<endl;
}
cout<<"Gaint chart"<<endl;
while (j<=n)
{
j++;
for(i = 0;i<n;i++)
{
if(rt[i] ==0) continue;
if(rt[i]>=ts)
{
cout<<"\t"<<q<<i+1<<endl;
q = q + ts;
rt[i] = rt[i] - ts;
te[i] = te[i] + 1;
}
else
{
cout<<" "<<q<<i+1<<endl;
wt[i] = q-te[i]*ts;
q = q +rt[i];
rt[i] = rt[i] - rt[i];
}
}
}
awt = 0;
cout<<"Process Waiting Time"<<endl;
for(i =0;i<n;i++)
{
wt[i] = wt[i] - at[i];
cout<<" "<<i+1<<endl;
cout<<wt[i]<<endl;
awt = awt + wt[i];
}
aw = awt;
cout<<"Total waiting time"<<aw<<endl;
cout<<"Average waiting time "<<awt/n<<endl;
return 0;
}
推荐答案
class Process {
public:
Process(const string& name): _name(name) {}
bool resume() const { cout << "resuming " << name << endl; return true; }
bool suspend() const { cout << "suspending " << name << endl; return true; }
private:
const string _name;
};
class SchedulingStrategy {
public:
virtual void next(const vector<Process*> &processes) = 0;
};
class RoundRobinStrategy: public SchedulingStrategy {
public:
RoundRobinStrategy(): _current(-1) {}
virtual void next(const vector<Process*> &processes)
{
// only active if there are processes
if (processes.size() > 0)
{
if (_current < 0)
{
// special initial handling
_current = 0;
}
else
{
// stady state handling
processes[_current]->suspend();
_current++;
_current %= processes.size();
}
processes[_current]->resume();
}
}
private:
int _current;
};
class Scheduler {
public:
Scheduler(SchedulingStrategy &strategy)
: _strategy(strategy), processes()
{}
void addProcess(const string& name) { _processes.push_back(new Process(name)); }
void run(int n)
{
while(--n > 0) _strategy.next(_processes);
}
private:
SchedulingStrategy &_strategy;
const vector<Process*> _processes;
};
int main()
{
RoundRobinStrategy rr;
Scheduler scheduler(rr);
scheduler.addProcess("A");
scheduler.addProcess("B");
scheduler.addProcess("C");
scheduler.addProcess("D");
scheduler.run(10); // won't call a final suspend in this simple implementation
}
玩得开心!
Andi
Have fun!
Andi
#include<iostream>
using namespace std;
int main()
{
int wtime[10],btime[10],rtime[10],num,quantum,total;
cout<<"Enter number of processes(MAX 10): ";
cin>>num;
cout<<"Enter burst time";
for(int i=0;i<num;i++)
{ cout<<"\nP["<<i+1<<"]: "; cin>>btime[i];
rtime[i] = btime[i];
wtime[i]=0;
}
cout<<"\n\nEnter quantum: "; cin>>quantum;
int rp = num;
int i=0;
int time=0;
cout<<"0";
wtime[0]=0;
while(rp!=0) {
if(rtime[i]>quantum)
{
rtime[i]=rtime[i]-quantum;
cout<<" | P["<<i+1<<"] | ";
time+=quantum;
cout<<time;
}
else if(rtime[i]<=quantum && rtime[i]>0)
{time+=rtime[i];
rtime[i]=rtime[i]-rtime[i];
cout<<" | P["<<i+1<<"] | ";
rp--;
cout<<time;
}
i++;
if(i==num)
{
i=0;
}
}
system("pause");
return 0;
}
这篇关于回顾c ++中的循环代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!