循环算法存在的问题 [英] Problems with Round Robin Algorithm

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

问题描述

之后,我之前关于尝试在c ++中提出循环算法代码的帖子可以在这里找到

[ ^ ]



我想要接受Andy的建议然后出现用一种新的编写算法的方法。感谢Andy和代码项目人员的帮助。



所以,这是我的新代码,几乎每个地方都添加了评论。我被困在程序的某些部分。请任何人都可以查看我的代码并更正我的错误。我被卡住的地方,我将在下面提到。这是我的代码 -



After,My previous post on trying to come up with a code for Round robin algorithm in c++ which can be found here
[^]

I thought of taking Andy's suggestion and come up with a new way of writing the algorithm. Thanks to Andy and the people of code project for their help.

So, here is my new code and i have added comments almost every where. I am stuck at some part of my program . Please can anyone review my code and correct my mistake. The areas where i am stuck, i shall mention below. Here is my code--

#include <iostream>
#include <stdio.h>

static int n;
int i,j,total_time = 0;
float avgtat=0, avgwt = 0;
int queue[];
int time = 0, front = 0,rear = 0,q_count = 0; //q_counnt means the number of elements in the que
				
using namespace std;


struct data //structure contains all the details of the processes
{
	int at,st,ct,tat,wt; //at = arrival time, st= scheduled time, ct = completion time,
	                     //tat= turn around time, wt = waiting time
	char pname[20];
	float ntat;


}temp;

