-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoundRobin.cs
More file actions
100 lines (90 loc) · 3.37 KB
/
RoundRobin.cs
File metadata and controls
100 lines (90 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace Scheduling
{
class RoundRobin : SchedulingPolicy
{
int curProcessId = 0;
int Round_Quantum;
List<int> RR_processes;
public RoundRobin(int quantum)
{
Quantum = quantum;
RR_processes = new List<int>();
}
public override int NextProcess(Dictionary<int, ProcessTableEntry> dProcessTable)
{
foreach (int processID in RR_processes)//הרעבה
{
if (!dProcessTable[processID].Done && !dProcessTable[processID].Blocked)
{
dProcessTable[processID].MaxStarvation++;
}
}
if (RR_processes.Count == 0)
return -1;
if (curProcessId == 0)
{
curProcessId = 1;
return curProcessId;
}
if (curProcessId == RR_processes.Count - 1)
{
for (int i = 1; i < RR_processes.Count; i++)
{
int nextProcessId = RR_processes[i];
var nextProcess = dProcessTable[nextProcessId];
if (!nextProcess.Yield && !nextProcess.Blocked && !nextProcess.Done)
{
if(dProcessTable[curProcessId].Quantum == 0)
dProcessTable[curProcessId].Quantum = Quantum;
curProcessId = nextProcessId;
dProcessTable[nextProcessId].MaxStarvation = 0;
return nextProcessId; // החזר את מזהה התהליך הנוכחי
}
}
}
else
{
for (int i = curProcessId; i < RR_processes.Count()-1; i++)
{
int nextProcessId = i+1;
var nextProcess = dProcessTable[nextProcessId];
if (!nextProcess.Done && !nextProcess.Blocked && !nextProcess.Yield)
{
if (dProcessTable[curProcessId].Quantum == 0)
dProcessTable[curProcessId].Quantum = Quantum; curProcessId = nextProcessId;
nextProcess.MaxStarvation = 0;
return nextProcessId;
}
}
}
if (dProcessTable[curProcessId].Quantum == 0)
dProcessTable[curProcessId].Quantum = Quantum;
curProcessId = 1;
for (int i = 1; i < RR_processes.Count(); i++)
{
int nextProcessId = i;
var nextProcess = dProcessTable[nextProcessId];
if (!nextProcess.Done && !nextProcess.Blocked && !nextProcess.Yield)
{
curProcessId = nextProcessId;
nextProcess.MaxStarvation = 0;
return nextProcessId;
}
}
return 0;
}
public override void AddProcess(int iProcessId)
{
RR_processes.Add(iProcessId);
}
public override bool RescheduleAfterInterrupt()
{
return true;
}
}
}