循环算法,进程不完成执行

时间:2015-02-04 11:07:47

标签: java algorithm cpu scheduler round-robin

我正在编写一个简单的cpu调度程序模拟器,目前我正在使用Round Robin算法。在这段代码中,我实现它的方式是这样的: 从用户输入中获取突发时间的总和,以便在何时进行跟踪 这个街区应该完成。只要总和大于或等于0,就继续执行。

现在,问题是总和变为负数,并且块在进程之前停止执行'爆发时间变成全0。有什么办法可以解决这个问题吗?

例如,假设我们的输入是{9,12,1,1,4,4}, 使用量子4执行所有进程后,阶段1的输出为: (其中第一列是原始数组,第二列是量子值的减少)

sum: 31

phase nr: 1
9 7     not zero
12 10   not zero
1 0     check
1 0     check
4 2     not zero
4 2     not zero

phase nr: 2
7 5     not zero
10 8    not zero
0 0     check
0 0     check
2 0     check
2 0     check

在这个阶段,总和变为负数并停止打印其他过程

这是迄今为止我一直在努力的代码。我不知道这个解释是否足够清楚,我试着尽可能清楚和具体。

注意:所有作业的到达时间均为0

int[] a = {9, 12, 1, 1, 4, 4};
        int sum = 0;

        //total sum of the a[]
        for (int i = 0; i < a.length; i++) 
        {
            sum += a[i];            
        }
        System.out.printf("\nsum: %d\n\n", sum);

        int counter = 1;
        while(sum >= 0)
        {
            System.out.printf("\nphase nr: %d\n", counter);

            for (int i = 0; i < a.length; i++) 
            {
                //prints the original array
                System.out.printf("%d ",a[i]);
                int value = a[i]; //takes a value from a[] at index i
                value -= 2;      //decrement the value by quantum 2
                a[i] = value;   //put the value at the same index
                if (a[i] < 0)  //it checks if any element is less than 0, if yes replace it with 0
                {
                    a[i] = 0;
                }

                /**
                 *prints the array after subtracting 2 from each element 
                 *in the original array and checks if there is any value
                 *equal to 0, if yes print "check" else "not zero"
                 */
                System.out.printf("%d \t%s",a[i], a[i]==0?"check":"not zero");
                System.out.println();
            }

            /**
             * decrements the sum based on the quantum 
             * multiplied by processes 
             * sum = sum -(quantum * numberOfProcesses)
             */
            sum = sum - (4 * 6);

            counter++;
        }

2 个答案:

答案 0 :(得分:1)

问题在于计算总和。

sum = sum - (4 * 6);

在每次迭代中减去24,即使进程先前完成(比如在1个时间内完成)或已经完成。

在每次迭代中你应该从sum中减去的是实际处理所花费的总时间。因此,如果处理完成(a [i] == 0),则不进行,并且如果a [i]&lt; 4你减去那个值。只有当[i]> = 4时,你应该减去4个量子。

如果你想为每个过程花费4个量子的时间而不管它的实际需要是否已经完成,那么你计算你的总和是错误的并且它不是真正的循环。

while(sum> = 0)         {             System.out.printf(&#34; \ nphase nr:%d \ n&#34;,counter);

        for (int i = 0; i < a.length; i++) 
        {
            //prints the original array
            System.out.printf("%d ",a[i]);
            int value = a[i]; //takes a value from a[] at index i
            if(a[i] == 0) { continue;} // skip process if it's done
            else if(a[i]<=4) { sum -=a[i]; a[i]=0;} // if process is less than one quantum then it's done and sum is decreased by time needed
            else { sum -=4; a[i] -=4} // otherwise process needs more than one quantum so it uses up all 4

            /**
             *prints the array after subtracting 2 from each element 
             *in the original array and checks if there is any value
             *equal to 0, if yes print "check" else "not zero"
             */
            System.out.printf("%d \t%s",a[i], a[i]==0?"check":"not zero");
            System.out.println();
        }
        // this part was completely unnecessary so it was removed to for loop above

        counter++;
    }

答案 1 :(得分:0)

我不确定你的算法是否完全理解,但总和的计算是不正确的。你应该定义一些函数,它将对数组a []中的实际值求和,而不是使用一些错误的常量计算(你使用4作为量子而不是2)并忽略一些事件,即一些事件不会减少设置为0。

您可以定义类似的内容:

public static int totalBurstTime(int[] bursts) {
    int total = 0;
    for (int b : bursts) {
        total += b;
    }
    return total;
}

并调用它来初始化while循环之前的和,然后在每个循环结束时。

此外,您应该使用一些常量而不是幻数,例如

static final int QUANTUM = 2;

并在任何需要的地方使用它......

相关问题