简体   繁体   English

硬算法实现

[英]Hard Algorithm Implementation

I couldn't implement SJF(Shortest Job First) Algorithm. 我无法实现SJF(最短的作业优先)算法。

SJF work like this SJF的工作是这样的

If process arrived 0 time it'll work till the next process arrival the algorithm have to check if the arrival (process/processes) which arrived in 1 are shorter than the current remaining time 如果流程到达0时间,它将一直工作到下一个流程到达,算法必须检查到达1的到达(一个或多个流程)是否短于当前剩余时间
Example: P0 executed 1 and still 2 to finish, now we have P0,P1,P2,P3,P4 in 1 algorithm will execute the shortest one P3, and after it P0 then P4 then P1 and so on. 示例:P0执行了1并且仍然是2来完成,现在我们有了P0,P1,P2,P3,P4,其中1种算法将执行最短的一个P3,然后执行P0然后是P4然后是P1,依此类推。 The problem is I have to save to all processes their start and end time execution, and the waiting time. 问题是我必须保存所有进程的开始和结束时间执行以及等待时间。

This is my latest algorithm. 这是我最新的算法。 (Some wrong cases occurs) (发生一些错误的情况)

input data: 输入数据:

Process Arrival Burest 
P0      0       3
P1      0       4
P2      0       5
P3      1       1
p4      1       3

for (i = 0; i < proc.Count; i++)
{
    minProcIndex = i;
    for (x = i; x < proc.Count; x++)
    {
        for (int y = 0 ; y < proc.Count; y++)
        {
            if (y == minProcIndex)
                continue;

            if (tempBrust[minProcIndex] - tempArrival[y] > tempBrust[y] 
                && tempBrust[y] != 0)
            {
                tempBrust[minProcIndex] -= tempArrival[y];
               // tempArrival[minProcIndex] += tempArrival[y];
                clock += tempArrival[y];
                //proc[y].start = clock;
                minProcIndex = y;
            }
            else if (y != proc.Count -1)
                continue;
            else
            {
                if (y == 0)
                {
                    proc[minProcIndex].start = 0;
                }
                else if (proc[y].start > 0)
                {
                    proc[y].start = clock;
                }
                else
                proc[minProcIndex].start = clock;

                proc[minProcIndex].end = proc[minProcIndex].brust + clock;
                tempBrust[minProcIndex] = 0;
                clock += proc[minProcIndex].brust;
                if (minProcIndex == proc.Count - 1)
                    minProcIndex = 0;
                else
                minProcIndex++;
                for (int c = 0; c < proc.Count; c++)
                {
                    if (tempArrival[c] < clock)
                        tempArrival[c] = clock - 1;
                }
            }
        }
    }
}

I think this is easier if you create a class to hold the process information 我认为如果您创建一个类来保存过程信息会更容易

public class ProcessInformation
{
    public string Name { get; private set; }
    public int Duration { get; private set; }
    public int ArrivalTime { get; private set; }

    public int TimeLeft { get; set; }
    public int? StartTime { get; set; }
    public int? EndTime { get; set; }
    public List<int> InterruptTimes { get; private set; }

    public ProcessInformation(string name, int arrivalTime, int duration)
    {
        Name = name;
        ArrivalTime = arrivalTime;
        Duration = duration;
        TimeLeft = duration;
        InterruptTimes = new List<int>();
    }
}

Then you can just run the following code 然后,您可以运行以下代码

var processes = new List<ProcessInformation>
                {
                    new ProcessInformation("P0", 0, 3),
                    new ProcessInformation("P1", 0, 4),
                    new ProcessInformation("P2", 0, 5),
                    new ProcessInformation("P3", 1, 1),
                    new ProcessInformation("P4", 1, 3)
                };

int currentTime = 0;
ProcessInformation currentProcess = null;
while (processes.Any(p => p.TimeLeft > 0))
{
    var shortest =
        processes.Where(p => p.ArrivalTime <= currentTime && p.TimeLeft > 0)
            .OrderBy(p => p.TimeLeft)
            .FirstOrDefault();

    if (currentProcess != null && currentProcess.TimeLeft > 0 && shortest != currentProcess)
    {
        currentProcess.InterruptTimes.Add(currentTime);
    }

    if (shortest != null)
    {
        if (shortest.StartTime == null) shortest.StartTime = currentTime;
        shortest.TimeLeft--;
        if (shortest.TimeLeft == 0) shortest.EndTime = currentTime + 1;
    }

    currentProcess = shortest;
    currentTime++;
}

foreach (var p in processes)
{
    Console.WriteLine(
        "Process {0} arrived {1} started {2} ended {3} duration {4} wait {5} interrupt times {6}", 
        p.Name, 
        p.ArrivalTime, 
        p.StartTime, 
        p.EndTime, 
        p.Duration, 
        p.EndTime - p.ArrivalTime - p.Duration,
        p.InteruptTimes.Any() ? string.Join(", ", p.InteruptTimes) : "None");
}

which outputs the following 输出以下内容

Process P0 arrived 0 started 0 ended 4 duration 3 wait 1 interrupt times 1 进程P0到达0开始0结束4持续时间3等待1中断时间1

Process P1 arrived 0 started 7 ended 11 duration 4 wait 7 interrupt times None 进程P1到达0开始7结束11持续时间4等待7中断时间无

Process P2 arrived 0 started 11 ended 16 duration 5 wait 11 interrupt times None 进程P2到达0开始11结束16持续时间5等待11中断时间无

Process P3 arrived 1 started 1 ended 2 duration 1 wait 0 interrupt times None 进程P3到达1开始1结束2持续时间1等待0中断时间无

Process P4 arrived 1 started 4 ended 7 duration 3 wait 3 interupt times None 进程P4到达1开始4结束7持续时间3等待3中断时间无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM