30/09/2018, 19:54
Lỗi không đọc được file trong chương trình điều mô phỏng điều phối tiến trình
Đây là lỗi
Đây là code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace NLDH3
{
class Program
{
static String INP = "input.txt";
public struct process
{
public string name;
public int timeRQ, timeCPU, priortity;
public int timeOUT, timeIN, timewait, timesave;
public int index;
}
static int quantum;
static int set;
static void input(ref process[] pr, ref int n, ref int timeOUT)
{
try
{
StreamReader sr = File.OpenText(INP);
string[] line = sr.ReadLine().Split(' ');
n = int.Parse(line[0]);
set = int.Parse(line[1]);
quantum = int.Parse(line[2]);
for (int i = 0; i < n; i++)
{
line = sr.ReadLine().Split(' ');
pr[i].name = line[0];
pr[i].timeRQ = int.Parse(line[1]);
pr[i].timeCPU = int.Parse(line[2]);
pr[i].priortity = int.Parse(line[3]);
if (timeOUT < pr[i].timeRQ)
{
timeOUT = pr[i].timeRQ + pr[i].timeCPU;
}
else
timeOUT += pr[i].timeCPU;
pr[i].index = i;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
static void output_input(process[] pr, int n)
{
Console.WriteLine("-------------INPUT-------------");
Console.WriteLine();
Console.WriteLine("{0} {1} {2} {3}", "name", "TimeRQ", "TimeCPU", "Priority");
for (int i = 0; i < n; i++)
{
Console.WriteLine("[0] {1} {2} {3}", pr[i].name, pr[i].timeRQ, pr[i].timeCPU, pr[i].priortity);
}
Console.WriteLine();
Console.WriteLine("------OUTPUT---------");
}
//thực hiện điều phối FIFO
static void process_FIFO(ref process[] pr, int n, int timeOUT)
{
process[] RQ = new process[n];
int m = -1;
for (int t = 0; t < timeOUT; t++)
for (int i = 0; i < n; i++)
if (t == pr[i].timeRQ)
RQ[++m] = pr[i];
timeOUT = 0;
for (int i = 0; i <= m; i++)
{
if (timeOUT <= RQ[i].timeRQ)
{
timeOUT = RQ[i].timeRQ + RQ[i].timeCPU;
RQ[i].timeIN = RQ[i].timeRQ;
}
else
{
timeOUT += RQ[i].timeCPU;
RQ[i].timeIN = RQ[i - 1].timeOUT;
}
RQ[i].timeOUT = timeOUT;
RQ[i].timewait = RQ[i].timeOUT - (RQ[i].timeRQ + RQ[i].timeCPU);
RQ[i].timesave = RQ[i].timeOUT - RQ[i].timeRQ;
}
pr = RQ;
}
// th5c hiện điều phối do uu tien doc quyen
static void process_PRIORITY_preemptive(process[] pr, int n, int timeOUT)
{
process[] RQ = new process[n];
int m = 0;
int j = 0;
int temptime = 0;
for (int t = 0; t <= timeOUT; t++)
{
if (m > 0 && j < m)
{
if (temptime < RQ[j].timeCPU)
temptime++;
if (temptime == RQ[j].timeCPU)
{
RQ[j].timeIN = t - RQ[j].timeCPU;
RQ[j].timeOUT = RQ[j].timeIN + RQ[j].timeCPU;
RQ[j].timewait = RQ[j].timeOUT - (RQ[j].timeRQ + RQ[j].timeCPU);
RQ[j].timesave = RQ[j].timeOUT - RQ[j].timeRQ;
temptime = 0;
j++;
}
}
for (int i = 0; i < n; i++)
if (t == pr[i].timeRQ)
{
int k = m;
while (k > j + 1 && pr[i].priortity < RQ[k - 1].priortity)
{
RQ[k] = RQ[k - 1];
k--;
}
RQ[k] = pr[i];
m++;
}
}
pr = RQ;
}
//Do uu tien khong doc quyen
static void process_PRIORITY_nopreemptive(process[] pr, int n, int m, int timeOUT)
{
process[] RQ = new process[n];
int j = 0;
m = 0;
int temptime = 0;
for (int t = 0; t <= timeOUT; t++)
{
if (m > 0 && j < m)
{
if (temptime < RQ[j].timeCPU)
temptime++;
if (temptime == RQ[j].timeCPU)
{
RQ[j].timeIN = t - RQ[j].timeCPU;
RQ[j].timeOUT = RQ[j].timeIN + RQ[j].timeCPU;
RQ[j].timewait = RQ[j].timeOUT - (RQ[j].timeRQ + RQ[j].timeCPU);
RQ[j].timesave = RQ[j].timeOUT - RQ[j].timeRQ;
pr[RQ[j].index].timeOUT = t;
pr[RQ[j].index].timewait = pr[RQ[j].index].timeOUT
- (pr[RQ[j].index].timeRQ + pr[RQ[j].index].timeCPU);
pr[RQ[j].index].timesave = pr[RQ[j].index].timeOUT - pr[RQ[j].index].timeRQ;
temptime = 0;
j++;
}
}
for (int i = 0; i < n; i++)
if (t == pr[i].timeRQ)
{
m++;
int k = m - 1;
if (temptime > 0 && pr[i].priortity < RQ[j].priortity)
{
m++;
k = m - 1;
for (k = m - 1; k > j + 1; k--)
RQ[k] = RQ[k - 2];
RQ[j + 1] = pr[i];
RQ[j + 2] = RQ[j];
RQ[j + 2].timeCPU -= temptime;
RQ[j].timeIN = t - temptime;
RQ[j].timeOUT = t;
RQ[j].timeCPU = temptime;
temptime = 0;
j++;
}
else
{
while (k > j && pr[i].priortity < RQ[k - 1].priortity)
{
RQ[k] = RQ[k - 1];
k--;
}
RQ[k] = pr[i];
}
}
}
}
//In ket qua thuc hien dieu phoi FiFO
static void output_FIFO(process[] pr, int n, int timeOUT)
{
Console.WriteLine("
FIFO
PROCESS
");
Console.WriteLine("{0} {1} {2} {3} {4} {5} {6} {7}
",
"Name", "TimeRQ", "TimeCPU", "Priority", "TimeIN", "TimeOUT", "Timewait",
"Timesave");
for (int i = 0; i < n; i++)
{
Console.WriteLine("{0} {1} {2} {3} {4} {5} {6} {7}
",
pr[i].name, pr[i].timeRQ, pr[i].timeCPU, pr[i].priortity, pr[i].timeIN,
pr[i].timeOUT, pr[i].timewait, pr[i].timesave);
}
}
//In ket qua thuc hien do uu tien doc quyen
static void output_PRIORITY_preemptive(process[] pr, int n, int timeOUT)
{
Console.WriteLine("
PRIORITY preemptive
PROCESS");
Console.WriteLine("{0} {1} {2} {3} {4} {5} {6} {7}
",
"Name", "TimeRQ", "TimeCPU", "Priority", "TimeIN", "TimeOUT", "Timewait",
"Timesave");
for (int i = 0; i < n; i++)
Console.WriteLine("{0} {1} {2} {3} {4} {5} {6} {7}
",
pr[i].name, pr[i].timeRQ, pr[i].timeCPU, pr[i].priortity, pr[i].timeIN,
pr[i].timeOUT, pr[i].timewait, pr[i].timesave);
}
////In ket qua thuc hien do uu tien khong doc quyen
static void output_PRIORITY_nopreemptive(process[] pr, process[] RL, int n, int m, int timeOUT)
{
Console.WriteLine("
PRIORITY nopreemptive
OUTPUT");
Console.WriteLine("{0} {1} {2} {3}", "Name", "TimeOUT", "Timewait", "Timesave");
for (int i = 0; i < n; i++)
Console.WriteLine("{0} {1} {2} {3}", pr[i].name, pr[i].timeOUT, pr[i].timewait, pr[i].timesave);
Console.WriteLine("-------PROCESS------");
Console.WriteLine("{0} {1} {2} {3} {4} {5}", "Name", "TimeRQ", "TimeCPU", "priority", "TimeOUT", "TimeIN");
for (int i = 0; i < m; i++)
Console.WriteLine("{0} {1} {2} {3} {4} {5}", RL[i].name, RL[i].timeRQ, RL[i].timeCPU, RL[i].priortity, RL[i].timeIN, RL[i].timeOUT);
}
////In ket qua thuc hien SJF doc quyen
static void output_SJF_preemptive(process[] pr, int n, int timeOUT)
{
Console.WriteLine("
SJFpreemptive
PROCESS");
Console.WriteLine("{0} {1} {2} {3} {4} {5} {6} {7}", "name", "TimeRQ", "TimeCPU", "Priority", "TimeIN", "TimeOUT", "Timewait", "Timesave");
for (int i = 0; i < n; i++)
Console.WriteLine("{0} {1} {2} {3} {4} {5} {6} {7}", pr[i].name, pr[i].timeRQ, pr[i].timeCPU, pr[i].priortity, pr[i].timeIN, pr[i].timeOUT, pr[i].timewait, pr[i].timesave);
}
static void output_SJF_nopreemptive(process[] pr, process[] RL, int n, int m, int timeOUT)
{
Console.WriteLine("
SJF nopreemptive
OUTPUT");
Console.WriteLine("{0} {1} {2} {3}", "Name", "TimeOUT", "Timewait", "Timesave");
for (int i = 0; i < n; i++)
Console.WriteLine("{0} {1} {2} {3}", pr[i].name, pr[i].timeOUT, pr[i].timewait, pr[i].timesave);
Console.WriteLine("-------PROCESS------");
Console.WriteLine("{0} {1} {2} {3} {4} {5}", "Name", "TimeRQ", "TimeCPU", "priority", "TimeOUT", "TimeIN");
for (int i = 0; i < m; i++)
Console.WriteLine("{0} {1} {2} {3} {4} {5}", RL[i].name, RL[i].timeRQ, RL[i].timeCPU, RL[i].priortity, RL[i].timeIN, RL[i].timeOUT);
}
//Thuc hien dieu phoi RoundRobin
static void process_RR(ref process[] pr, ref process[] RQ, int n, ref int m, int timeOUT, int quantum)
{
m = 0;
int count = 0;
int j = 0;
int temptime = 0;
for (int t = 0; t <= timeOUT; t++)
{
if (m > 0 && j < m)
{
count++;
if (count <= quantum && RQ[j].timeCPU - temptime > 0)
temptime++;
if (count == quantum && RQ[j].timeCPU - temptime > 0)
{
m++;
RQ[m - 1] = RQ[j];
RQ[m - 1].timeCPU -= temptime;
}
if (RQ[j].timeCPU - temptime == 0)
{
pr[RQ[j].index].timeOUT = t;
pr[RQ[j].index].timewait = pr[RQ[j].index].timeOUT
- (pr[RQ[j].index].timeRQ + pr[RQ[j].index].timeCPU);
pr[RQ[j].index].timesave = pr[RQ[j].index].timeOUT - pr[RQ[j].index].timeRQ;
}
if (count == quantum || RQ[j].timeCPU - temptime == 0)
{
RQ[j].timeOUT = t;
RQ[j].timeCPU = temptime;
RQ[j].timeIN = t - RQ[j].timeCPU;
j++;
temptime = 0;
count = 0;
}
}
for (int i = 0; i < n; i++)
if (t == pr[i].timeRQ)
{
m++;
RQ[m - 1] = pr[i];
}
}
}
//In ket qua dieu phoi RoundRobin
static void output_RR(process[] pr, process[] RQ, int n, int m, int timeOUT)
{
Console.WriteLine("
ROUND ROBIN
OUTPUT
");
Console.WriteLine("{0} {1} {2} {3}", "Name", "TimeOUT", "Timewait", "Timesave");
for (int i = 0; i < n; i++)
{
Console.WriteLine("{0} {1} {2} {3}", pr[i].name, pr[i].timeOUT,
pr[i].timewait, pr[i].timesave);
}
Console.WriteLine("
---PROCESS---
");
Console.WriteLine("{0} {1} {2} {3} {4}", "Name", "TimeRQ", "TimeCPU",
"TimeIN", "TimeOUT");
for (int i = 0; i < m; i++)
{
Console.WriteLine("{0} {1} {2} {3} {4}
", RQ[i].name, RQ[i].timeRQ,
RQ[i].timeCPU, RQ[i].timeIN, RQ[i].timeOUT);
}
}
//sjf docquyem
static void process_SJF_preemptive(process[] pr, int n, int timeOUT)
{
process[] RQ = new process[n];
int j = 0, m = 0;
int temptime = 0;
for (int t = 0; t <= timeOUT; t++)
{
if (m > 0 && j < m)
{
if (temptime < RQ[j].timeCPU)
temptime++;
if (temptime == RQ[j].timeCPU)
{
RQ[j].timeIN = t - RQ[j].timeCPU;
RQ[j].timeOUT = RQ[j].timeIN + RQ[j].timeCPU;
RQ[j].timewait = RQ[j].timeOUT - (RQ[j].timeRQ + RQ[j].timeCPU);
RQ[j].timesave = RQ[j].timeOUT - RQ[j].timeRQ;
temptime = 0;
j++;
}
}
for (int i = 0; i < n; i++)
if (t == pr[i].timeRQ)
{
int k = m;
while (k > j + 1 && pr[i].timeCPU < RQ[k - 1].timeCPU)
{
RQ[k] = RQ[k - 1];
k--;
}
RQ[k] = pr[i];
m++;
}
}
pr = RQ;
}
//jsf ko doc quyen
static void process_SJF_nopreemptive(process[] pr, int n, int m, int timeOUT)
{
process[] RQ = new process[n];
int j = 0;
m = 0;
int temptime = 0;
for (int t = 0; t <= timeOUT; t++)
{
if (m > 0 && j < m)
{
if (temptime < RQ[j].timeCPU)
temptime++;
if (temptime == RQ[j].timeCPU)
{
RQ[j].timeIN = t - RQ[j].timeCPU;
RQ[j].timeOUT = RQ`indent preformatted text by 4 spaces`[j].timeIN + RQ[j].timeCPU;
RQ[j].timewait = RQ[j].timeOUT - (RQ[j].timeRQ + RQ[j].timeCPU);
RQ[j].timesave = RQ[j].timeOUT - RQ[j].timeRQ;
pr[RQ[j].index].timeOUT = t;
pr[RQ[j].index].timewait = pr[RQ[j].index].timeOUT
- (pr[RQ[j].index].timeRQ + pr[RQ[j].index].timeCPU);
pr[RQ[j].index].timesave = pr[RQ[j].index].timeOUT - pr[RQ[j].index].timeRQ;
temptime = 0;
j++;
}
}
for (int i = 0; i < n; i++)
if (t == pr[i].timeRQ)
{
m++;
int k = m - 1;
if (temptime > 0 && pr[i].timeCPU < RQ[j].timeCPU - temptime)
{
m++;
k = m - 1;
for (k = m - 1; k > j + 1; k--)
RQ[k] = RQ[k - 2];
RQ[j + 1] = pr[i];
RQ[j + 2] = RQ[j];
RQ[j + 2].timeCPU -= temptime;
RQ[j].timeIN = t - temptime;
RQ[j].timeOUT = t;
RQ[j].timeCPU = temptime;
temptime = 0;
j++;
}
else
{
while (k > j + 1 && pr[i].timeCPU < RQ[k - 1].timeCPU)
{
RQ[k] = RQ[k - 1];
k--;
if (k == j + 1
&& pr[i].timeCPU < RQ[k - 1].timeCPU - temptime)
{
RQ[k] = RQ[k - 1];
k--;
}
}
RQ[k] = pr[i];
}
}
}
}
//Thuc hien dieu phoi
static void PROCESS(ref process[] pr, ref process[] RQ ,int n, ref int m, int timeOUT, int
select)
{
switch (select)
{
case 1:
process_FIFO(ref pr, n, timeOUT);
break;
case 2:
process_RR(ref pr, ref RQ, n, ref m, timeOUT, quantum);
break;
case 3:
process_PRIORITY_preemptive(pr, n, timeOUT);
break;
case 4:
process_PRIORITY_nopreemptive(pr, n, m, timeOUT);
break;
case 5:
process_SJF_preemptive(pr, n, timeOUT);
break;
case 6:
process_SJF_nopreemptive(pr, n, m, timeOUT);
break;
}
}
//In ket quan dieu phoi
static void OUTPUT(process[] pr, process[] RQ, int n, int m, int timeOUT, int select)
{
switch (select)
{
case 1:
output_FIFO(pr, n, timeOUT);
break;
case 2:
output_RR(pr, RQ, n, m, timeOUT);
break;
case 3:
output_PRIORITY_preemptive(pr, n, timeOUT);
break;
case 4:
output_PRIORITY_nopreemptive(pr, RQ, n, m, timeOUT);
break;
case 5:
output_SJF_preemptive(pr, n, timeOUT);
break;
case 6:
output_SJF_nopreemptive(pr, RQ, n, m, timeOUT);
break;
}
Console.WriteLine("-----------------END-----------------");
}
static void Main(string[] args)
{
process[] pr = new process[100]; //Danh sach cac tien trinh
process[] RQ = new process[100]; //Danh sach cac tien trinh trong RQ
int n = 0; //So tien trinh dang xu ly
int m = 0; //So tien trinh trong RQ
int timeOUT = 0; //Thoi diem ket cua tat ca cac tien trinh
input(ref pr, ref n, ref timeOUT); //Doc File du lieu dau vao => xac dinh pr, n,
output_input(pr, n);
PROCESS(ref pr, ref RQ, n, ref m, timeOUT, set);
OUTPUT(pr, RQ, n, m, timeOUT, set);
Console.ReadLine();
}
}
Bài liên quan
@ltd giúp em với anh đạt ơi
bạn ơi mình không hiểu rõ ý tưởng bài của bạn lắm @@ bạn có thể nói rõ hơn đc không