-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcess.java
More file actions
186 lines (155 loc) · 4.37 KB
/
Process.java
File metadata and controls
186 lines (155 loc) · 4.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import java.util.ArrayList;
import java.util.Queue;
import java.util.*;
import java.io.*;
public class Process {
public ArrayList ownedVariables;
int countOfInstr; // Next Instruction to be executed
private int maximumInstr;
static int pnum;
int pid;
public File ProgramFile;
InterpreterEngine interpreterEngine;
InterpreterClient interpreterClient;
public Scanner sc;
boolean isBlocked ;
int arrival;
String tempValue;
String tempVariable;
public Process(String programPath,int arrival) throws FileNotFoundException {
ownedVariables = new ArrayList<String>();
this.countOfInstr = 1;
this.ProgramFile = new File(programPath);
this.maximumInstr = this.countProgramsize();
this.sc = new Scanner(ProgramFile);
pnum++;
this.pid=pnum;
this.isBlocked=false;
this.arrival=arrival;
}
// METHODS
public int countProgramsize() {
int count = 0;
try {
BufferedReader counting = new BufferedReader(new FileReader(this.ProgramFile));
String line=counting.readLine();
while (line!= null) {
if(line.contains("assign")&&(line.contains("read") || line.contains("input")))
{count+=2;
}
else {count++;
}
line=counting.readLine();
}
counting.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return count;
}
public int getMaximumInstr() {
return maximumInstr;
}
public void setMaximumInstr(int maximumInstr) {
this.maximumInstr = maximumInstr;
}
public void execute(mutex userInput ,mutex userOutput ,mutex file) {
InterpreterEngine interpreterEngine = new InterpreterEngine();
InterpreterClient interpreterClient = new InterpreterClient(interpreterEngine);
//check if tempvalue is not null
//so we can pass its value to whatever assign operation
if(this.tempValue!=null)
{
System.out.println("Process: "+ pid +" is executing" );
System.out.println(interpreterClient.interpret("assign " + this.tempVariable +" "+ this.tempValue, this));
this.tempValue=null;
this.tempVariable=null;
}
else if (sc.hasNextLine()) {
System.out.println("Process: "+ pid +" is executing" );
String line = sc.nextLine();
if(line.contains("semWait")||line.contains("semSignal")) {
mutex_method(line, userInput , userOutput , file);
}
else {
System.out.println(interpreterClient.interpret(line, this));
}
}
else {this.countOfInstr++ ;}
}
public void mutex_method(String line,mutex userInput ,mutex userOutput ,mutex file) {
if(line.contains("semWait")) {
if (line.contains("userInput"))
{
System.out.println("semWait userInput");
this.semWait(userInput);
}
if (line.contains("userOutput")) {
System.out.println("semWait userOutput");
this.semWait(userOutput);
}
if (line.contains("file")) {
System.out.println("semWait file");
this.semWait(file);
}
}
if(line.contains("semSignal")) {
if (line.contains("userInput")) {
System.out.println("semSignal userInput");
this.semSignal(userInput);
}
if (line.contains("userOutput")) {
System.out.println("semSignal userOutput");
this.semSignal(userOutput);
}
if (line.contains("file")) {
System.out.println("semSignal file");
this.semSignal(file);
}
}
}
public void getblocked(Queue<Process> GnrlBlocked, Queue<Process> resourceBlocked) {
GnrlBlocked.add(this);
resourceBlocked.add(this);
}
public void addtoReadyQueue(Queue<Process> ReadyQueue) {
}
//Mutex METHODS
//SEMWAIT
public void semWait (mutex m) {
if(!m.lock) {
m.lock=true;
m.ownerID=this.pid;
}
else {
this.getblocked(m.Gnrl, m.blocked);
m.printBlocked();
this.isBlocked=true;
}
this.countOfInstr++;
}
//SEMSIGNAL
public void semSignal(mutex m) {
if(m.ownerID==this.pid)
{
if(m.blocked.isEmpty())
m.lock=false;
else
{
Process removed=(Process)m.blocked.remove();
int size = m.Gnrl.size();
for(int i=0;i<size;i++)
{
Process g =(Process)m.Gnrl.remove();
if(!removed.equals(g))
m.Gnrl.add(g);
}
m.ownerID= removed.pid;
removed.isBlocked=false;
m.readylist.add(removed);
}
}
this.countOfInstr++;
}
}