using
System;
using
System.Collections.Generic;
using
System.Linq;
class
Process
{
public
int
ProcessID {
get
;
set
; }
public
int
ArrivalTime {
get
;
set
; }
public
int
Priority {
get
;
set
; }
public
int
BurstTime {
get
;
set
; }
public
int
TempBurstTime {
get
;
set
; }
public
int
ResponseTime {
get
;
set
; } = -1;
public
int
OutTime {
get
;
set
; }
public
int
InTime {
get
;
set
; } = -1;
public
Process(
int
processID,
int
arrivalTime,
int
priority,
int
burstTime)
{
ProcessID = processID;
ArrivalTime = arrivalTime;
Priority = priority;
BurstTime = burstTime;
TempBurstTime = burstTime;
}
}
class
Program
{
static
void
Insert(List<Process> heap, Process value,
ref
int
heapSize,
ref
int
currentTime)
{
int
start = heapSize;
heap.Add(value);
if
(heap[heapSize].InTime == -1)
heap[heapSize].InTime = currentTime;
heapSize++;
while
(start != 0 && heap[(start - 1) / 2].Priority > heap[start].Priority)
{
Process temp = heap[(start - 1) / 2];
heap[(start - 1) / 2] = heap[start];
heap[start] = temp;
start = (start - 1) / 2;
}
}
static
void
Order(List<Process> heap,
int
heapSize,
int
start)
{
int
smallest = start;
int
left = 2 * start + 1;
int
right = 2 * start + 2;
if
(left < heapSize && heap[left].Priority < heap[smallest].Priority)
smallest = left;
if
(right < heapSize && heap[right].Priority < heap[smallest].Priority)
smallest = right;
if
(smallest != start)
{
Process temp = heap[smallest];
heap[smallest] = heap[start];
heap[start] = temp;
Order(heap, heapSize, smallest);
}
}
static
Process ExtractMinimum(List<Process> heap,
ref
int
heapSize,
ref
int
currentTime)
{
Process minProcess = heap[0];
if
(minProcess.ResponseTime == -1)
minProcess.ResponseTime = currentTime - minProcess.ArrivalTime;
heapSize--;
if
(heapSize >= 1)
{
heap[0] = heap[heapSize];
Order(heap, heapSize, 0);
}
return
minProcess;
}
static
bool
Compare(Process p1, Process p2)
{
return
p1.ArrivalTime < p2.ArrivalTime;
}
static
void
Scheduling(List<Process> heap, List<Process> array,
int
n,
ref
int
heapSize,
ref
int
currentTime)
{
if
(heapSize == 0)
return
;
Process minProcess = ExtractMinimum(heap,
ref
heapSize,
ref
currentTime);
minProcess.OutTime = currentTime + 1;
minProcess.BurstTime--;
Console.WriteLine($
"process id = {minProcess.ProcessID} current time = {currentTime}"
);
if
(minProcess.BurstTime > 0)
{
Insert(heap, minProcess,
ref
heapSize,
ref
currentTime);
return
;
}
for
(
int
i = 0; i < n; i++)
{
if
(array[i].ProcessID == minProcess.ProcessID)
{
array[i] = minProcess;
break
;
}
}
}
static
void
Priority(List<Process> array,
int
n)
{
array = array.OrderBy(p => p.ArrivalTime).ToList();
int
totalWaitingTime = 0;
int
totalBurstTime = 0;
int
totalTurnaroundTime = 0;
int
insertedProcess = 0;
int
heapSize = 0;
int
currentTime = array[0].ArrivalTime;
int
totalResponseTime = 0;
List<Process> heap =
new
List<Process>(4 * n);
for
(
int
i = 0; i < n; i++)
{
totalBurstTime += array[i].BurstTime;
array[i].TempBurstTime = array[i].BurstTime;
}
do
{
if
(insertedProcess != n)
{
for
(
int
i = 0; i < n; i++)
{
if
(array[i].ArrivalTime == currentTime)
{
insertedProcess++;
array[i].InTime = -1;
array[i].ResponseTime = -1;
Insert(heap, array[i],
ref
heapSize,
ref
currentTime);
}
}
}
Scheduling(heap, array, n,
ref
heapSize,
ref
currentTime);
currentTime++;
if
(heapSize == 0 && insertedProcess == n)
break
;
}
while
(
true
);
for
(
int
i = 0; i < n; i++)
{
totalResponseTime += array[i].ResponseTime;
totalWaitingTime += (array[i].OutTime - array[i].InTime - array[i].TempBurstTime);
totalTurnaroundTime += (array[i].OutTime - array[i].InTime);
totalBurstTime += array[i].BurstTime;
}
Console.WriteLine($
"Average waiting time = {totalWaitingTime / (float)n}"
);
Console.WriteLine($
"Average response time = {totalResponseTime / (float)n}"
);
Console.WriteLine($
"Average turn around time = {totalTurnaroundTime / (float)n}"
);
}
static
void
Main(
string
[] args)
{
int
n = 5;
List<Process> a =
new
List<Process>
{
new
Process(1, 4, 2, 6),
new
Process(4, 5, 1, 3),
new
Process(2, 5, 3, 1),
new
Process(3, 1, 4, 2),
new
Process(5, 3, 5, 4)
};
Priority(a, n);
}
}