回顾c ++中的循环代码 [英] Review of Round robin code in c++

查看:84
本文介绍了回顾c ++中的循环代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过大量的在线研究和研究各种源代码。我想过尝试自己想出一种编写循环代码的方法。

我试过,但是在输出的某些部分出现错误。我无法生成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;


}

推荐答案

我明白你想要它的乐趣(例如学习一些东西;-))*模拟*简单的循环仲裁。

在这个假设下,有几个问题要讨论:

- 你把问题的所有方面混合到一起一堆平坦且非结构化的嵌套ifs和fors

- 你不使用数据结构(类)和函数来连接属于一起的东西

- 之间没有分离算法,输入数据和统计数据可见



让我们先从statig开始。如果你不能用散文或伪代码说出你想要的东西,你就不需要开始编码了 - 它不会带来丰硕的成果。



回合Robin

简单的循环算法从下一个N候选池中获取,在其他人等待时为其提供所需资源,在1 / N时间后,转到下一个(再次追逐第一个候选者)。



对于一个CPU上的进程,这将在某种计时器中实现,该计时器在每次执行后进行上下文切换第N个时间片(每个时间片是1 / N持续时间)。在这种情况下,进行统计是没有意义的,因为在所有(N-1)个其他候选者被服务之后,每个进程总是*它的1 / N时间片。一个进程可能处于某种等待状态,它会放弃他的时间片,因此,下一个进程将在早期启动方面受益 - 但它可能不会成为更大的时间片。 Round Robin保证任何进程都没有*挨饿:每个进程总是在每个(N-1)时间片后执行。



循环模拟

为了模拟Round Robin arbitation,你可以通过一些处于两种可能状态的类来建模过程:暂停或运行(这个过程模型是最简单的一个)对于Round Robin模拟)。



然后你可以定义一个Scheduler类,它带有一个进程向量和一个策略来安排(例如Round Robin)。



例如跟随骨架可能是一个起点:
I understand that you want for the fun of it (e.g. to learn something ;-)) *simulate* simple Round Robin arbitration.
Under that assumption, there are several issues to discuss:
- you mix all aspects of the problem into a flat and unstructured pile of nested ifs and fors
- you do not use data structures (classes) and functions to connect things that belong together
- there is no separation between the algorithm, the input data and the statistics visible

Let's start with statig the prpoblem first. If you cannot say what you want in prose or pseudo code, you do not need to start coding - it would not lead to a fruitful end.

Round Robin
The simple round robin algorithm takes from a pool of N candidates the next one, gives it the needed resources while the others wait, and after 1/N time, goes to the next (going after the last candidate to the first again).

For processes on one CPU, that would be implemented in some kind of timer which does the context switch after each Nth time slice (each time slice is 1/N duration). In such a situation, there is no point in doing statistics since each process gets *always* its 1/N time slice after all (N-1) other candidates were served. A process may be in some waiting state, where it kind of gives up on his time slice, so, the next process would benefit in terms of being started earlier - but it may not become a larger time slice. Round Robin guarantees that there is *no* starving of any process: every process always gets executed at latest after every (N-1) time slice.

Simulation of the Round Robin
To simulate the Round Robin arbitation, you may model the processes by some class that is in two possible states: suspended or running (this process model is the simplest one for the Round Robin simulation).

Then you could define a Scheduler class that takes a vector of processes and a strategy to schedule (e.g. the Round Robin).

E.g. the followong skeleton may be a starting point:
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 ++中的循环代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