Skip to content

Commit 0da178e

Browse files
authored
Add files via upload
1 parent 1966475 commit 0da178e

9 files changed

+660
-0
lines changed

CPU.java

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
3+
import java.util.Queue;
4+
5+
public class CPU
6+
{ public static boolean available;
7+
public static int countNormally;
8+
public static int countAbNormally;
9+
public static OS os;
10+
11+
12+
public static void main(String[] args)
13+
{ available = true;
14+
15+
os = new OS();
16+
Queue<PCB> ready = os.CPUScheduler();
17+
18+
// CPU Scheduler
19+
while(ready.size() > 0)
20+
{ PCB p = null;
21+
available = true;
22+
23+
if(available)
24+
{ available = false;
25+
System.out.println("\t-- Dispatching Process --\n");
26+
27+
p = ready.poll();
28+
p.run();
29+
30+
while(trackProcess(p))
31+
if(!trackProcess(p))
32+
{ os.ramSize += p.jobSize;
33+
break; } }
34+
35+
if(os.ramSize > os.min && os.jobs.size() > 0)
36+
{ System.out.println("\t-- Avilable memory in RAM: CPUScheduler is called -- \n");
37+
os.CPUScheduler(); } }
38+
39+
// Simulation finished
40+
41+
42+
// For output file
43+
os.generateOutput(countNormally, countAbNormally); }
44+
45+
46+
47+
// This method will return false if process is terminated
48+
public static boolean trackProcess(PCB p)
49+
{ p.cut++;
50+
int num = (int)(Math.random()*100);
51+
52+
if(p.cut > p.ecu)
53+
{ termination(p, false);
54+
return false; }
55+
56+
else
57+
if(num <= 10)
58+
{ termination(p, true);
59+
return false; }
60+
61+
else
62+
if( num <= 15)
63+
{ termination(p, false);
64+
return false; }
65+
66+
return true; }
67+
68+
69+
70+
public static void termination(PCB p, boolean normal)
71+
{ p.terminate();
72+
available = true;
73+
74+
os.ramSize += p.jobSize;
75+
76+
System.out.printf("Process Termination \nPID: %d CUT: %d Size: %d KB ", p.pid, p.cut, p.jobSize);
77+
78+
if(normal)
79+
{ countNormally++;
80+
System.out.println("Normal termination. \n"); }
81+
82+
else
83+
{ countAbNormally++;
84+
System.out.println("Abnormal termination. \n"); } } }
85+
86+

Input.java

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
2+
3+
import java.io.File;
4+
import java.io.FileOutputStream;
5+
import java.io.PrintWriter;
6+
import java.util.Scanner;
7+
import java.util.Queue;
8+
import java.util.LinkedList;
9+
10+
public class Input
11+
{ public Queue<Job> jobs;
12+
public int count;
13+
int min;
14+
int max;
15+
int total;
16+
int n; // number of jobs
17+
18+
public Input()
19+
{ jobs= new LinkedList<Job>();
20+
count= 0;
21+
min= 256;
22+
max= 0;
23+
total= 0;
24+
n= (int)((Math.random()*(500 - 100) + 100)); }
25+
26+
27+
28+
public Job generateJob()
29+
{ int id= count++;
30+
int ecu= (int)((Math.random()*(512 - 16) + 16));
31+
int jobSize= (int)((Math.random()*(256 - 16) + 16));
32+
33+
if(jobSize < min)
34+
min= jobSize;
35+
36+
if(max < jobSize)
37+
max= jobSize;
38+
39+
total += jobSize;
40+
Job obj= new Job(id, ecu, jobSize);
41+
return obj; }
42+
43+
44+
45+
public void generateInput()
46+
{ File f= new File("Jobs.txt");
47+
FileOutputStream fOut;
48+
PrintWriter writer;
49+
50+
try
51+
{ fOut = new FileOutputStream(f);
52+
writer= new PrintWriter(fOut);
53+
54+
Job obj= null;
55+
for(int i= 0; i < n; i++)
56+
{ obj= generateJob();
57+
writer.println(obj.jID + "\t" + obj.ECU + "\t" + obj.jobSize + "\n"); }
58+
59+
writer.close();
60+
fOut.close(); }
61+
catch (Exception e)
62+
{ e.getMessage(); } }
63+
64+
65+
66+
public void fillJQueue()
67+
{ File f= new File("Jobs.txt");
68+
Scanner input;
69+
70+
try
71+
{ input= new Scanner(f);
72+
73+
Job obj;
74+
int id;
75+
int ecu;
76+
int size;
77+
while(input.hasNext())
78+
{ id= input.nextInt();
79+
ecu= input.nextInt();
80+
size= input.nextInt();
81+
obj= new Job(id, ecu, size);
82+
83+
jobs.add(obj); }
84+
85+
input.close(); }
86+
catch (Exception e)
87+
{ e.getMessage(); } } }
88+
89+
90+

Job.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
3+
4+
public class Job
5+
{ public int jID;
6+
public int jobSize;
7+
public int ECU;
8+
9+
10+
public Job(int id, int ecu, int size)
11+
{ jID= id;
12+
ECU= ecu;
13+
jobSize= size; }
14+
15+
16+
public String toString()
17+
{ String str= jID + " " + ECU;
18+
return str; } }

JobComparator.java

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
3+
import java.util.Comparator;
4+
5+
public class JobComparator implements Comparator<Job>
6+
{
7+
public int compare(Job a, Job b)
8+
{ return a.jobSize - b.jobSize; }
9+
}

0 commit comments

Comments
 (0)