void push(int q) //push function basically pushes a current process in the que
	{
		queue[rear++] = q;
	}

	int pop() //to pop out an element from the top of the que
	{
		int x;
		x = queue[front++];
		return x;
	}

	void check(struct data a[]) //this is to check which processes have arrived
	{
		while(a[j].at<time&&j>		{
			q_count++; //number of process in the que is incremented by 1 
			push(j++); //pushing the element in the que and incrementing the j value
		}
	}



	void finding(struct data a[])
	{
		int temp_st[n] ,flag =0, count = 0, p_process; //temp_st is the temporary service time, flag is used to determine if a process is under
		                                             //execution ,p_process is the name of the current process
		j = 0;  //j is the global variable
		int tq;
		cout<<"Enter time quantum"<<endl;
		cin>>tq;
		for(i=0;i<n;i++)>
		{
			temp_st[i] = a[i].st; //service time of the processes is stored in the temporary service time
		}

		time = a[0].at; //this is to set the starting time as the arrival time of a[0] which is my first process
		q_count = 1; //number of elements in the que is 1
		push(j++); //pushing the first process in the que
		while(time<=total_time)  //time is less than the total time
		{
			if(flag==1 ||q_count!=0) //variable flag denotes that a process is selected and q_count != 0 is to denoted that atleast one process has arrived
			{
				if(flag==0 &&count==0) //condition to select the process
				{
					p_process = pop(); //to pop a process from the que
					count =0; //count is set to 0
					flag=1; //flag is set to 1 to identify that a process is under execution
				}

				temp_st[p_process]--; //we are decrementing the service time by 1
				
				if(temp_st[p_process]==0) //to check if the service time of the present process is equal to 0
				{
					time++; //time is incremented by 1
					count =0;
					a[p_process].ct = time; //this is to get the completion time of the present process
					flag = 0; //flag is set to 0
					q_count--; //the process is removed from the que 
					check(a); //check if any new process has arrived
					continue; //continue back to the top part of the loop
				}
				count++;

				if(count==tq) //if count is equal to time quantum
				{
					count = 0; //we make count set to zero again
					time++;  //time is incremented by 1
					check(a); //check if any process has arrived
					push(p_process); //push the current process back to the que
					flag =0; //flag is set to zero that is no process is under execution yet
				}

				else //if time count is not equal to time quantum
				{
					time++; //we increment time 
					check(a); //to check if any new process has arrived
				}
		}

	

	else  
	{
		time++;
		check(a);

	}

		}
	
	}


	void display(struct data a[])
	{  
		//Please help me here, i am confused what should i declare here
	}



int main()
{
	cout<<"Enter the number of processes"<<endl;
	cin>>n;

	struct data aa[n];


	//sorting
	for (i = 0;i<n;i++)>
	{
		cout<<"Enter the name of the processes"<<endl;
		cin>>aa[i].pname;
		cin>>aa[i].at>>aa[i].st;
	}

	for (i=0;i<n;i++)>
	{
		for(j=i+1; j<n;j++)>
		{
			if(aa[j].at<aa[i].at)>
			{
				temp = aa[i];
				aa[i] = aa[j]; 
				aa[j] = temp;
	            
			}
			else if(aa[i].at==aa[j].at) 
			{
				if(aa[j].st<aa[i].st)>
				{
					temp = aa[i];
					aa[i] = aa[j];
					aa[j] = temp;
				}
			}
		}

		//finding the total time

		total_time+=aa[0].at+aa[0].st;
		for(i = 0;i<n;i++)>
		{
         if(aa[i].at>total_time)
			 total_time += aa[i].st;
		}
		
		finding(aa);

	 return 0; 
	}          







以下是我遇到的几个问题 -



1.我在主要结构RR中调用结构RR的部分

结构RR a [n];

//在这里它给我的错误是说静态n必须有一个恒定值







2.Also在我的void find函数中,我声明了一个名为

int temp_st [n]的数组来存储每个进程的服务时间/突发时间。我得到了相同的错误,上面写着静态n必须有一个恒定的值



3.最后,我是数据结构的新手,我对它没有多少经验。所以,我很困惑应该在我的显示功能中声明什么

void display(struct data a []){------请在这里帮助我-----}



最后请建议我是否还有其他更正。谢谢




Here are the few problems i am having--

1. The part where i call the structure RR in my main
struct RR a[n];
//here it gives me error by saying that static n must have a constant value



2.Also in my void finding function where i declare an array called
int temp_st[n] to store the service time/burst time of each process. I get the same error which says "Static n must have a constant value"

3. Finally, i am new to data structures and i dont have much experience with it. So, i am confused what should i declare in my display function
void display(struct data a[]) { ------Please help me here -----}

Lastly please suggest if there is any more corrections which i should make. Thanks

推荐答案

我完成了代码并发现了以下问题:



1.糟糕的设计:你不必要地使用了很多全局变量。你不应该。虽然您可以不使用面向对象的范例,但即使是过程编程也使用结构和函数参数来传递变量和值而不是全局变量。那么为什么不使用存储过程当前状态的结构并将其传递给每个操作它的函数?



2. int queue []; 此数组似乎没有在任何地方初始化。如果您最初没有指定它的尺寸,或者稍后在堆上以足够的大小动态分配它,则无法访问数组的元素!



3. queue [rear ++] = q;

函数push()不检查索引后面的有效性。什么是最大值?当然你不能访问超过最后一个数组队列的元素 - 但是你没有为数组开始分配内存......



4。 x = queue [front ++];

与pop()函数相同的问题!您不检查有效的索引值。



5. void check(struct data a [])

缺少大小信息:您传递的数组没有关于数组大小的信息,并且在不确定它们存在的情况下迭代元素。在C中,通常的安全方法是将数组的大小作为第二个参数传递。在C ++中,更好的方法是使用标准容器,例如e。 G。 std :: vector



6. while(a [j] .at<时间&&>

除了这条线缺少一个右括号(可能只是一个错误的类型)之外,这个条件真的是你想要的吗?特别是最后一个我真的不知道你打算在这里完成什么。



更重要的是:j的初始值是什么?为什么你甚至在这里使用全局变量? ?我只能重复自己:不要使用全局变量!(除非你有一个很好的理由 - 你没有一个)



7. q_count ++; //在进程中的进程数增加1

最后一次初始化q_count是什么时候?



8. int temp_st [n]

这不是数组的有效定义。数组的大小(n)必须是常量并且在编译时是已知的.n是一个变量,即不是常量,因此在编译时不知道。编译器不能为一个不知道大小的数组保留内存 - 更糟糕的是,它不可能为连续调用函数的大小分配内存 finding()



9. for(i = 0; i< n; i ++)>

看起来像是行尾的另一个错字。

除此之外:这是另一个循环/函数,它假定它知道传递给函数的数组的大小。确保此工作的唯一方法是跟踪调用此函数的代码中的每个位置!对于像你这样的小程序来说,这可能不是一个问题,但是一旦你开始编写更大的程序,它就非常非常糟糕!最好不要习惯那种风格的编程!



10. while(time< = total_time)//时间小于总时间

呃。我是否提到全局变量以及为什么不应该使用它们?谁初始化了total_time,什么时候?是否初始化了?请不要这样做 - 如果您正在阅读不熟悉的代码,那么您最不希望看到的是这样的一行,然后必须检查整个代码是否出现全局变量并验证它是否已初始化。或者不是!



11. count = 0; // count设置为0

这只是我的一个小小的烦恼:您赞赏写评论的努力。但是,这个评论绝对没有提供该声明显然没有提供的信息!如果没有任何不明显的东西你可以添加澄清声明的需要或目的,只需添加评论!



以下代码中有更多这样的评论 - 我会留给你消除它们......



12. 确认(a); //检查是否有任何新进程

这里没有任何内在错误 - 但是,上面我正在评论函数检查() - 你看到问题了吗?提示:稍后会有更多调用check()...



13. struct data aa [n];

错误的aarray声明。见上文第8项。



14. cin>> aa [i] .pname;

老实说,我不知道这是否会按预期工作。我不知道因为我永远不会使用iostream来读取char数组。目前还不清楚如果你不小心输入的字符串比保留缓冲区要长的话会发生什么,而且如果输入一个较短的字符串也不清楚会发生什么 - 尽管我怀疑它可能正常工作。事实上,我甚至不确定它是否会起作用,因为从技术上讲, aa [i] .pname 是一个指针,我完全不确定如果这是一个固定大小的字符数组的信息甚至传递给cin! (它可能是,但我不打算尝试,因为有更好的方法来读取字符串!)



15.排序循环缺少最后的结束括号。







这就是我从查看代码中得到的结果,而不是试图弄清楚它做了什么,或应该做什么(现在可能不一样)



如果您需要有关一个或多个问题的进一步建议我指出,请告诉我。
I went through the code and found the following issues:

1. Bad design: you're using lots of global variables needlessly. You shouldn't. While you are free to not use the object oriented paradigm, even procedural programming uses structs and function arguments to pass on variables and values instead of global variables. So why don't you use a struct that stores the current state of the processes and pass it to every function that manpulates it?

2. int queue[]; this array doesn't appear to be initialized anywhere. You can't access the elements of an array if you don't at first either specify it's dimensons, or later allocate it dynamically on the heap with sufficient size!

3. queue[rear++] = q;
The function push() does not check for the validity of the index rear. What is the maximum value? Of course you cannot access an element of the array queue past the last - but then you didn't allocate memory for the array to start with...

4. x = queue[front++];
Same issue with function pop()! You don't check for valid index values.

5. void check (struct data a[])
Missing size info: You're passing an array without information on the size of the array, and iterating over the elements without being sure they exist. In C, the usual way to be on the safe side is to pass the size of the array as a second argument. In C++ the better way is to use a standard container instead, e. g. std::vector

6. while(a[j].at<time&&j>
Apart from the fact this line is missing a closing parenthesis (probably just a mistype ) , is that condition really what you wanted? Especially the last bit? I honestly have no idea what you meant to accomplish here.

More importantly: what is the initial value of j? Why are you even using a global variable here? I can only repeat myself: do not use global variables! (unless you have a very good reason - and you don't one)

7. q_count++; //number of process in the que is incremented by 1
When was the last time you initialized q_count?

8. int temp_st[n]
This is not a valid definition of an array. The size of the array (n) must be constant and known at compile time. n is a variable, i. e. not constant, and therefore not known at compile time. The compiler cannot reserve memory for an array that it doesn't know the size of - even worse, it cannot possibly allocate memory for a size that may be different for consecutive calls to the function finding() !

9. for(i=0;i<n;i++)>
Looks like another typo at the end of the line.
Apart from that: here's yet another loop/function that assumes it knows the size of the array passed to the function. The only way to ensure this works is to keep track of every location in the code that calls this function! This may not be an issue for a small program like yours, but it is very, very bad once you start writing larger programs! Better not get used to that style f programming!

10. while(time<=total_time) //time is less than the total time
Ugh. Did I mention global variables and why you shouldn't use them? Who intialized total_time, and when? Is it initialized at all? Please don't do this - if you're reading unfamiliar code, the last thing you want is see a line like this and then have to check the entire code for occurences of a global variables and verify that it has been initialized. Or not!

11. count =0; //count is set to 0
This is just a pet peeve of mine: your effort to write comments is appreciated. However, this comment offers absolutely no information that the statement doesn't obviously provide already! if there isn't anything non-obvious you can add to clarify the need or purpose of a statement, just don't add a comment!

There are plenty more of such comments in the following code - I'll leave it to you to eliminate them...

12. check(a); //check if any new process has arrived
Nothing inherently wrong here - however, above I was commenting on the function check() - do you see the problem right here? Hint: there are more calls to check() later...

13. struct data aa[n];
Wrong aarray declaration. See item 8 above.

14. cin>>aa[i].pname;
Honestly, I don't know if this would work as intended. I don't know because I'd never use iostream to read a char array. It's unclear what happens if you accidentally enter a longer string than you reserved a buffer for, and it's unclear what happens if you enter a shorter string too - although I suspect that might work alright. In fact, I'm not even sure it would work at all, because, technically, aa[i].pname is a pointer, and I'm not at all sure if the information that this is a fixed size character array is even passed to cin! (it probably is, but I'll not bother trying, because there are better ways to read strings!)

15. The sort loop is missing a final closing bracket.



This is just what I get from looking at the code, without trying to figure out what it does, or is supposed to do (which likely isn't the same thing right now)

If you need any further advice on one or more of the issues I pointed out, let me know.


这篇关于循环算法存在的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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